comparison 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
comparison
equal deleted inserted replaced
3:7e3c01987454 4:4016ab04414d
1 use termbox; 1 use termbox;
2 use os;
3 use types::c; 2 use types::c;
4 use fmt; 3 use fmt;
4 use time;
5 use strings;
6 use fs;
7 use io;
8 use path;
9 use os;
10 use encoding::utf8;
5 //use hg; 11 //use hg;
6 12
13 fn branch_name() (str | utf8::invalid) = {
14 let cwd = path::init(strings::dup(os::getcwd())!)!;
15 let branch_filep = path::push(&cwd, ".hg", "branch")!;
16 let buf: [32]u8 = [0...];
17 let fp = fs::open(os::cwd, branch_filep)!;
18 let sz: size = 0;
19 match(io::readall(fp, buf)){
20 case let e: io::error => {
21 if(e is io::underread)
22 sz = e: size;
23 };
24 case => none();
25 };
26 return strings::fromutf8(buf[..sz-1]);
27 };
28
29 // Helper fn to draw a string within a line and return the next position
30 fn draw_str(x: u16, y: u16, text: str) u16 = {
31 let iter = strings::iter(text);
32 for(let r => strings::next(&iter)){
33 termbox::change_cell(x, y, r);
34 //Assume ascii width of 1 for now
35 x+=1;
36 };
37 return x;
38 };
39
40 fn draw_title_bar(ts: termbox::resize_event) void = {
41 let title = path::basename(strings::dup(os::getcwd())!);
42 let branch = branch_name()!;
43 let x: u16 = ((ts.w: size - (len(title) + len(branch) + 3))/2): u16;
44 x = draw_str(x, 0, title);
45 termbox::change_cell(x+1, 0, '[');
46 x+=2;
47 x = draw_str(x, 0, branch);
48 termbox::change_cell(x, 0, ']');
49 };
50
51 fn draw_ui(ts: termbox::resize_event) void = {
52 draw_title_bar(ts);
53
54 termbox::present();
55 };
56
57 fn none() void = {
58 return;
59 };
7 60
8 export fn main() void = { 61 export fn main() void = {
9 //termbox::init()!; 62 termbox::init()!;
63
64 draw_ui(termbox::resize_event{ w = termbox::width(), h = termbox::height() });
65
66 for(true){
67 match(termbox::poll_event()){
68 case let ev: termbox::event => {
69 match(ev){
70 case let key: termbox::key_event => {
71 match(key.key){
72 case let keytype: termbox::key_type => {
73 if(keytype == termbox::key_type::ESC){
74 break;
75 };
76 };
77 case let ch: rune => {
78 if(ch == 'q'){
79 break;
80 };
81 };
82 };
83 };
84 case let ev: termbox::resize_event => {
85 termbox::clear();
86 draw_ui(ev);
87 };
88 case => none();
89 };
90 };
91 case => none();
92 };
93 };
10 94
11 //TODO 95 //TODO
12 fmt::println("Hello")!; 96 termbox::shutdown();
13 //termbox::shutdown();
14 }; 97 };