~sircmpwn/hare-vt

2bd8ef3c9fee77c1a166d6efbec0c1d96e79d834 — Drew DeVault 1 year, 1 month ago 608cbac
Add vt::getsize
4 files changed, 23 insertions(+), 1 deletions(-)

M example.ha
M vt/draw.ha
M vt/driver.ha
M vt/xterm.ha
M example.ha => example.ha +7 -0
@@ 31,7 31,14 @@ fn run() (void | vt::error) = {

	const p1 = vt::newpen(void, color::RED, style::ULINE);
	const p2 = vt::newpen(color::YELLOW, color::RED);
	const p3 = vt::newpen(void, color::BLUE);
	vt::print(term, "oh! ", tag(p1, "hello, ", tag(p2, "vt", 100, "!")), "\r\n")?;
	const (rows, cols) = vt::getsize(term)?;
	vt::print(term, "your terminal is ",
		tag(p3, rows),
		" rows by ",
		tag(p3, cols),
		" columns\r\n")?;

	for (true) {
		const ev = match (vt::getevent(term)?) {

M vt/draw.ha => vt/draw.ha +1 -1
@@ 42,7 42,7 @@ export fn print(
		case let t: styled =>
			term.driver.chpen(term, &t.pen)?;
			print(term, t.args...)?;
			term.driver.chpen(term, &prev)?;
		};
	};
	term.driver.chpen(term, &prev)?;
};

M vt/driver.ha => vt/driver.ha +8 -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 size of the terminal window, rows and columns.
	getsize: *fn(term: *term) ((uint, uint) | error),

	// Releases state associated with the driver and cleans up the terminal.
	destroy: *fn(term: *term) void,
};


@@ 81,3 84,8 @@ export fn enablecur(term: *term) (void | error) = {
export fn disablecur(term: *term) (void | error) = {
	return term.driver.chcur(term, false);
};

// Returns the size of the terminal window, rows and columns.
export fn getsize(term: *term) ((uint, uint) | error) = {
	return term.driver.getsize(term);
};

M vt/xterm.ha => vt/xterm.ha +7 -0
@@ 35,6 35,7 @@ const xterm_driver = driver {
	chalt = &xterm_chalt,
	chcur = &xterm_chcur,
	movecur = &xterm_movecur,
	getsize = &xterm_getsize,
	destroy = &xterm_destroy,
};



@@ 485,3 486,9 @@ fn xterm_movecur(term: *term, row: uint, col: uint) (void | error) = {
	let term = term: *xterm;
	fmt::fprintf(term.out, "\x1b[{};{}H", row, col)?;
};

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