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