xref: /vim-8.2.3635/src/testdir/term_util.vim (revision 30e8e735)
1" 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 = GetVimCommandCleanTerm() .. a:arguments
63
64  let options = {
65	\ 'curwin': 1,
66	\ 'term_rows': rows,
67	\ 'term_cols': cols,
68	\ }
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
74  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
96  return buf
97endfunc
98
99" Stop a Vim running in terminal buffer "buf".
100func StopVimInTerminal(buf)
101  call assert_equal("running", term_getstatus(a:buf))
102
103  " CTRL-O : works both in Normal mode and Insert mode to start a command line.
104  " In Command-line it's inserted, the CTRL-U removes it again.
105  call term_sendkeys(a:buf, "\<C-O>:\<C-U>qa!\<cr>")
106
107  call WaitForAssert({-> assert_equal("finished", term_getstatus(a:buf))})
108  only!
109endfunc
110