view cmd/hg-wolp.ha @ 4:4016ab04414d main tip

Implemented simple titlebar with repo and branch name
author luxanna <l.lux@magenta.de>
date Mon, 18 May 2026 23:45:48 +0200
parents 8376aa48453d
children
line wrap: on
line source

use termbox;
use types::c;
use fmt;
use time;
use strings;
use fs;
use io;
use path;
use os;
use encoding::utf8;
//use hg;

fn branch_name() (str | utf8::invalid) = {
	let cwd = path::init(strings::dup(os::getcwd())!)!;
	let branch_filep = path::push(&cwd, ".hg", "branch")!;
	let buf: [32]u8 = [0...];
	let fp = fs::open(os::cwd, branch_filep)!;
	let sz: size = 0;
	match(io::readall(fp, buf)){
		case let e: io::error => {
			if(e is io::underread)
				sz = e: size;
		};
		case => none();
	};
	return strings::fromutf8(buf[..sz-1]);
};

// Helper fn to draw a string within a line and return the next position
fn draw_str(x: u16, y: u16, text: str) u16 = {
	let iter = strings::iter(text);
	for(let r => strings::next(&iter)){
		termbox::change_cell(x, y, r);
                //Assume ascii width of 1 for now
		x+=1;
        };
	return x;
};

fn draw_title_bar(ts: termbox::resize_event) void = {
	let title = path::basename(strings::dup(os::getcwd())!);
	let branch = branch_name()!;
	let x: u16 = ((ts.w: size - (len(title) + len(branch) + 3))/2): u16;
	x = draw_str(x, 0, title);
	termbox::change_cell(x+1, 0, '[');
	x+=2;
	x = draw_str(x, 0, branch);
	termbox::change_cell(x, 0, ']');
};

fn draw_ui(ts: termbox::resize_event) void = {
	draw_title_bar(ts);

	termbox::present();
};

fn none() void = {
        return;
};

export fn main() void = {
	termbox::init()!;

	draw_ui(termbox::resize_event{ w = termbox::width(), h = termbox::height() });

	for(true){
		match(termbox::poll_event()){
			case let ev: termbox::event => {
				match(ev){
					case let key: termbox::key_event => {
						match(key.key){
							case let keytype: termbox::key_type => {
								if(keytype == termbox::key_type::ESC){
									break;
								};
							};
							case let ch: rune => {
								if(ch == 'q'){
									break;
								};
							};
						};
					};
					case let ev: termbox::resize_event => {
						termbox::clear();
						draw_ui(ev);
					};
					case => none();
				};
			};
			case => none();
		};
	};

	//TODO
	termbox::shutdown();
};