robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 9a29622b3b00ddb601

master
15.8 KiB457 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4local tt = require('test.functional.testterm')
5
6local describe, it, before_each = t.describe, t.it, t.before_each
7local assert_alive = n.assert_alive
8local feed, clear = n.feed, n.clear
9local api = n.api
10local testprg, command = n.testprg, n.command
11local fn = n.fn
12local nvim_set = n.nvim_set
13local is_os = t.is_os
14local skip = t.skip
15
16describe(':terminal highlight', function()
17  local screen
18
19  before_each(function()
20    clear()
21    screen = Screen.new(50, 7, { rgb = false })
22    screen:set_default_attr_ids({
23      [1] = { foreground = 45 },
24      [2] = { background = 46 },
25      [3] = { foreground = 45, background = 46 },
26      [4] = { bold = true, italic = true, underline = true, strikethrough = true },
27      [5] = { bold = true },
28      [6] = { foreground = 12 },
29      [7] = { bold = true, reverse = true },
30      [8] = { background = 11 },
31      [9] = { foreground = 130 },
32      [10] = { reverse = true },
33      [11] = { background = 11 },
34      [12] = { bold = true, underdouble = true },
35      [13] = { italic = true, undercurl = true },
36    })
37    command(("enew | call jobstart(['%s'], {'term':v:true})"):format(testprg('tty-test')))
38    feed('i')
39    screen:expect([[
40      tty ready                                         |
41      ^                                                  |
42                                                        |*4
43      {5:-- TERMINAL --}                                    |
44    ]])
45  end)
46
47  local function descr(title, attr_num, set_attrs_fn)
48    local function sub(s)
49      local str = s:gsub('NUM', attr_num)
50      return str
51    end
52
53    describe(title, function()
54      before_each(function()
55        set_attrs_fn()
56        tt.feed_data('text')
57        tt.clear_attrs()
58        tt.feed_data('text')
59      end)
60
61      local function pass_attrs()
62        screen:expect(sub([[
63          tty ready                                         |
64          {NUM:text}text^                                          |
65                                                            |*4
66          {5:-- TERMINAL --}                                    |
67        ]]))
68      end
69
70      it('will pass the corresponding attributes', pass_attrs)
71
72      it('will pass the corresponding attributes on scrollback', function()
73        pass_attrs()
74        local lines = {}
75        for i = 1, 8 do
76          table.insert(lines, 'line' .. tostring(i))
77        end
78        table.insert(lines, '')
79        tt.feed_data(lines)
80        screen:expect([[
81          line4                                             |
82          line5                                             |
83          line6                                             |
84          line7                                             |
85          line8                                             |
86          ^                                                  |
87          {5:-- TERMINAL --}                                    |
88        ]])
89        feed('<c-\\><c-n>gg')
90        screen:expect(sub([[
91          ^tty ready                                         |
92          {NUM:text}textline1                                     |
93          line2                                             |
94          line3                                             |
95          line4                                             |
96          line5                                             |
97                                                            |
98        ]]))
99      end)
100    end)
101  end
102
103  descr('foreground', 1, function()
104    tt.set_fg(45)
105  end)
106  descr('background', 2, function()
107    tt.set_bg(46)
108  end)
109  descr('foreground and background', 3, function()
110    tt.set_fg(45)
111    tt.set_bg(46)
112  end)
113  descr('bold, italics, underline and strikethrough', 4, function()
114    tt.set_bold()
115    tt.set_italic()
116    tt.set_underline()
117    tt.set_strikethrough()
118  end)
119  descr('bold and underdouble', 12, function()
120    tt.set_bold()
121    tt.set_underdouble()
122  end)
123  descr('italics and undercurl', 13, function()
124    tt.set_italic()
125    tt.set_undercurl()
126  end)
127end)
128
129it(':terminal highlight has lower precedence than editor #9964', function()
130  clear()
131  local screen = Screen.new(30, 4, { rgb = true })
132  screen:set_default_attr_ids({
133    -- "Normal" highlight emitted by the child nvim process.
134    N_child = {
135      foreground = tonumber('0x4040ff'),
136      background = tonumber('0xffff40'),
137      fg_indexed = true,
138      bg_indexed = true,
139    },
140    -- "Search" highlight in the parent nvim process.
141    S = { background = Screen.colors.Green, italic = true, foreground = Screen.colors.Red },
142    -- "Question" highlight in the parent nvim process.
143    -- note: bg is indexed as it comes from the (cterm) child, while fg isn't as it comes from (rgb) parent
144    Q = {
145      background = tonumber('0xffff40'),
146      bold = true,
147      foreground = Screen.colors.SeaGreen4,
148      bg_indexed = true,
149    },
150  })
151  -- Child nvim process in :terminal (with cterm colors).
152  fn.jobstart({
153    n.nvim_prog,
154    '-n',
155    '-u',
156    'NORC',
157    '-i',
158    'NONE',
159    '--cmd',
160    nvim_set .. ' notermguicolors',
161    '+hi Normal ctermfg=Blue ctermbg=Yellow',
162    '+norm! ichild nvim',
163    '+norm! oline 2',
164  }, {
165    term = true,
166    env = {
167      VIMRUNTIME = os.getenv('VIMRUNTIME'),
168    },
169  })
170  screen:expect([[
171    {N_child:^child nvim                    }|
172    {N_child:line 2                        }|
173    {N_child:                              }|
174                                  |
175  ]])
176  command('hi Search gui=italic guifg=Red guibg=Green cterm=italic ctermfg=Red ctermbg=Green')
177  feed('/nvim<cr>')
178  screen:expect([[
179    {N_child:child }{S:^nvim}{N_child:                    }|
180    {N_child:line 2                        }|
181    {N_child:                              }|
182    /nvim                         |
183  ]])
184  command('syntax keyword Question line')
185  screen:expect([[
186    {N_child:child }{S:^nvim}{N_child:                    }|
187    {Q:line}{N_child: 2                        }|
188    {N_child:                              }|
189    /nvim                         |
190  ]])
191end)
192
193it('CursorLine and CursorColumn work in :terminal buffer in Normal mode', function()
194  clear()
195  local screen = Screen.new(50, 7)
196  screen:add_extra_attr_ids({ [100] = { background = Screen.colors.Gray90, reverse = true } })
197  command(("enew | call jobstart(['%s'], {'term':v:true})"):format(testprg('tty-test')))
198  screen:expect([[
199    ^tty ready                                         |
200                                                      |*6
201  ]])
202  tt.feed_data((' foobar'):rep(30))
203  screen:expect([[
204    ^tty ready                                         |
205     foobar foobar foobar foobar foobar foobar foobar |
206    foobar foobar foobar foobar foobar foobar foobar f|
207    oobar foobar foobar foobar foobar foobar foobar fo|
208    obar foobar foobar foobar foobar foobar foobar foo|
209    bar foobar                                        |
210                                                      |
211  ]])
212  command('set cursorline cursorcolumn')
213  feed('j10w')
214  screen:expect([[
215    tty ready     {21: }                                   |
216     foobar foobar{21: }foobar foobar foobar foobar foobar |
217    {21:foobar foobar ^foobar foobar foobar foobar foobar f}|
218    oobar foobar f{21:o}obar foobar foobar foobar foobar fo|
219    obar foobar fo{21:o}bar foobar foobar foobar foobar foo|
220    bar foobar    {21: }                                   |
221                                                      |
222  ]])
223  -- Entering terminal mode disables 'cursorline' and 'cursorcolumn'.
224  feed('i')
225  screen:expect([[
226    tty ready                                         |
227     foobar foobar foobar foobar foobar foobar foobar |
228    foobar foobar foobar foobar foobar foobar foobar f|
229    oobar foobar foobar foobar foobar foobar foobar fo|
230    obar foobar foobar foobar foobar foobar foobar foo|
231    bar foobar^                                        |
232    {5:-- TERMINAL --}                                    |
233  ]])
234  -- Leaving terminal mode restores old values.
235  feed([[<C-\><C-N>]])
236  screen:expect([[
237    tty ready{21: }                                        |
238     foobar f{21:o}obar foobar foobar foobar foobar foobar |
239    foobar fo{21:o}bar foobar foobar foobar foobar foobar f|
240    oobar foo{21:b}ar foobar foobar foobar foobar foobar fo|
241    obar foob{21:a}r foobar foobar foobar foobar foobar foo|
242    {21:bar fooba^r                                        }|
243                                                      |
244  ]])
245
246  -- CursorLine and CursorColumn are combined with terminal colors.
247  tt.set_reverse()
248  tt.feed_data(' foobar')
249  tt.clear_attrs()
250  screen:expect([[
251    tty ready{21: }                                        |
252     foobar f{21:o}obar foobar foobar foobar foobar foobar |
253    foobar fo{21:o}bar foobar foobar foobar foobar foobar f|
254    oobar foo{21:b}ar foobar foobar foobar foobar foobar fo|
255    obar foob{21:a}r foobar foobar foobar foobar foobar foo|
256    {21:bar fooba^r}{100: foobar}{21:                                 }|
257                                                      |
258  ]])
259  feed('2gg15|')
260  screen:expect([[
261    tty ready     {21: }                                   |
262    {21: foobar foobar^ foobar foobar foobar foobar foobar }|
263    foobar foobar {21:f}oobar foobar foobar foobar foobar f|
264    oobar foobar f{21:o}obar foobar foobar foobar foobar fo|
265    obar foobar fo{21:o}bar foobar foobar foobar foobar foo|
266    bar foobar{2: foo}{100:b}{2:ar}                                 |
267                                                      |
268  ]])
269
270  -- Set bg color to red
271  tt.feed_csi('48;2;255:0:0m')
272  tt.feed_data(' foobar')
273  tt.clear_attrs()
274  feed('2gg20|')
275
276  -- Terminal color has higher precedence
277  screen:expect([[
278    tty ready          {21: }                              |
279    {21: foobar foobar foob^ar foobar foobar foobar foobar }|
280    foobar foobar fooba{21:r} foobar foobar foobar foobar f|
281    oobar foobar foobar{21: }foobar foobar foobar foobar fo|
282    obar foobar foobar {21:f}oobar foobar foobar foobar foo|
283    bar foobar{2: foobar}{30: foobar}                          |
284                                                      |
285  ]])
286  feed('G$')
287  screen:expect([[
288    tty ready              {21: }                          |
289     foobar foobar foobar f{21:o}obar foobar foobar foobar |
290    foobar foobar foobar fo{21:o}bar foobar foobar foobar f|
291    oobar foobar foobar foo{21:b}ar foobar foobar foobar fo|
292    obar foobar foobar foob{21:a}r foobar foobar foobar foo|
293    {21:bar foobar}{100: foobar}{30: fooba^r}{21:                          }|
294                                                      |
295  ]])
296end)
297
298describe(':terminal highlight forwarding', function()
299  local screen
300
301  before_each(function()
302    clear()
303    screen = Screen.new(50, 7)
304    screen:set_rgb_cterm(true)
305    screen:set_default_attr_ids({
306      [1] = { { bold = true }, { bold = true } },
307      [2] = { { fg_indexed = true, foreground = tonumber('0xe0e000') }, { foreground = 3 } },
308      [3] = { { foreground = tonumber('0xff8000') }, {} },
309    })
310    command(("enew | call jobstart(['%s'], {'term':v:true})"):format(testprg('tty-test')))
311    feed('i')
312    screen:expect([[
313      tty ready                                         |
314      ^                                                  |
315                                                        |*4
316      {1:-- TERMINAL --}                                    |
317    ]])
318  end)
319
320  it('will handle cterm and rgb attributes', function()
321    skip(is_os('win'))
322    tt.set_fg(3)
323    tt.feed_data('text')
324    tt.feed_termcode('[38:2:255:128:0m')
325    tt.feed_data('color')
326    tt.clear_attrs()
327    tt.feed_data('text')
328    screen:expect([[
329      tty ready                                         |
330      {2:text}{3:color}text^                                     |
331                                                        |*4
332      {1:-- TERMINAL --}                                    |
333    ]])
334  end)
335
336  it('handles truecolor SGR with a colour space id', function()
337    skip(is_os('win'))
338    tt.feed_termcode('[38:2::255:128:0m')
339    tt.feed_data('color')
340    tt.clear_attrs()
341    tt.feed_data('text')
342    screen:expect([[
343      tty ready                                         |
344      {3:color}text^                                         |
345                                                        |*4
346      {1:-- TERMINAL --}                                    |
347    ]])
348  end)
349end)
350
351--- @param buflocal boolean
352local function test_term_hl_custom_palette(buflocal)
353  local screen
354
355  before_each(function()
356    clear()
357    screen = Screen.new(50, 7, { rgb = true })
358    command('enew')
359    if buflocal then
360      api.nvim_buf_set_var(0, 'terminal_color_3', '#123456')
361    else
362      api.nvim_set_var('terminal_color_3', '#123456')
363    end
364    screen:add_extra_attr_ids({ [100] = { foreground = tonumber('0x123456') } })
365  end)
366
367  it('will use the custom color with jobstart()', function()
368    command(("call jobstart(['%s'], {'term': v:true})"):format(testprg('tty-test')))
369    feed('i')
370    screen:expect([[
371      tty ready                                         |
372      ^                                                  |
373                                                        |*4
374      {5:-- TERMINAL --}                                    |
375    ]])
376    tt.set_fg(3)
377    tt.feed_data('text')
378    tt.clear_attrs()
379    tt.feed_data('text')
380    screen:expect([[
381      tty ready                                         |
382      {100:text}text^                                          |
383                                                        |*4
384      {5:-- TERMINAL --}                                    |
385    ]])
386  end)
387
388  it('will use the custom color with nvim_open_term() in non-curbuf', function()
389    local oldbuf = api.nvim_get_current_buf()
390    command('set laststatus=0 | vnew')
391    local chan = api.nvim_open_term(oldbuf, {})
392    api.nvim_chan_send(chan, '\027[38;5;3mtext\027[0;10mtext')
393    screen:expect([[
394      ^                         │{100:text}text                |
395      {1:~                        }│                        |*5
396                                                        |
397    ]])
398  end)
399end
400
401describe(':terminal highlight with custom palette', function()
402  describe('using g:termimal_color_*', function()
403    test_term_hl_custom_palette(false)
404  end)
405
406  describe('using b:termimal_color_*', function()
407    test_term_hl_custom_palette(true)
408  end)
409end)
410
411describe(':terminal', function()
412  before_each(clear)
413
414  it('can display URLs', function()
415    local screen = Screen.new(50, 7)
416    screen:add_extra_attr_ids({ [100] = { url = 'https://example.com' } })
417    local chan = api.nvim_open_term(0, {})
418    api.nvim_chan_send(
419      chan,
420      'This is an \027]8;;https://example.com\027\\example\027]8;;\027\\ of a link'
421    )
422    screen:expect([[
423      ^This is an {100:example} of a link                      |
424                                                        |*6
425    ]])
426    -- Also works if OSC 8 is split into multiple fragments.
427    api.nvim_chan_send(chan, '\nThis is another \027]8;;https')
428    n.poke_eventloop()
429    api.nvim_chan_send(chan, '://')
430    n.poke_eventloop()
431    api.nvim_chan_send(chan, 'example')
432    n.poke_eventloop()
433    api.nvim_chan_send(chan, '.com\027\\EXAMPLE\027]8;;\027\\ of a link')
434    screen:expect([[
435      ^This is an {100:example} of a link                      |
436      This is another {100:EXAMPLE} of a link                 |
437                                                        |*5
438    ]])
439  end)
440
441  it('zoomout with large horizontal output #30374', function()
442    skip(is_os('win'))
443
444    -- Start terminal smaller.
445    local screen = Screen.new(50, 50, { rgb = false })
446    feed([[:terminal<cr>]])
447
448    -- Generate very wide output.
449    feed('ifor i in $(seq 1 10000); do echo -n $i; done\r\n')
450
451    -- Make terminal big.
452    screen:try_resize(5000, 5000)
453    command('call jobresize(b:terminal_job_id, 5000, 5000)')
454
455    assert_alive()
456  end)
457end)