diff options
Diffstat (limited to 'cmd/il.ha')
| -rw-r--r-- | cmd/il.ha | 59 |
1 files changed, 59 insertions, 0 deletions
@@ -5,6 +5,7 @@ use bufio; use os; use strings; use fmt; +use io; export fn main() void = { const scanner = bufio::newscanner(os::stdin); @@ -28,6 +29,9 @@ export fn main() void = { vl.layout.print(&vl); + let term: (str | void) = void; + defer if (term is str) free(term as str) else void; + let revsearch = false; for (true) { const r = tui::read(&state)!; if (r == 'j') { @@ -55,6 +59,61 @@ export fn main() void = { fmt::println(li.items[li.cursor])!; break; }; + if (r == '/' || r == '?') { + revsearch = r == '?'; + term = search(&state, &li, r as rune); + if (revsearch) prevsearch(&li, term) else nextsearch(&li, term); + }; + if (r == 'n') { + if (revsearch) prevsearch(&li, term) else nextsearch(&li, term); + }; + if (r == 'N') { + if (!revsearch) prevsearch(&li, term) else nextsearch(&li, term); + }; vl.layout.print(&vl); }; }; + +fn search(state: *tui::tui, li: *list::scrolllist, prefix: (str | rune) = '/') (str | void) = { + tui::unraw(state); + defer tui::raw(state)!; + fmt::fprint(state.out, prefix)!; + const uline = match (bufio::read_line(state.out)!) { + case let u: []u8 => + yield u; + case io::EOF => + return; + }; + defer free(uline); + return strings::dup(strings::fromutf8(uline)!); +}; + +fn nextsearch(li: *list::scrolllist, term: (str | void)) void = { + const term = match (term) { + case let term: str => + yield term; + case void => + return; + }; + for (let i = li.cursor + 1; i < len(li.items); i += 1) { + if (strings::contains(li.items[i], term)) { + list::setcursor(li, i: u16); + return; + }; + }; +}; + +fn prevsearch(li: *list::scrolllist, term: (str | void)) void = { + const term = match (term) { + case let term: str => + yield term; + case void => + return; + }; + for (let i: int = li.cursor: int - 1; i >= 0; i -= 1) { + if (strings::contains(li.items[i], term)) { + list::setcursor(li, i: u16); + return; + }; + }; +}; |
