git.haldean.org e / a74a07d
fuzzy file finder works haldean 1 year, 5 months ago
5 changed file(s) with 104 addition(s) and 41 deletion(s). Raw diff Collapse all Expand all
0 {. experimental: "notnil" .}
01 import blit
2 import input
13 import unicode
24
35 type
4 Buf* = ref object
6 Buf* = ref object of RootObj
57 buf: string
68 blit*: Blit
79
1214 return true
1315 return false
1416
15 proc paint(buf: var Buf) =
17 proc paint(buf: Buf) =
1618 var
1719 w = 0
1820 h = 0
1921 cur = 0
22 last: Rune
2023 for r in runes(buf.buf):
2124 if r.skip():
2225 continue
2730 cur += 1
2831 if cur > w:
2932 w = cur
33 last = r
34 if last != Rune('\n'):
35 h += 1
3036
3137 buf.blit.resize(Size(w: w, h: h))
3238 buf.blit.fill(NO_CHAR)
4349 p.x += 1
4450
4551 proc newBuf*(): Buf =
46 Buf(blit: newBlit(Size(w: 0, h: 0)))
52 return Buf(blit: newBlit(Size(w: 0, h: 0)))
4753
48 proc load*(buf: var Buf, f: string) =
54 proc load*(buf: Buf, f: string) =
4955 buf.buf = readFile(f)
5056 buf.paint()
5157
52 proc load*(buf: var Buf, f: File) =
58 proc load*(buf: Buf, f: File) =
5359 buf.buf = f.readAll()
5460 buf.paint()
5561
56 proc set*(buf: var Buf, str: string) =
62 proc set*(buf: Buf, str: string) =
5763 buf.buf = str
5864 buf.paint()
65
66 method handlekey*(this: Buf, k: Key): bool {. base .} =
67 false
119119 proc focused*(c: Comp): Layer =
120120 return c.layers[c.focus]
121121
122 proc focusnext*(c: var Comp, fwd: bool = true) =
123 if fwd:
124 c.focus = (c.focus + 1) mod c.layers.len
125 else:
126 if c.focus == 0:
127 c.focus = c.layers.len - 1
128 else:
129 c.focus -= 1
122 proc focus*(c: var Comp, layer: Layer) =
123 c.focus = find(c.layers, layer)
130124
131125 proc `size=`*(c: var Comp, s: Size) =
132126 c.tb = newTerminalBuffer(s.w, s.h)
+34
-23
e.nim less more
0 import blit, buf, comp
0 import blit, buf, comp, find
11 import illwill
2 import input
23 import os, system
34
45 var c = newComp(Size(w: 30, h: 20))
56
7 var fb = newFindBuf()
8
69 var bx = newBuf()
710 bx.load("test/pg844.txt")
811
9 var lx = newLayer(bx.blit)
10 c.pushlayer(lx)
12 var lx = newLayer(fb.blit)
13 lx.tag = LayoutTag.center
1114
1215 var ly = newLayer(bx.blit)
1316 ly.tag = LayoutTag.left
14 c.pushlayer(ly)
1517
1618 var lz = newLayer(bx.blit)
1719 lz.tag = LayoutTag.right
18 c.pushlayer(lz)
1920
2021 proc exitProc() {.noconv.} =
2122 illwillDeinit()
3334 setControlCHook(exitProc)
3435 hideCursor()
3536
37 #var bufs = @[(fb, lx), (bx, ly), (bx, lz)]
38 var bufs = @[(fb, lx)]
39 var focused = 0
40 for bl in bufs:
41 c.pushlayer(bl[1])
42 c.focus(bufs[focused][1])
43
3644 try:
3745 while true:
3846 var size = Size(w: terminalWidth(), h: terminalHeight())
3947 if size != c.size:
4048 c.size = size
4149
42 var k = illwill.getKey()
43 case k
44 of Key.Escape, Key.Q:
45 exitProc()
46 of Key.Tab:
47 c.focusnext()
48 of Key.J:
49 c.focused().src.y += 1
50 of Key.K:
51 c.focused().src.y -= 1
52 of Key.H:
53 c.focused().src.x += 1
54 of Key.L:
55 c.focused().src.x -= 1
56 of Key.None:
57 discard
58 else:
59 discard
50 var k = input.Key(illwill.getKey())
51
52 if not bufs[focused][0].handlekey(k):
53 case k.toIllwill
54 of illwill.Key.Escape, illwill.Key.Q:
55 exitProc()
56 of illwill.Key.Tab:
57 focused = (focused + 1) mod bufs.len
58 c.focus(bufs[focused][1])
59 of illwill.Key.J:
60 c.focused().src.y += 1
61 of illwill.Key.K:
62 c.focused().src.y -= 1
63 of illwill.Key.H:
64 c.focused().src.x += 1
65 of illwill.Key.L:
66 c.focused().src.x -= 1
67 of illwill.Key.None:
68 discard
69 else:
70 discard
6071
6172 c.paint()
6273 sleep(16)
33 import re
44 import sets
55 import sequtils
6 import strutils
67
78 type
89 Found = seq[string]
8384 scored.sort()
8485 return scored.map do (p: (Score, string)) -> string: p[1]
8586
87 proc findInCurrent*(): Found =
88 return find(getCurrentDir())
89
90 proc findInCurrent*(search: string): Found =
91 return find(getCurrentDir(), search)
92
8693 when isMainModule:
8794 var found: Found
88 if paramCount() < 1:
89 found = find(getCurrentDir())
95 if paramCount() == 0:
96 found = findInCurrent()
9097 else:
91 let search = paramstr(1)
92 found = find(getCurrentDir(), search)
98 found = findInCurrent(paramstr(1))
9399 for f in found:
94100 echo f
101
102 import blit
103 import buf
104 import input
105
106 type
107 FindBuf* = ref object of Buf
108 search: string
109
110 proc refresh(fb: FindBuf) =
111 let found = findInCurrent(fb.search)
112 fb.set("find file: " & fb.search & "\n" & found.join("\n"))
113
114 proc newFindBuf*(): Buf =
115 var fb = FindBuf(blit: newBlit(Size(w: 0, h: 0)))
116 fb.refresh()
117 return fb
118
119 method handlekey*(this: FindBuf, k: Key): bool =
120 if k.isEscape():
121 return false
122 let c = toChar(k)
123 if isNone(c):
124 return false
125 this.search = this.search & c.get()
126 this.refresh()
127 return true
0 import illwill
1 import options
2
3 type
4 Key* = distinct illwill.Key
5
6 proc toChar*(k: Key): Option[char] =
7 let i = int(k)
8 if i > 255 or i < 0:
9 return none[char]()
10 return some(i.char)
11
12 proc isEscape*(k: Key): bool =
13 return illwill.Key(k) == illwill.Key.Escape
14
15 proc toIllwill*(k: Key): illwill.Key = illwill.Key(k)