2014-12-08 18:31:45 -07:00
|
|
|
-- This module contains the Screen class, a complete Nvim screen implementation
|
|
|
|
-- designed for functional testing. The goal is to provide a simple and
|
|
|
|
-- intuitive API for verifying screen state after a set of actions.
|
|
|
|
--
|
|
|
|
-- The screen class exposes a single assertion method, "Screen:expect". This
|
|
|
|
-- method takes a string representing the expected screen state and an optional
|
|
|
|
-- set of attribute identifiers for checking highlighted characters(more on
|
|
|
|
-- this later).
|
|
|
|
--
|
|
|
|
-- The string passed to "expect" will be processed according to these rules:
|
|
|
|
--
|
|
|
|
-- - Each line of the string represents and is matched individually against
|
|
|
|
-- a screen row.
|
|
|
|
-- - The entire string is stripped of common indentation
|
|
|
|
-- - Expected screen rows are stripped of the last character. The last
|
|
|
|
-- character should be used to write pipes(|) that make clear where the
|
|
|
|
-- screen ends
|
|
|
|
-- - The last line is stripped, so the string must have (row count + 1)
|
|
|
|
-- lines.
|
|
|
|
--
|
|
|
|
-- Example usage:
|
|
|
|
--
|
|
|
|
-- local screen = Screen.new(25, 10)
|
|
|
|
-- -- attach the screen to the current Nvim instance
|
|
|
|
-- screen:attach()
|
|
|
|
-- --enter insert mode and type some text
|
|
|
|
-- feed('ihello screen')
|
|
|
|
-- -- declare an expectation for the eventual screen state
|
|
|
|
-- screen:expect([[
|
|
|
|
-- hello screen |
|
|
|
|
-- ~ |
|
|
|
|
-- ~ |
|
|
|
|
-- ~ |
|
|
|
|
-- ~ |
|
|
|
|
-- ~ |
|
|
|
|
-- ~ |
|
|
|
|
-- ~ |
|
|
|
|
-- ~ |
|
|
|
|
-- -- INSERT -- |
|
|
|
|
-- ]]) -- <- Last line is stripped
|
|
|
|
--
|
|
|
|
-- Since screen updates are received asynchronously, "expect" is actually
|
|
|
|
-- specifying the eventual screen state. This is how "expect" works: It will
|
|
|
|
-- start the event loop with a timeout of 5 seconds. Each time it receives an
|
|
|
|
-- update the expected state will be checked against the updated state.
|
|
|
|
--
|
|
|
|
-- If the expected state matches the current state, the event loop will be
|
|
|
|
-- stopped and "expect" will return. If the timeout expires, the last match
|
|
|
|
-- error will be reported and the test will fail.
|
|
|
|
--
|
|
|
|
-- If the second argument is passed to "expect", the screen rows will be
|
|
|
|
-- transformed before being matched against the string lines. The
|
|
|
|
-- transformation rule is simple: Each substring "S" composed with characters
|
|
|
|
-- having the exact same set of attributes will be substituted by "{K:S}",
|
|
|
|
-- where K is a key associated the attribute set via the second argument of
|
|
|
|
-- "expect".
|
2015-01-24 07:07:02 -07:00
|
|
|
-- If a transformation table is present, unexpected attribute sets in the final
|
|
|
|
-- state is considered an error. To make testing simpler, a list of attribute
|
|
|
|
-- sets that should be ignored can be passed as a third argument. Alternatively,
|
|
|
|
-- this third argument can be "true" to indicate that all unexpected attribute
|
|
|
|
-- sets should be ignored.
|
2014-12-08 18:31:45 -07:00
|
|
|
--
|
2015-01-24 07:07:02 -07:00
|
|
|
-- To illustrate how this works, let's say that in the above example we wanted
|
2014-12-08 18:31:45 -07:00
|
|
|
-- to assert that the "-- INSERT --" string is highlighted with the bold
|
|
|
|
-- attribute(which normally is), here's how the call to "expect" should look
|
|
|
|
-- like:
|
|
|
|
--
|
2015-01-26 11:32:20 -07:00
|
|
|
-- NonText = Screen.colors.Blue
|
2014-12-08 18:31:45 -07:00
|
|
|
-- screen:expect([[
|
2015-05-19 07:11:32 -07:00
|
|
|
-- hello screen |
|
|
|
|
-- ~ |
|
|
|
|
-- ~ |
|
|
|
|
-- ~ |
|
|
|
|
-- ~ |
|
|
|
|
-- ~ |
|
|
|
|
-- ~ |
|
|
|
|
-- ~ |
|
|
|
|
-- ~ |
|
|
|
|
-- {b:-- INSERT --} |
|
2015-01-24 07:07:02 -07:00
|
|
|
-- ]], {b = {bold = true}}, {{bold = true, foreground = NonText}})
|
2014-12-08 18:31:45 -07:00
|
|
|
--
|
|
|
|
-- In this case "b" is a string associated with the set composed of one
|
|
|
|
-- attribute: bold. Note that since the {b:} markup is not a real part of the
|
2015-01-24 07:07:02 -07:00
|
|
|
-- screen, the delimiter(|) had to be moved right. Also, the highlighting of the
|
|
|
|
-- NonText markers (~) is ignored in this test.
|
|
|
|
--
|
|
|
|
-- Multiple expect:s will likely share a group of attribute sets to test.
|
|
|
|
-- Therefore these could be specified at the beginning of a test like this:
|
2015-01-26 11:32:20 -07:00
|
|
|
-- NonText = Screen.colors.Blue
|
2015-01-24 07:07:02 -07:00
|
|
|
-- screen:set_default_attr_ids( {
|
|
|
|
-- [1] = {reverse = true, bold = true},
|
|
|
|
-- [2] = {reverse = true}
|
|
|
|
-- })
|
|
|
|
-- screen:set_default_attr_ignore( {{}, {bold=true, foreground=NonText}} )
|
|
|
|
-- These can be overridden for a specific expect expression, by passing
|
|
|
|
-- different sets as parameters.
|
|
|
|
--
|
|
|
|
-- To help writing screen tests, there is a utility function
|
|
|
|
-- "screen:snapshot_util()", that can be placed in a test file at any point an
|
|
|
|
-- "expect(...)" should be. It will wait a short amount of time and then dump
|
|
|
|
-- the current state of the screen, in the form of an "expect(..)" expression
|
|
|
|
-- that would match it exactly. "snapshot_util" optionally also take the
|
|
|
|
-- transformation and ignore set as parameters, like expect, or uses the default
|
|
|
|
-- set. It will generate a larger attribute transformation set, if needed.
|
|
|
|
-- To generate a text-only test without highlight checks,
|
|
|
|
-- use `screen:snapshot_util({},true)`
|
|
|
|
|
2016-04-23 16:53:11 -07:00
|
|
|
local helpers = require('test.functional.helpers')(nil)
|
2016-06-08 02:26:06 -07:00
|
|
|
local request, run, uimeths = helpers.request, helpers.run, helpers.uimeths
|
2015-11-17 14:44:00 -07:00
|
|
|
local dedent = helpers.dedent
|
2014-12-08 18:31:45 -07:00
|
|
|
|
|
|
|
local Screen = {}
|
|
|
|
Screen.__index = Screen
|
|
|
|
|
2015-01-13 14:05:57 -07:00
|
|
|
local debug_screen
|
|
|
|
|
2015-02-16 19:47:56 -07:00
|
|
|
local default_screen_timeout = 3500
|
2015-01-23 15:00:45 -07:00
|
|
|
if os.getenv('VALGRIND') then
|
2015-02-13 06:28:02 -07:00
|
|
|
default_screen_timeout = default_screen_timeout * 3
|
|
|
|
end
|
|
|
|
|
2016-01-01 21:58:42 -07:00
|
|
|
if os.getenv('CI') then
|
2015-02-13 06:28:02 -07:00
|
|
|
default_screen_timeout = default_screen_timeout * 3
|
2015-01-23 15:00:45 -07:00
|
|
|
end
|
2015-01-13 14:05:57 -07:00
|
|
|
|
2015-03-17 04:45:13 -07:00
|
|
|
do
|
|
|
|
local spawn, nvim_prog = helpers.spawn, helpers.nvim_prog
|
2015-10-23 05:09:07 -07:00
|
|
|
local session = spawn({nvim_prog, '-u', 'NONE', '-i', 'NONE', '-N', '--embed'})
|
2015-03-17 04:45:13 -07:00
|
|
|
local status, rv = session:request('vim_get_color_map')
|
|
|
|
if not status then
|
|
|
|
print('failed to get color map')
|
|
|
|
os.exit(1)
|
|
|
|
end
|
|
|
|
local colors = rv
|
|
|
|
local colornames = {}
|
|
|
|
for name, rgb in pairs(colors) do
|
2015-01-26 11:32:20 -07:00
|
|
|
-- we disregard the case that colornames might not be unique, as
|
|
|
|
-- this is just a helper to get any canonical name of a color
|
|
|
|
colornames[rgb] = name
|
2015-03-17 04:45:13 -07:00
|
|
|
end
|
2016-04-13 05:21:32 -07:00
|
|
|
session:close()
|
2015-03-17 04:45:13 -07:00
|
|
|
Screen.colors = colors
|
|
|
|
Screen.colornames = colornames
|
2015-01-26 11:32:20 -07:00
|
|
|
end
|
|
|
|
|
2015-01-13 14:05:57 -07:00
|
|
|
function Screen.debug(command)
|
|
|
|
if not command then
|
2015-02-20 16:38:42 -07:00
|
|
|
command = 'pynvim -n -c '
|
2015-01-13 14:05:57 -07:00
|
|
|
end
|
|
|
|
command = command .. request('vim_eval', '$NVIM_LISTEN_ADDRESS')
|
|
|
|
if debug_screen then
|
|
|
|
debug_screen:close()
|
|
|
|
end
|
|
|
|
debug_screen = io.popen(command, 'r')
|
|
|
|
debug_screen:read()
|
|
|
|
end
|
|
|
|
|
2014-12-08 18:31:45 -07:00
|
|
|
function Screen.new(width, height)
|
|
|
|
if not width then
|
|
|
|
width = 53
|
|
|
|
end
|
|
|
|
if not height then
|
|
|
|
height = 14
|
|
|
|
end
|
2015-01-15 05:01:25 -07:00
|
|
|
local self = setmetatable({
|
2015-10-01 11:03:40 -07:00
|
|
|
timeout = default_screen_timeout,
|
2015-01-15 05:01:25 -07:00
|
|
|
title = '',
|
|
|
|
icon = '',
|
|
|
|
bell = false,
|
2015-07-10 16:03:30 -07:00
|
|
|
update_menu = false,
|
2015-01-15 05:01:25 -07:00
|
|
|
visual_bell = false,
|
|
|
|
suspended = false,
|
2016-11-25 03:33:57 -07:00
|
|
|
mode = 'normal',
|
2014-12-08 18:31:45 -07:00
|
|
|
_default_attr_ids = nil,
|
2015-01-17 06:59:47 -07:00
|
|
|
_default_attr_ignore = nil,
|
2014-12-08 18:31:45 -07:00
|
|
|
_mouse_enabled = true,
|
|
|
|
_attrs = {},
|
|
|
|
_cursor = {
|
2015-03-15 06:21:05 -07:00
|
|
|
row = 1, col = 1
|
|
|
|
},
|
2015-03-18 08:34:36 -07:00
|
|
|
_busy = false
|
2014-12-08 18:31:45 -07:00
|
|
|
}, Screen)
|
2015-01-15 05:01:25 -07:00
|
|
|
self:_handle_resize(width, height)
|
|
|
|
return self
|
2014-12-08 18:31:45 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function Screen:set_default_attr_ids(attr_ids)
|
|
|
|
self._default_attr_ids = attr_ids
|
|
|
|
end
|
|
|
|
|
2015-01-17 06:59:47 -07:00
|
|
|
function Screen:set_default_attr_ignore(attr_ignore)
|
|
|
|
self._default_attr_ignore = attr_ignore
|
|
|
|
end
|
|
|
|
|
2016-06-08 02:26:06 -07:00
|
|
|
function Screen:attach(options)
|
|
|
|
if options == nil then
|
|
|
|
options = {rgb=true}
|
2015-03-25 05:14:47 -07:00
|
|
|
end
|
2016-06-08 02:26:06 -07:00
|
|
|
uimeths.attach(self._width, self._height, options)
|
2014-12-08 18:31:45 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function Screen:detach()
|
2016-06-08 02:26:06 -07:00
|
|
|
uimeths.detach()
|
2015-01-15 05:01:25 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function Screen:try_resize(columns, rows)
|
2016-06-08 02:26:06 -07:00
|
|
|
uimeths.try_resize(columns, rows)
|
2014-12-08 18:31:45 -07:00
|
|
|
end
|
|
|
|
|
2016-06-08 02:26:06 -07:00
|
|
|
function Screen:expect(expected, attr_ids, attr_ignore, condition)
|
2014-12-08 18:31:45 -07:00
|
|
|
-- remove the last line and dedent
|
|
|
|
expected = dedent(expected:gsub('\n[ ]+$', ''))
|
|
|
|
local expected_rows = {}
|
|
|
|
for row in expected:gmatch('[^\n]+') do
|
|
|
|
-- the last character should be the screen delimiter
|
|
|
|
row = row:sub(1, #row - 1)
|
|
|
|
table.insert(expected_rows, row)
|
|
|
|
end
|
|
|
|
local ids = attr_ids or self._default_attr_ids
|
2015-01-23 15:53:21 -07:00
|
|
|
local ignore = attr_ignore or self._default_attr_ignore
|
2015-01-15 05:01:25 -07:00
|
|
|
self:wait(function()
|
2016-06-08 02:26:06 -07:00
|
|
|
if condition ~= nil then
|
|
|
|
local status, res = pcall(condition)
|
|
|
|
if not status then
|
|
|
|
return tostring(res)
|
|
|
|
end
|
|
|
|
end
|
2016-02-27 22:15:52 -07:00
|
|
|
local actual_rows = {}
|
2014-12-08 18:31:45 -07:00
|
|
|
for i = 1, self._height do
|
2016-02-27 22:15:52 -07:00
|
|
|
actual_rows[i] = self:_row_repr(self._rows[i], ids, ignore)
|
|
|
|
end
|
|
|
|
for i = 1, self._height do
|
|
|
|
if expected_rows[i] ~= actual_rows[i] then
|
|
|
|
local msg_expected_rows = {}
|
2016-02-29 05:48:59 -07:00
|
|
|
for j = 1, #expected_rows do
|
|
|
|
msg_expected_rows[j] = expected_rows[j]
|
|
|
|
end
|
2016-02-27 22:15:52 -07:00
|
|
|
msg_expected_rows[i] = '*' .. msg_expected_rows[i]
|
|
|
|
actual_rows[i] = '*' .. actual_rows[i]
|
2016-02-29 05:48:59 -07:00
|
|
|
return (
|
2016-02-27 22:15:52 -07:00
|
|
|
'Row ' .. tostring(i) .. ' didn\'t match.\n'
|
|
|
|
.. 'Expected:\n|' .. table.concat(msg_expected_rows, '|\n|') .. '|\n'
|
2016-08-13 06:51:53 -07:00
|
|
|
.. 'Actual:\n|' .. table.concat(actual_rows, '|\n|') .. '|\n\n' .. [[
|
|
|
|
To print the expect() call that would assert the current screen state, use
|
|
|
|
screen:snaphot_util(). In case of non-deterministic failures, use
|
|
|
|
screen:redraw_debug() to show all intermediate screen states. ]])
|
2014-12-08 18:31:45 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2015-01-15 05:01:25 -07:00
|
|
|
function Screen:wait(check, timeout)
|
2014-12-09 10:12:12 -07:00
|
|
|
local err, checked = false
|
2015-03-22 07:23:53 -07:00
|
|
|
local success_seen = false
|
|
|
|
local failure_after_success = false
|
2014-12-08 18:31:45 -07:00
|
|
|
local function notification_cb(method, args)
|
|
|
|
assert(method == 'redraw')
|
|
|
|
self:_redraw(args)
|
|
|
|
err = check()
|
2014-12-09 10:12:12 -07:00
|
|
|
checked = true
|
2014-12-08 18:31:45 -07:00
|
|
|
if not err then
|
2015-03-22 07:23:53 -07:00
|
|
|
success_seen = true
|
2015-11-17 15:31:22 -07:00
|
|
|
helpers.stop()
|
2015-03-22 07:23:53 -07:00
|
|
|
elseif success_seen and #args > 0 then
|
|
|
|
failure_after_success = true
|
|
|
|
--print(require('inspect')(args))
|
2014-12-08 18:31:45 -07:00
|
|
|
end
|
2015-03-22 07:23:53 -07:00
|
|
|
|
2014-12-08 18:31:45 -07:00
|
|
|
return true
|
|
|
|
end
|
2015-10-01 11:03:40 -07:00
|
|
|
run(nil, notification_cb, nil, timeout or self.timeout)
|
2014-12-09 10:12:12 -07:00
|
|
|
if not checked then
|
|
|
|
err = check()
|
|
|
|
end
|
2015-03-22 07:23:53 -07:00
|
|
|
|
|
|
|
if failure_after_success then
|
|
|
|
print([[
|
|
|
|
Warning: Screen changes have been received after the expected state was seen.
|
|
|
|
This is probably due to an indeterminism in the test. Try adding
|
|
|
|
`wait()` (or even a separate `screen:expect(...)`) at a point of possible
|
|
|
|
indeterminism, typically in between a `feed()` or `execute()` which is non-
|
|
|
|
synchronous, and a synchronous api call.
|
2015-07-07 18:58:32 -07:00
|
|
|
|
|
|
|
Note that sometimes a `wait` can trigger redraws and consequently generate more
|
|
|
|
indeterminism. If adding `wait` calls seems to increase the frequency of these
|
|
|
|
messages, try removing every `wait` call in the test.
|
|
|
|
|
|
|
|
If everything else fails, use Screen:redraw_debug to help investigate what is
|
|
|
|
causing the problem.
|
2015-03-22 07:23:53 -07:00
|
|
|
]])
|
|
|
|
local tb = debug.traceback()
|
|
|
|
local index = string.find(tb, '\n%s*%[C]')
|
|
|
|
print(string.sub(tb,1,index))
|
|
|
|
end
|
|
|
|
|
2014-12-08 18:31:45 -07:00
|
|
|
if err then
|
2015-01-23 14:06:52 -07:00
|
|
|
assert(false, err)
|
2014-12-08 18:31:45 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-22 12:53:43 -07:00
|
|
|
function Screen:sleep(ms)
|
|
|
|
pcall(function() self:wait(function() return "error" end, ms) end)
|
|
|
|
end
|
|
|
|
|
2014-12-08 18:31:45 -07:00
|
|
|
function Screen:_redraw(updates)
|
|
|
|
for _, update in ipairs(updates) do
|
|
|
|
-- print('--')
|
|
|
|
-- print(require('inspect')(update))
|
|
|
|
local method = update[1]
|
|
|
|
for i = 2, #update do
|
|
|
|
local handler = self['_handle_'..method]
|
2016-06-08 02:26:06 -07:00
|
|
|
if handler ~= nil then
|
|
|
|
handler(self, unpack(update[i]))
|
|
|
|
else
|
|
|
|
self._on_event(method, update[i])
|
|
|
|
end
|
2014-12-08 18:31:45 -07:00
|
|
|
end
|
|
|
|
-- print(self:_current_screen())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-08 02:26:06 -07:00
|
|
|
function Screen:set_on_event_handler(callback)
|
|
|
|
self._on_event = callback
|
|
|
|
end
|
|
|
|
|
2014-12-08 18:31:45 -07:00
|
|
|
function Screen:_handle_resize(width, height)
|
2015-01-15 05:01:25 -07:00
|
|
|
local rows = {}
|
2015-11-17 14:44:00 -07:00
|
|
|
for _ = 1, height do
|
2015-01-15 05:01:25 -07:00
|
|
|
local cols = {}
|
2015-11-17 14:44:00 -07:00
|
|
|
for _ = 1, width do
|
2015-01-15 05:01:25 -07:00
|
|
|
table.insert(cols, {text = ' ', attrs = {}})
|
|
|
|
end
|
|
|
|
table.insert(rows, cols)
|
|
|
|
end
|
2015-02-16 19:47:56 -07:00
|
|
|
self._cursor.row = 1
|
|
|
|
self._cursor.col = 1
|
2015-01-15 05:01:25 -07:00
|
|
|
self._rows = rows
|
|
|
|
self._width = width
|
|
|
|
self._height = height
|
|
|
|
self._scroll_region = {
|
|
|
|
top = 1, bot = height, left = 1, right = width
|
|
|
|
}
|
2014-12-08 18:31:45 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function Screen:_handle_clear()
|
2014-12-12 12:05:52 -07:00
|
|
|
self:_clear_block(self._scroll_region.top, self._scroll_region.bot,
|
|
|
|
self._scroll_region.left, self._scroll_region.right)
|
2014-12-08 18:31:45 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function Screen:_handle_eol_clear()
|
|
|
|
local row, col = self._cursor.row, self._cursor.col
|
2015-01-16 13:22:32 -07:00
|
|
|
self:_clear_block(row, row, col, self._scroll_region.right)
|
2014-12-08 18:31:45 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function Screen:_handle_cursor_goto(row, col)
|
|
|
|
self._cursor.row = row + 1
|
|
|
|
self._cursor.col = col + 1
|
|
|
|
end
|
|
|
|
|
2015-03-15 06:21:05 -07:00
|
|
|
function Screen:_handle_busy_start()
|
|
|
|
self._busy = true
|
2014-12-08 18:31:45 -07:00
|
|
|
end
|
|
|
|
|
2015-03-15 06:21:05 -07:00
|
|
|
function Screen:_handle_busy_stop()
|
|
|
|
self._busy = false
|
2014-12-08 18:31:45 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function Screen:_handle_mouse_on()
|
|
|
|
self._mouse_enabled = true
|
|
|
|
end
|
|
|
|
|
|
|
|
function Screen:_handle_mouse_off()
|
|
|
|
self._mouse_enabled = false
|
|
|
|
end
|
|
|
|
|
2015-05-17 01:22:46 -07:00
|
|
|
function Screen:_handle_mode_change(mode)
|
2016-11-25 16:39:33 -07:00
|
|
|
assert(mode == 'insert' or mode == 'replace'
|
|
|
|
or mode == 'normal' or mode == 'cmdline')
|
2016-11-25 03:33:57 -07:00
|
|
|
self.mode = mode
|
2014-12-08 18:31:45 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function Screen:_handle_set_scroll_region(top, bot, left, right)
|
|
|
|
self._scroll_region.top = top + 1
|
|
|
|
self._scroll_region.bot = bot + 1
|
|
|
|
self._scroll_region.left = left + 1
|
|
|
|
self._scroll_region.right = right + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
function Screen:_handle_scroll(count)
|
|
|
|
local top = self._scroll_region.top
|
|
|
|
local bot = self._scroll_region.bot
|
|
|
|
local left = self._scroll_region.left
|
|
|
|
local right = self._scroll_region.right
|
|
|
|
local start, stop, step
|
|
|
|
|
|
|
|
if count > 0 then
|
|
|
|
start = top
|
|
|
|
stop = bot - count
|
|
|
|
step = 1
|
|
|
|
else
|
|
|
|
start = bot
|
|
|
|
stop = top - count
|
|
|
|
step = -1
|
|
|
|
end
|
|
|
|
|
|
|
|
-- shift scroll region
|
|
|
|
for i = start, stop, step do
|
|
|
|
local target = self._rows[i]
|
|
|
|
local source = self._rows[i + count]
|
2015-01-15 05:01:25 -07:00
|
|
|
for j = left, right do
|
|
|
|
target[j].text = source[j].text
|
|
|
|
target[j].attrs = source[j].attrs
|
|
|
|
end
|
2014-12-08 18:31:45 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
-- clear invalid rows
|
2015-01-15 05:01:25 -07:00
|
|
|
for i = stop + step, stop + count, step do
|
2014-12-08 18:31:45 -07:00
|
|
|
self:_clear_row_section(i, left, right)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Screen:_handle_highlight_set(attrs)
|
|
|
|
self._attrs = attrs
|
|
|
|
end
|
|
|
|
|
|
|
|
function Screen:_handle_put(str)
|
|
|
|
local cell = self._rows[self._cursor.row][self._cursor.col]
|
|
|
|
cell.text = str
|
|
|
|
cell.attrs = self._attrs
|
|
|
|
self._cursor.col = self._cursor.col + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
function Screen:_handle_bell()
|
2015-01-15 05:01:25 -07:00
|
|
|
self.bell = true
|
2014-12-08 18:31:45 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function Screen:_handle_visual_bell()
|
2015-01-15 05:01:25 -07:00
|
|
|
self.visual_bell = true
|
2014-12-08 18:31:45 -07:00
|
|
|
end
|
|
|
|
|
2014-12-12 12:25:11 -07:00
|
|
|
function Screen:_handle_update_fg(fg)
|
|
|
|
self._fg = fg
|
|
|
|
end
|
|
|
|
|
|
|
|
function Screen:_handle_update_bg(bg)
|
|
|
|
self._bg = bg
|
|
|
|
end
|
|
|
|
|
2016-04-23 15:01:15 -07:00
|
|
|
function Screen:_handle_update_sp(sp)
|
|
|
|
self._sp = sp
|
|
|
|
end
|
|
|
|
|
2015-01-12 08:14:52 -07:00
|
|
|
function Screen:_handle_suspend()
|
2015-01-15 05:01:25 -07:00
|
|
|
self.suspended = true
|
2014-12-12 12:25:11 -07:00
|
|
|
end
|
|
|
|
|
2015-07-10 16:03:30 -07:00
|
|
|
function Screen:_handle_update_menu()
|
|
|
|
self.update_menu = true
|
|
|
|
end
|
|
|
|
|
2015-01-13 17:19:59 -07:00
|
|
|
function Screen:_handle_set_title(title)
|
2015-01-15 05:01:25 -07:00
|
|
|
self.title = title
|
2015-01-13 17:19:59 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function Screen:_handle_set_icon(icon)
|
2015-01-15 05:01:25 -07:00
|
|
|
self.icon = icon
|
2015-01-13 17:19:59 -07:00
|
|
|
end
|
|
|
|
|
2015-01-16 13:22:32 -07:00
|
|
|
function Screen:_clear_block(top, bot, left, right)
|
|
|
|
for i = top, bot do
|
|
|
|
self:_clear_row_section(i, left, right)
|
2014-12-08 18:31:45 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Screen:_clear_row_section(rownum, startcol, stopcol)
|
|
|
|
local row = self._rows[rownum]
|
|
|
|
for i = startcol, stopcol do
|
|
|
|
row[i].text = ' '
|
|
|
|
row[i].attrs = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-17 06:59:47 -07:00
|
|
|
function Screen:_row_repr(row, attr_ids, attr_ignore)
|
2014-12-08 18:31:45 -07:00
|
|
|
local rv = {}
|
|
|
|
local current_attr_id
|
|
|
|
for i = 1, self._width do
|
2015-11-17 15:31:22 -07:00
|
|
|
local attr_id = self:_get_attr_id(attr_ids, attr_ignore, row[i].attrs)
|
2014-12-08 18:31:45 -07:00
|
|
|
if current_attr_id and attr_id ~= current_attr_id then
|
|
|
|
-- close current attribute bracket, add it before any whitespace
|
|
|
|
-- up to the current cell
|
|
|
|
-- table.insert(rv, backward_find_meaningful(rv, i), '}')
|
|
|
|
table.insert(rv, '}')
|
|
|
|
current_attr_id = nil
|
|
|
|
end
|
|
|
|
if not current_attr_id and attr_id then
|
|
|
|
-- open a new attribute bracket
|
|
|
|
table.insert(rv, '{' .. attr_id .. ':')
|
|
|
|
current_attr_id = attr_id
|
|
|
|
end
|
2015-03-16 04:29:57 -07:00
|
|
|
if not self._busy and self._rows[self._cursor.row] == row and self._cursor.col == i then
|
2014-12-08 18:31:45 -07:00
|
|
|
table.insert(rv, '^')
|
|
|
|
end
|
2015-03-05 02:07:55 -07:00
|
|
|
table.insert(rv, row[i].text)
|
2014-12-08 18:31:45 -07:00
|
|
|
end
|
|
|
|
if current_attr_id then
|
|
|
|
table.insert(rv, '}')
|
|
|
|
end
|
|
|
|
-- return the line representation, but remove empty attribute brackets and
|
|
|
|
-- trailing whitespace
|
|
|
|
return table.concat(rv, '')--:gsub('%s+$', '')
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function Screen:_current_screen()
|
|
|
|
-- get a string that represents the current screen state(debugging helper)
|
|
|
|
local rv = {}
|
|
|
|
for i = 1, self._height do
|
|
|
|
table.insert(rv, "'"..self:_row_repr(self._rows[i]).."'")
|
|
|
|
end
|
|
|
|
return table.concat(rv, '\n')
|
|
|
|
end
|
|
|
|
|
2015-01-17 06:59:47 -07:00
|
|
|
function Screen:snapshot_util(attrs, ignore)
|
2015-01-17 04:46:29 -07:00
|
|
|
-- util to generate screen test
|
2016-05-22 12:53:43 -07:00
|
|
|
self:sleep(250)
|
2015-03-22 06:18:35 -07:00
|
|
|
self:print_snapshot(attrs, ignore)
|
|
|
|
end
|
|
|
|
|
2015-07-27 04:39:38 -07:00
|
|
|
function Screen:redraw_debug(attrs, ignore, timeout)
|
2015-03-22 06:18:35 -07:00
|
|
|
self:print_snapshot(attrs, ignore)
|
|
|
|
local function notification_cb(method, args)
|
|
|
|
assert(method == 'redraw')
|
|
|
|
for _, update in ipairs(args) do
|
|
|
|
print(require('inspect')(update))
|
|
|
|
end
|
|
|
|
self:_redraw(args)
|
|
|
|
self:print_snapshot(attrs, ignore)
|
|
|
|
return true
|
|
|
|
end
|
2015-07-27 04:39:38 -07:00
|
|
|
if timeout == nil then
|
|
|
|
timeout = 250
|
|
|
|
end
|
|
|
|
run(nil, notification_cb, nil, timeout)
|
2015-03-22 06:18:35 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
function Screen:print_snapshot(attrs, ignore)
|
2015-01-17 06:59:47 -07:00
|
|
|
if ignore == nil then
|
|
|
|
ignore = self._default_attr_ignore
|
|
|
|
end
|
2015-01-17 04:46:29 -07:00
|
|
|
if attrs == nil then
|
2015-01-17 06:59:47 -07:00
|
|
|
attrs = {}
|
|
|
|
if self._default_attr_ids ~= nil then
|
2016-11-25 03:33:57 -07:00
|
|
|
for i, a in pairs(self._default_attr_ids) do
|
2015-01-17 06:59:47 -07:00
|
|
|
attrs[i] = a
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-23 15:53:21 -07:00
|
|
|
if ignore ~= true then
|
|
|
|
for i = 1, self._height do
|
|
|
|
local row = self._rows[i]
|
|
|
|
for j = 1, self._width do
|
|
|
|
local attr = row[j].attrs
|
2015-11-17 15:31:22 -07:00
|
|
|
if self:_attr_index(attrs, attr) == nil and self:_attr_index(ignore, attr) == nil then
|
|
|
|
if not self:_equal_attrs(attr, {}) then
|
2015-01-23 15:53:21 -07:00
|
|
|
table.insert(attrs, attr)
|
|
|
|
end
|
|
|
|
end
|
2015-01-17 04:46:29 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local rv = {}
|
|
|
|
for i = 1, self._height do
|
2015-01-17 06:59:47 -07:00
|
|
|
table.insert(rv, " "..self:_row_repr(self._rows[i],attrs, ignore).."|")
|
2015-01-17 04:46:29 -07:00
|
|
|
end
|
|
|
|
local attrstrs = {}
|
2015-01-17 06:59:47 -07:00
|
|
|
local alldefault = true
|
2015-01-17 04:46:29 -07:00
|
|
|
for i, a in ipairs(attrs) do
|
2015-01-17 06:59:47 -07:00
|
|
|
if self._default_attr_ids == nil or self._default_attr_ids[i] ~= a then
|
|
|
|
alldefault = false
|
|
|
|
end
|
2015-11-17 15:31:22 -07:00
|
|
|
local dict = "{"..self:_pprint_attrs(a).."}"
|
2015-01-17 04:46:29 -07:00
|
|
|
table.insert(attrstrs, "["..tostring(i).."] = "..dict)
|
|
|
|
end
|
|
|
|
local attrstr = "{"..table.concat(attrstrs, ", ").."}"
|
|
|
|
print( "\nscreen:expect([[")
|
|
|
|
print( table.concat(rv, '\n'))
|
2015-01-17 06:59:47 -07:00
|
|
|
if alldefault then
|
|
|
|
print( "]])\n")
|
|
|
|
else
|
|
|
|
print( "]], "..attrstr..")\n")
|
|
|
|
end
|
2015-03-25 05:14:47 -07:00
|
|
|
io.stdout:flush()
|
2015-01-17 04:46:29 -07:00
|
|
|
end
|
|
|
|
|
2015-11-17 15:31:22 -07:00
|
|
|
function Screen:_pprint_attrs(attrs)
|
2015-01-23 15:52:48 -07:00
|
|
|
local items = {}
|
|
|
|
for f, v in pairs(attrs) do
|
2015-01-26 11:32:20 -07:00
|
|
|
local desc = tostring(v)
|
2016-04-24 02:46:48 -07:00
|
|
|
if f == "foreground" or f == "background" or f == "special" then
|
2015-03-17 04:45:13 -07:00
|
|
|
if Screen.colornames[v] ~= nil then
|
|
|
|
desc = "Screen.colors."..Screen.colornames[v]
|
2015-01-26 11:32:20 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
table.insert(items, f.." = "..desc)
|
2015-01-23 15:52:48 -07:00
|
|
|
end
|
|
|
|
return table.concat(items, ", ")
|
|
|
|
end
|
2015-01-17 04:46:29 -07:00
|
|
|
|
2015-11-17 15:31:22 -07:00
|
|
|
function backward_find_meaningful(tbl, from) -- luacheck: ignore
|
2014-12-08 18:31:45 -07:00
|
|
|
for i = from or #tbl, 1, -1 do
|
|
|
|
if tbl[i] ~= ' ' then
|
|
|
|
return i + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return from
|
|
|
|
end
|
|
|
|
|
2015-11-17 15:31:22 -07:00
|
|
|
function Screen:_get_attr_id(attr_ids, ignore, attrs)
|
2014-12-08 18:31:45 -07:00
|
|
|
if not attr_ids then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
for id, a in pairs(attr_ids) do
|
2015-11-17 15:31:22 -07:00
|
|
|
if self:_equal_attrs(a, attrs) then
|
2014-12-08 18:31:45 -07:00
|
|
|
return id
|
|
|
|
end
|
|
|
|
end
|
2015-11-17 15:31:22 -07:00
|
|
|
if self:_equal_attrs(attrs, {}) or
|
|
|
|
ignore == true or self:_attr_index(ignore, attrs) ~= nil then
|
2015-01-17 06:59:47 -07:00
|
|
|
-- ignore this attrs
|
|
|
|
return nil
|
|
|
|
end
|
2015-11-17 15:31:22 -07:00
|
|
|
return "UNEXPECTED "..self:_pprint_attrs(attrs)
|
2014-12-08 18:31:45 -07:00
|
|
|
end
|
|
|
|
|
2015-11-17 15:31:22 -07:00
|
|
|
function Screen:_equal_attrs(a, b)
|
2015-01-17 04:46:29 -07:00
|
|
|
return a.bold == b.bold and a.standout == b.standout and
|
|
|
|
a.underline == b.underline and a.undercurl == b.undercurl and
|
|
|
|
a.italic == b.italic and a.reverse == b.reverse and
|
|
|
|
a.foreground == b.foreground and
|
2016-05-17 13:25:11 -07:00
|
|
|
a.background == b.background and
|
|
|
|
a.special == b.special
|
2015-01-17 04:46:29 -07:00
|
|
|
end
|
|
|
|
|
2015-11-17 15:31:22 -07:00
|
|
|
function Screen:_attr_index(attrs, attr)
|
2015-01-17 06:59:47 -07:00
|
|
|
if not attrs then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
for i,a in pairs(attrs) do
|
2015-11-17 15:31:22 -07:00
|
|
|
if self:_equal_attrs(a, attr) then
|
2015-01-17 06:59:47 -07:00
|
|
|
return i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2014-12-08 18:31:45 -07:00
|
|
|
return Screen
|