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