win/pty: log errors

This commit is contained in:
erw7
2017-04-02 18:32:23 +09:00
committed by Justin M. Keyes
parent 84fb794da6
commit d3a8c4f992
6 changed files with 150 additions and 81 deletions

View File

@@ -4,15 +4,34 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <uv.h>
#ifdef _WIN32
#include <windows.h>
#endif
// -V:STRUCT_CAST:641
#define STRUCT_CAST(Type, obj) ((Type *)(obj))
uv_tty_t tty;
#define is_terminal(stream) (uv_guess_handle(fileno(stream)) == UV_TTY)
#define BUF_SIZE 0xfff
#define CTRL_C 0x03
#ifdef _WIN32
#define CTRL_Q 0x11
#endif
#ifdef _WIN32
typedef struct screen_size {
int width;
int height;
} ScreenSize;
#endif
uv_tty_t tty;
#ifdef _WIN32
ScreenSize screen_rect;
#endif
#ifdef _WIN32
#include <windows.h>
bool owns_tty(void)
{
// XXX: We need to make proper detect owns tty
@@ -30,10 +49,8 @@ bool owns_tty(void)
}
#endif
#define is_terminal(stream) (uv_guess_handle(fileno(stream)) == UV_TTY)
#define BUF_SIZE 0xfff
static void walk_cb(uv_handle_t *handle, void *arg) {
static void walk_cb(uv_handle_t *handle, void *arg)
{
if (!uv_is_closing(handle)) {
uv_close(handle, NULL);
}
@@ -42,7 +59,7 @@ static void walk_cb(uv_handle_t *handle, void *arg) {
#ifndef WIN32
static void sig_handler(int signum)
{
switch(signum) {
switch (signum) {
case SIGWINCH: {
int width, height;
uv_tty_get_winsize(&tty, &width, &height);
@@ -82,14 +99,19 @@ static void read_cb(uv_stream_t *stream, ssize_t cnt, const uv_buf_t *buf)
}
int *interrupted = stream->data;
#ifdef _WIN32
bool prsz = false;
int width, height;
int width;
int height;
#endif
for (int i = 0; i < cnt; i++) {
if (buf->base[i] == 3) {
if (buf->base[i] == CTRL_C) {
(*interrupted)++;
} else if (buf->base[i] == 17) {
#ifdef _WIN32
} else if (buf->base[i] == CTRL_Q) {
prsz = true;
#endif
}
}
@@ -98,15 +120,23 @@ static void read_cb(uv_stream_t *stream, ssize_t cnt, const uv_buf_t *buf)
uv_tty_t out;
uv_tty_init(&write_loop, &out, fileno(stdout), 0);
#ifdef _WIN32
if (prsz) {
uv_tty_get_winsize(&out, &width, &height);
fprintf(stderr, "rows: %d, cols: %d\n", height, width);
if (screen_rect.width != width || screen_rect.height != height) {
screen_rect.width = width;
screen_rect.height = height;
fprintf(stderr, "rows: %d, cols: %d\n", height, width);
}
} else {
#endif
uv_write_t req;
uv_buf_t b = {.base = buf->base, .len = (size_t)cnt};
uv_write(&req, STRUCT_CAST(uv_stream_t, &out), &b, 1, NULL);
uv_run(&write_loop, UV_RUN_DEFAULT);
#ifdef _WIN32
}
#endif
uv_close(STRUCT_CAST(uv_handle_t, &out), NULL);
uv_run(&write_loop, UV_RUN_DEFAULT);
@@ -152,7 +182,7 @@ int main(int argc, char **argv)
if (argc > 1) {
int count = atoi(argv[1]);
for (int i = 0; i < count; ++i) {
for (int i = 0; i < count; i++) {
printf("line%d\n", i);
}
fflush(stdout);
@@ -168,6 +198,14 @@ int main(int argc, char **argv)
uv_tty_init(uv_default_loop(), &tty, fileno(stderr), 1);
#else
uv_tty_init(uv_default_loop(), &tty, fileno(stdin), 1);
uv_tty_t out;
uv_tty_init(uv_default_loop(), &out, fileno(stdout), 0);
int width;
int height;
uv_tty_get_winsize(&out, &width, &height);
screen_rect.width = width;
screen_rect.height = height;
uv_close((uv_handle_t *)&out, NULL);
#endif
uv_tty_set_mode(&tty, UV_TTY_MODE_RAW);
tty.data = &interrupted;

View File

@@ -30,10 +30,6 @@ local function clear_attrs() feed_termcode('[0;10m') end
-- mouse
local function enable_mouse() feed_termcode('[?1002h') end
local function disable_mouse() feed_termcode('[?1002l') end
local function print_screen_size()
helpers.sleep(1000)
nvim('command', 'call jobsend(b:terminal_job_id, "\\<C-q>")')
end
local default_command = '["'..nvim_dir..'/tty-test'..'"]'
@@ -115,6 +111,5 @@ return {
clear_attrs = clear_attrs,
enable_mouse = enable_mouse,
disable_mouse = disable_mouse,
print_screen_size = print_screen_size,
screen_setup = screen_setup
}

View File

@@ -3,7 +3,7 @@ local helpers = require('test.functional.helpers')(after_each)
local thelpers = require('test.functional.terminal.helpers')
local clear, eq, curbuf = helpers.clear, helpers.eq, helpers.curbuf
local feed, nvim_dir, feed_command = helpers.feed, helpers.nvim_dir, helpers.feed_command
local iswin, print_screen_size = helpers.iswin, thelpers.print_screen_size
local iswin = helpers.iswin
local eval = helpers.eval
local command = helpers.command
local wait = helpers.wait
@@ -141,10 +141,7 @@ describe('terminal scrollback', function()
describe('and the height is decreased by 1', function()
local function will_hide_top_line()
screen:try_resize(screen._width, screen._height - 1)
if iswin() then
print_screen_size()
end
screen:expect([[
screen:expect_after_resize([[
line2 |
line3 |
line4 |
@@ -160,13 +157,10 @@ describe('terminal scrollback', function()
before_each(function()
will_hide_top_line()
screen:try_resize(screen._width, screen._height - 2)
if iswin() then
print_screen_size()
end
end)
it('will hide the top 3 lines', function()
screen:expect([[
screen:expect_after_resize([[
rows: 5, cols: 30 |
rows: 3, cols: 30 |
{1: } |
@@ -189,9 +183,6 @@ describe('terminal scrollback', function()
describe('and the height is decreased by 2', function()
before_each(function()
screen:try_resize(screen._width, screen._height - 2)
if iswin() then
print_screen_size()
end
end)
local function will_delete_last_two_lines()
@@ -213,9 +204,6 @@ describe('terminal scrollback', function()
before_each(function()
will_delete_last_two_lines()
screen:try_resize(screen._width, screen._height - 1)
if iswin() then
print_screen_size()
end
end)
it('will delete the last line and hide the first', function()
@@ -258,10 +246,7 @@ describe('terminal scrollback', function()
{3:-- TERMINAL --} |
]])
screen:try_resize(screen._width, screen._height - 3)
if iswin() then
print_screen_size()
end
screen:expect([[
screen:expect_after_resize([[
line4 |
rows: 3, cols: 30 |
{1: } |
@@ -273,10 +258,7 @@ describe('terminal scrollback', function()
describe('and the height is increased by 1', function()
local function pop_then_push()
screen:try_resize(screen._width, screen._height + 1)
if iswin() then
print_screen_size()
end
screen:expect([[
screen:expect_after_resize([[
line4 |
rows: 3, cols: 30 |
rows: 4, cols: 30 |
@@ -292,13 +274,10 @@ describe('terminal scrollback', function()
pop_then_push()
eq(8, curbuf('line_count'))
screen:try_resize(screen._width, screen._height + 3)
if iswin() then
print_screen_size()
end
end)
local function pop3_then_push1()
screen:expect([[
screen:expect_after_resize([[
line2 |
line3 |
line4 |
@@ -330,9 +309,6 @@ describe('terminal scrollback', function()
pop3_then_push1()
feed('Gi')
screen:try_resize(screen._width, screen._height + 4)
if iswin() then
print_screen_size()
end
end)
it('will show all lines and leave a blank one at the end', function()

View File

@@ -73,6 +73,8 @@
local helpers = require('test.functional.helpers')(nil)
local request, run, uimeths = helpers.request, helpers.run, helpers.uimeths
local iswin, nvim, retry = helpers.iswin, helpers.nvim, helpers.retry
local dedent = helpers.dedent
local Screen = {}
@@ -259,6 +261,17 @@ screen:redraw_debug() to show all intermediate screen states. ]])
end)
end
function Screen:expect_after_resize(expected)
if iswin() then
retry(nil, nil, function()
nvim('command', 'call jobsend(b:terminal_job_id, "\\<C-q>")')
self:expect(expected)
end)
else
self:expect(expected)
end
end
function Screen:wait(check, timeout)
local err, checked = false
local success_seen = false