blob: 1cfac0e9d0bcf466847b0f06183cb537597d11e9 [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 Moolenaar7a39dd72019-06-23 00:50:15 +020064 let buf = term_start(cmd, {
65 \ 'curwin': 1,
66 \ 'term_rows': rows,
67 \ 'term_cols': cols,
68 \ })
69 if &termwinsize == ''
70 " in the GUI we may end up with a different size, try to set it.
71 if term_getsize(buf) != [rows, cols]
72 call term_setsize(buf, rows, cols)
73 endif
74 call assert_equal([rows, cols], term_getsize(buf))
75 else
76 let rows = term_getsize(buf)[0]
77 let cols = term_getsize(buf)[1]
78 endif
79
80 " Wait for "All" or "Top" of the ruler to be shown in the last line or in
81 " the status line of the last window. This can be quite slow (e.g. when
82 " using valgrind).
83 " If it fails then show the terminal contents for debugging.
84 try
85 call WaitFor({-> len(term_getline(buf, rows)) >= cols - 1 || len(term_getline(buf, rows - statusoff)) >= cols - 1})
86 catch /timed out after/
87 let lines = map(range(1, rows), {key, val -> term_getline(buf, val)})
88 call assert_report('RunVimInTerminal() failed, screen contents: ' . join(lines, "<NL>"))
89 endtry
90
91 return buf
92endfunc
93
94" Stop a Vim running in terminal buffer "buf".
95func StopVimInTerminal(buf)
96 call assert_equal("running", term_getstatus(a:buf))
97
98 " CTRL-O : works both in Normal mode and Insert mode to start a command line.
99 " In Command-line it's inserted, the CTRL-U removes it again.
100 call term_sendkeys(a:buf, "\<C-O>:\<C-U>qa!\<cr>")
101
102 call WaitForAssert({-> assert_equal("finished", term_getstatus(a:buf))})
103 only!
104endfunc