virterm is a virtual terminal that executes a process in an off-screen terminal. You can control the process and the terminal with a lua script. It can be useful to make screenshots of or testing a cli/tui apps.
Supports Linux, Macos, Windows.
Features:
- make png screenshot
- make text screenshot
- wait until desired text appears in terminal
- send keys
- send mouse events
- send signal (unix)
- resize running terminal
- Usage
- Lua api
- vt.start(command [, params]) -> proc
- vt.sleep(duration_ms: int)
- proc:pid() -> int
- proc:cell(opts: table) -> table
- proc:contents() -> string
- proc:send_str(str: string)
- proc:send_key(key: string)
- proc:click(opts: table)
- proc:scroll(opts: table)
- proc:send_signal(signal: int | string)
- proc:kill()
- proc:resize(size: table)
- proc:wait()
- proc:wait_text(text:string [, opts])
- proc:dump_txt(path: string)
- proc:dump_png(path: string)
- Lua api
Run virterm my-script.lua
Example lua script:
local proc = vt.start("nvim", { width = 120, height = 20 })
print("Pid: " .. proc:pid())
-- Wait until the terminal screen contains "[No Name]" text.
proc:wait_text("[No Name]")
print(proc:contents())
proc:send_str("iHello")
proc:send_key("<Enter>")
proc:send_str("World")
proc:wait_text("World")
proc:resize({ width = 60, height = 30 })
vt.sleep(300)
proc:dump_png("screenshot.png")
proc:send_signal("SIGTERM")
proc:wait()
Starts a new process.
- command - Shell command to run. Example:
"vim file.txt"
. - params - Table with parameters
- height - Optional. Terminal height in rows. Default:
30
. - width - Optional. Terminal width in columns. Default:
80
.
- height - Optional. Terminal height in rows. Default:
Sleeps for duration_ms
milliseconds.
Returns process' pid.
Get info about one cell on the terminal screen.
- opts
- x - column (starts from 0).
- y - row (starts from 0).
Returns:
- content - Cell content as a string.
- fg - Cell foreground color if defined. Can be a number for index colors or a string for rgb colors.
- bg - Cell background color if defined. Can be a number for index colors or a string for rgb colors.
- bold
- italic
- underline
- inverse
- wide - True if the cell content takes more than one character width.
Returns terminal screen content as a string.
Sends a string to stdin of the process.
Sends a key as an input to the process (into stdin).
Key examples:
<a>
"a" key<C-a>
Control-a<S-a>
Shift-a<M-a>
Alt-a<Enter>
Enter key<Esc>
Escape key<BS>
Backspace<Left>
/<Right>
/<Up>
/<Down>
Send mouse click event.
- x - column (starts from 0).
- y - row (starts from 0).
- button = Optional. Mouse button. Possible values:
"left"
,"right"
,"middle"
. Default is"left"
.
Send mouse scroll event.
- x - column (top line is 0).
- y - row (left row is 0).
- dir = Scroll direction. Possible values:
"up"
,"down"
.
Send a signal to the process.
Kill the process.
Resize the terminal of the process.
- height - height in rows.
- width - width in columns.
Wait until the process exits.
Wait until the terminal contains provided text. The terminal is checked every 50 milliseconds. When timeout expires, virterm exits with non-zero exit code.
- opts
- timeout - Optional. Timeout in milliseconds. Default:
1000
.
- timeout - Optional. Timeout in milliseconds. Default:
Output terminal content as a text file.
Renders and outputs terminal screen as a png file.