fuzzy file finder works
haldean
2 years ago
|
0 |
{. experimental: "notnil" .}
|
0 | 1 |
import blit
|
|
2 |
import input
|
1 | 3 |
import unicode
|
2 | 4 |
|
3 | 5 |
type
|
4 | |
Buf* = ref object
|
|
6 |
Buf* = ref object of RootObj
|
5 | 7 |
buf: string
|
6 | 8 |
blit*: Blit
|
7 | 9 |
|
|
12 | 14 |
return true
|
13 | 15 |
return false
|
14 | 16 |
|
15 | |
proc paint(buf: var Buf) =
|
|
17 |
proc paint(buf: Buf) =
|
16 | 18 |
var
|
17 | 19 |
w = 0
|
18 | 20 |
h = 0
|
19 | 21 |
cur = 0
|
|
22 |
last: Rune
|
20 | 23 |
for r in runes(buf.buf):
|
21 | 24 |
if r.skip():
|
22 | 25 |
continue
|
|
27 | 30 |
cur += 1
|
28 | 31 |
if cur > w:
|
29 | 32 |
w = cur
|
|
33 |
last = r
|
|
34 |
if last != Rune('\n'):
|
|
35 |
h += 1
|
30 | 36 |
|
31 | 37 |
buf.blit.resize(Size(w: w, h: h))
|
32 | 38 |
buf.blit.fill(NO_CHAR)
|
|
43 | 49 |
p.x += 1
|
44 | 50 |
|
45 | 51 |
proc newBuf*(): Buf =
|
46 | |
Buf(blit: newBlit(Size(w: 0, h: 0)))
|
|
52 |
return Buf(blit: newBlit(Size(w: 0, h: 0)))
|
47 | 53 |
|
48 | |
proc load*(buf: var Buf, f: string) =
|
|
54 |
proc load*(buf: Buf, f: string) =
|
49 | 55 |
buf.buf = readFile(f)
|
50 | 56 |
buf.paint()
|
51 | 57 |
|
52 | |
proc load*(buf: var Buf, f: File) =
|
|
58 |
proc load*(buf: Buf, f: File) =
|
53 | 59 |
buf.buf = f.readAll()
|
54 | 60 |
buf.paint()
|
55 | 61 |
|
56 | |
proc set*(buf: var Buf, str: string) =
|
|
62 |
proc set*(buf: Buf, str: string) =
|
57 | 63 |
buf.buf = str
|
58 | 64 |
buf.paint()
|
|
65 |
|
|
66 |
method handlekey*(this: Buf, k: Key): bool {. base .} =
|
|
67 |
false
|
119 | 119 |
proc focused*(c: Comp): Layer =
|
120 | 120 |
return c.layers[c.focus]
|
121 | 121 |
|
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)
|
130 | 124 |
|
131 | 125 |
proc `size=`*(c: var Comp, s: Size) =
|
132 | 126 |
c.tb = newTerminalBuffer(s.w, s.h)
|
0 | |
import blit, buf, comp
|
|
0 |
import blit, buf, comp, find
|
1 | 1 |
import illwill
|
|
2 |
import input
|
2 | 3 |
import os, system
|
3 | 4 |
|
4 | 5 |
var c = newComp(Size(w: 30, h: 20))
|
5 | 6 |
|
|
7 |
var fb = newFindBuf()
|
|
8 |
|
6 | 9 |
var bx = newBuf()
|
7 | 10 |
bx.load("test/pg844.txt")
|
8 | 11 |
|
9 | |
var lx = newLayer(bx.blit)
|
10 | |
c.pushlayer(lx)
|
|
12 |
var lx = newLayer(fb.blit)
|
|
13 |
lx.tag = LayoutTag.center
|
11 | 14 |
|
12 | 15 |
var ly = newLayer(bx.blit)
|
13 | 16 |
ly.tag = LayoutTag.left
|
14 | |
c.pushlayer(ly)
|
15 | 17 |
|
16 | 18 |
var lz = newLayer(bx.blit)
|
17 | 19 |
lz.tag = LayoutTag.right
|
18 | |
c.pushlayer(lz)
|
19 | 20 |
|
20 | 21 |
proc exitProc() {.noconv.} =
|
21 | 22 |
illwillDeinit()
|
|
33 | 34 |
setControlCHook(exitProc)
|
34 | 35 |
hideCursor()
|
35 | 36 |
|
|
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 |
|
36 | 44 |
try:
|
37 | 45 |
while true:
|
38 | 46 |
var size = Size(w: terminalWidth(), h: terminalHeight())
|
39 | 47 |
if size != c.size:
|
40 | 48 |
c.size = size
|
41 | 49 |
|
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
|
60 | 71 |
|
61 | 72 |
c.paint()
|
62 | 73 |
sleep(16)
|
3 | 3 |
import re
|
4 | 4 |
import sets
|
5 | 5 |
import sequtils
|
|
6 |
import strutils
|
6 | 7 |
|
7 | 8 |
type
|
8 | 9 |
Found = seq[string]
|
|
83 | 84 |
scored.sort()
|
84 | 85 |
return scored.map do (p: (Score, string)) -> string: p[1]
|
85 | 86 |
|
|
87 |
proc findInCurrent*(): Found =
|
|
88 |
return find(getCurrentDir())
|
|
89 |
|
|
90 |
proc findInCurrent*(search: string): Found =
|
|
91 |
return find(getCurrentDir(), search)
|
|
92 |
|
86 | 93 |
when isMainModule:
|
87 | 94 |
var found: Found
|
88 | |
if paramCount() < 1:
|
89 | |
found = find(getCurrentDir())
|
|
95 |
if paramCount() == 0:
|
|
96 |
found = findInCurrent()
|
90 | 97 |
else:
|
91 | |
let search = paramstr(1)
|
92 | |
found = find(getCurrentDir(), search)
|
|
98 |
found = findInCurrent(paramstr(1))
|
93 | 99 |
for f in found:
|
94 | 100 |
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)
|