summaryrefslogtreecommitdiffstatshomepage
path: root/test/functional/terminal/altscreen_spec.lua
blob: 40917dd0f70b39f2896d87dd923e840c13b0067f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
local t = require('test.testutil')
local n = require('test.functional.testnvim')()

local tt = require('test.functional.testterm')
local clear, eq, api = n.clear, t.eq, n.api
local feed = n.feed
local feed_data = tt.feed_data
local enter_altscreen = tt.enter_altscreen
local exit_altscreen = tt.exit_altscreen

describe(':terminal altscreen', function()
  local screen

  before_each(function()
    clear()
    screen = tt.setup_screen()
    feed_data({
      'line1',
      'line2',
      'line3',
      'line4',
      'line5',
      'line6',
      'line7',
      'line8',
      '',
    })
    screen:expect([[
      line4                                             |
      line5                                             |
      line6                                             |
      line7                                             |
      line8                                             |
      ^                                                  |
      {5:-- TERMINAL --}                                    |
    ]])
    enter_altscreen()
    screen:expect([[
                                                        |*5
      ^                                                  |
      {5:-- TERMINAL --}                                    |
    ]])
    eq(10, api.nvim_buf_line_count(0))
  end)

  it('wont clear lines already in the scrollback', function()
    feed('<c-\\><c-n>gg')
    screen:expect([[
      ^tty ready                                         |
      line1                                             |
      line2                                             |
      line3                                             |
                                                        |*3
    ]])
    -- ED 3 is no-op in altscreen
    feed_data('\027[3J')
    screen:expect_unchanged()
  end)

  describe('restores buffer state', function()
    local function test_exit_altscreen_restores_buffer_state()
      exit_altscreen()
      screen:expect([[
        line4                                             |
        line5                                             |
        line6                                             |
        line7                                             |
        line8                                             |
        ^                                                  |
        {5:-- TERMINAL --}                                    |
      ]])
      feed('<c-\\><c-n>gg')
      screen:expect([[
        ^tty ready                                         |
        line1                                             |
        line2                                             |
        line3                                             |
        line4                                             |
        line5                                             |
                                                          |
      ]])
    end

    it('after exit', function()
      test_exit_altscreen_restores_buffer_state()
    end)

    it('after ED 2 and ED 3 and exit', function()
      feed_data('\027[H\027[2J\027[3J')
      screen:expect([[
        ^                                                  |
                                                          |*5
        {5:-- TERMINAL --}                                    |
      ]])
      test_exit_altscreen_restores_buffer_state()
    end)
  end)

  describe('with lines printed after the screen height limit', function()
    before_each(function()
      feed_data({
        'line9',
        'line10',
        'line11',
        'line12',
        'line13',
        'line14',
        'line15',
        'line16',
        '',
      })
      screen:expect([[
        line12                                            |
        line13                                            |
        line14                                            |
        line15                                            |
        line16                                            |
        ^                                                  |
        {5:-- TERMINAL --}                                    |
      ]])
    end)

    it('wont modify line count', function()
      eq(10, api.nvim_buf_line_count(0))
    end)

    it('wont modify lines in the scrollback', function()
      feed('<c-\\><c-n>gg')
      screen:expect([[
        ^tty ready                                         |
        line1                                             |
        line2                                             |
        line3                                             |
        line12                                            |
        line13                                            |
                                                          |
      ]])
    end)
  end)

  describe('after height is decreased by 2', function()
    local function wait_removal()
      screen:try_resize(screen._width, screen._height - 2)
      screen:expect([[
                                                          |*2
        rows: 4, cols: 50                                 |
        ^                                                  |
        {5:-- TERMINAL --}                                    |
      ]])
    end

    it('removes 2 lines from the bottom of the visible buffer', function()
      wait_removal()
      feed('<c-\\><c-n>4k')
      screen:expect([[
        ^                                                  |
                                                          |*2
        rows: 4, cols: 50                                 |
                                                          |
      ]])
      eq(9, api.nvim_buf_line_count(0))
    end)

    describe('and after exit', function()
      before_each(function()
        wait_removal()
        exit_altscreen()
      end)

      it('restore buffer state', function()
        screen:expect(t.is_os('win') and [[
          line6                                             |
          line7                                             |
          line8                                             |
          ^                                                  |
          {5:-- TERMINAL --}                                    |
        ]] or [[
          line5                                             |
          line6                                             |
          line7                                             |
          ^line8                                             |
          {5:-- TERMINAL --}                                    |
        ]])
      end)
    end)
  end)
end)