~sircmpwn/hare-vt

990c943f09f53249f4e4bc9d431d51b4f3bc1a71 — Drew DeVault 1 year, 1 month ago 5aa0e8d
vt/xterm.ha: merge eof into xterm struct
1 files changed, 35 insertions(+), 37 deletions(-)

M vt/xterm.ha
M vt/xterm.ha => vt/xterm.ha +35 -37
@@ 71,13 71,13 @@ fn xterm_pollev(term: *term, block: bool) (void | event | io::EOF | error) = {

	for (let first = true; first || block; first = false) {
		// opportunistic nonblocking poll on first loop
		let eof = xterm_poll(term, !first)? is io::EOF;
		xterm_poll(term, !first)?;
		let d = utf8::decode(term.buf[..term.end]);
		let res = event { value = '?', ... };

		let r = match (xterm_nextrune(term, &d, eof)?) {
		let r = match (xterm_nextrune(term, &d)?) {
		case void =>
			if (eof) {
			if (term.eof) {
				return io::EOF;
			} else {
				continue;


@@ 88,7 88,7 @@ fn xterm_pollev(term: *term, block: bool) (void | event | io::EOF | error) = {
			yield r;
		};

		match (xterm_decode_event(term, &d, eof, &res, r)) {
		match (xterm_decode_event(term, &d, &res, r)) {
		case more =>
			continue;
		case void =>


@@ 124,11 124,33 @@ fn xterm_pollev(term: *term, block: bool) (void | event | io::EOF | error) = {
	};
};

// TODO: Merge dec and eof into [[term]]
fn xterm_poll(term: *xterm, block: bool) (size | io::EOF | error) = {
	const polls = [
		poll::pollfd {
			fd = os::stdin_file,
			events = poll::event::POLLIN | poll::event::POLLHUP,
			...
		},
	];

	const timeout = if (block) poll::INDEF else poll::NONBLOCK;
	if (poll::poll(polls, timeout)? == 0) {
		return 0z;
	};

	match (io::read(os::stdin_file, term.buf[term.end..])?) {
	case io::EOF =>
		term.eof = true;
		return io::EOF;
	case let n: size =>
		term.end += n;
		return n;
	};
};

fn xterm_decode_event(
	term: *xterm,
	dec: *utf8::decoder,
	eof: bool,
	res: *event,
	r: rune,
) (void | more | invalid) = {


@@ 138,7 160,7 @@ fn xterm_decode_event(
		return;
	};

	r = match (xterm_nextrune(term, dec, eof)?) {
	r = match (xterm_nextrune(term, dec)?) {
	case void =>
		yield ESC;
	case let r: rune =>


@@ 160,7 182,7 @@ fn xterm_decode_event(
	};

	let ss3 = r == 'O';
	match (xterm_nextrune(term, dec, eof)?) {
	match (xterm_nextrune(term, dec)?) {
	case void =>
		res.mods = modflag::ALT;
		res.value = r;


@@ 171,9 193,9 @@ fn xterm_decode_event(
				n[i] *= 10;
				n[i] += r: u32 - '0': u32;

				r = match (xterm_nextrune(term, dec, eof)?) {
				r = match (xterm_nextrune(term, dec)?) {
				case void =>
					if (eof) {
					if (term.eof) {
						xterm_trimbuf(term, dec.offs);
						return invalid;
					} else {


@@ 253,11 275,11 @@ fn xterm_decode_event(
				res.mods = (n[i]: u8 - 1): modflag;
				break;
			case ';' =>
				r = match (xterm_nextrune(term, dec, eof)?) {
				r = match (xterm_nextrune(term, dec)?) {
				case let r: rune =>
					yield r;
				case void =>
					if (eof) {
					if (term.eof) {
						xterm_trimbuf(term, dec.offs);
						return invalid;
					} else {


@@ 307,7 329,6 @@ fn xterm_decode_key_tilde(n: [3]u32, i: size) (key | invalid) = {
fn xterm_nextrune(
	term: *xterm,
	d: *utf8::decoder,
	eof: bool,
) (void | rune | utf8::more | invalid) = {
	match (utf8::next(d)) {
	case let r: rune =>


@@ 315,7 336,7 @@ fn xterm_nextrune(
	case void =>
		return void;
	case utf8::more =>
		if (eof) {
		if (term.eof) {
			xterm_trimbuf(term, d.offs);
			return invalid;
		} else {


@@ 332,29 353,6 @@ fn xterm_trimbuf(term: *xterm, pos: size) void = {
	term.end -= pos;
};

fn xterm_poll(term: *xterm, block: bool) (size | io::EOF | error) = {
	const polls = [
		poll::pollfd {
			fd = os::stdin_file,
			events = poll::event::POLLIN | poll::event::POLLHUP,
			...
		},
	];

	const timeout = if (block) poll::INDEF else poll::NONBLOCK;
	if (poll::poll(polls, timeout)? == 0) {
		return 0z;
	};

	match (io::read(os::stdin_file, term.buf[term.end..])?) {
	case io::EOF =>
		return io::EOF;
	case let n: size =>
		term.end += n;
		return n;
	};
};

fn xterm_getpen(term: *term) pen = {
	let term = term: *xterm;
	return term.pen;