blob: c9e0ac528085381a6eabafc82d20d3e6a20a7497 [file] [log] [blame]
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001" Functions about terminal shared by several tests
2
3" Only load this script once.
4if exists('*CanRunVimInTerminal')
5 finish
6endif
7
8" For most tests we need to be able to run terminal Vim with 256 colors. On
9" MS-Windows the console only has 16 colors and the GUI can't run in a
10" terminal.
11func CanRunVimInTerminal()
12 return has('terminal') && !has('win32')
13endfunc
14
15" Skip the rest if there is no terminal feature at all.
16if !has('terminal')
17 finish
18endif
19
20" Stops the shell running in terminal "buf".
21func StopShellInTerminal(buf)
22 call term_sendkeys(a:buf, "exit\r")
23 let job = term_getjob(a:buf)
24 call WaitFor({-> job_status(job) == "dead"})
25endfunc
26
27" Run Vim with "arguments" in a new terminal window.
28" By default uses a size of 20 lines and 75 columns.
29" Returns the buffer number of the terminal.
30"
31" Options is a dictionary, these items are recognized:
32" "rows" - height of the terminal window (max. 20)
33" "cols" - width of the terminal window (max. 78)
34" "statusoff" - number of lines the status is offset from default
35func RunVimInTerminal(arguments, options)
36 " If Vim doesn't exit a swap file remains, causing other tests to fail.
37 " Remove it here.
38 call delete(".swp")
39
40 if exists('$COLORFGBG')
41 " Clear $COLORFGBG to avoid 'background' being set to "dark", which will
42 " only be corrected if the response to t_RB is received, which may be too
43 " late.
44 let $COLORFGBG = ''
45 endif
46
47 " Make a horizontal and vertical split, so that we can get exactly the right
48 " size terminal window. Works only when the current window is full width.
49 call assert_equal(&columns, winwidth(0))
50 split
51 vsplit
52
53 " Always do this with 256 colors and a light background.
54 set t_Co=256 background=light
55 hi Normal ctermfg=NONE ctermbg=NONE
56
57 " Make the window 20 lines high and 75 columns, unless told otherwise.
58 let rows = get(a:options, 'rows', 20)
59 let cols = get(a:options, 'cols', 75)
60 let statusoff = get(a:options, 'statusoff', 1)
61
Bram Moolenaar0d702022019-07-04 14:20:41 +020062 let cmd = GetVimCommandCleanTerm() .. a:arguments
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020063
Bram Moolenaard2842ea2019-09-26 23:08:54 +020064 let options = {
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020065 \ 'curwin': 1,
66 \ 'term_rows': rows,
67 \ 'term_cols': cols,
Bram Moolenaard2842ea2019-09-26 23:08:54 +020068 \ }
69 " Accept other options whose name starts with 'term_'.
70 call extend(options, filter(copy(a:options), 'v:key =~# "^term_"'))
71
72 let buf = term_start(cmd, options)
73
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020074 if &termwinsize == ''
75 " in the GUI we may end up with a different size, try to set it.
76 if term_getsize(buf) != [rows, cols]
77 call term_setsize(buf, rows, cols)
78 endif
79 call assert_equal([rows, cols], term_getsize(buf))
80 else
81 let rows = term_getsize(buf)[0]
82 let cols = term_getsize(buf)[1]
83 endif
84
85 " Wait for "All" or "Top" of the ruler to be shown in the last line or in
86 " the status line of the last window. This can be quite slow (e.g. when
87 " using valgrind).
88 " If it fails then show the terminal contents for debugging.
89 try
90 call WaitFor({-> len(term_getline(buf, rows)) >= cols - 1 || len(term_getline(buf, rows - statusoff)) >= cols - 1})
91 catch /timed out after/
92 let lines = map(range(1, rows), {key, val -> term_getline(buf, val)})
93 call assert_report('RunVimInTerminal() failed, screen contents: ' . join(lines, "<NL>"))
94 endtry
95
Bram Moolenaar3cdcb092020-03-18 19:18:10 +010096 " Starting a terminal to run Vim is always considered flaky.
97 let test_is_flaky = 1
98
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020099 return buf
100endfunc
101
102" Stop a Vim running in terminal buffer "buf".
103func StopVimInTerminal(buf)
Bram Moolenaar3cdcb092020-03-18 19:18:10 +0100104 " Using a terminal to run Vim is always considered flaky.
105 let test_is_flaky = 1
106
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200107 call assert_equal("running", term_getstatus(a:buf))
108
109 " CTRL-O : works both in Normal mode and Insert mode to start a command line.
110 " In Command-line it's inserted, the CTRL-U removes it again.
111 call term_sendkeys(a:buf, "\<C-O>:\<C-U>qa!\<cr>")
112
113 call WaitForAssert({-> assert_equal("finished", term_getstatus(a:buf))})
114 only!
115endfunc