~sircmpwn/hare-vt

788a8b0d37bc5e6fe21d0503bcf1c37bec132664 — Drew DeVault 11 months ago 47879e7
vt::getcur: new function
3 files changed, 25 insertions(+), 0 deletions(-)

M vt/draw.ha
M vt/driver.ha
M vt/xterm.ha
M vt/draw.ha => vt/draw.ha +5 -0
@@ 18,6 18,11 @@ export fn move(term: *term, row: uint, col: uint) (void | error) = {
	return term.driver.movecur(term, row, col);
};

// Returns the terminal's cursor position, rows and columns, from 1.
export fn getcur(term: *term) ((uint, uint) | error) = {
	return term.driver.getcur(term);
};

// Prepares a styled value with a given pen, for use with [[fprint]].
export fn tag(
	pen: pen,

M vt/driver.ha => vt/driver.ha +3 -0
@@ 24,6 24,9 @@ export type driver = struct {
	// Move the cursor to the desired row and column, 1-indexed.
	movecur: *fn(term: *term, row: uint, col: uint) (void | error),

	// Returns the cursor position row and column, 1-indexed.
	getcur: *fn(term: *term) ((uint, uint) | error),

	// Returns the size of the terminal window, rows and columns.
	getsize: *fn(term: *term) ((uint, uint) | error),


M vt/xterm.ha => vt/xterm.ha +17 -0
@@ 3,6 3,8 @@ use encoding::utf8;
use fmt;
use io;
use os;
use strconv;
use strings;
use unix::poll;
use unix::tty;



@@ 36,6 38,7 @@ const xterm_driver = driver {
	chalt = &xterm_chalt,
	chcur = &xterm_chcur,
	movecur = &xterm_movecur,
	getcur = &xterm_getcur,
	getsize = &xterm_getsize,
	destroy = &xterm_destroy,
};


@@ 488,6 491,20 @@ fn xterm_movecur(term: *term, row: uint, col: uint) (void | error) = {
	fmt::fprintf(term.out, "\x1b[{};{}H", row, col)?;
};

fn xterm_getcur(term: *term) ((uint, uint) | error) = {
	let term = term: *xterm;
	fmt::fprint(term.out, "\x1b[6n")?;

	let buf: [16]u8 = [0...];
	const n = io::read(term.in, buf)? as size;
	assert(buf[0] == '\x1b' && buf[1] == '[');

	const dsr = strings::fromutf8(buf[2..n])!;
	fmt::errorln(dsr)!;
	const (a, b) = strings::cut(strings::rtrim(dsr, 'R'), ";");
	return (strconv::stou(a)!, strconv::stou(b)!);
};

fn xterm_getsize(term: *term) ((uint, uint) | error) = {
	const term = term: *xterm;
	const sz = tty::winsize(term.out)?;