-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlss.py
executable file
·103 lines (89 loc) · 2.16 KB
/
lss.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import curses
import os
import sys
search = ""
argId = 0
for arg in sys.argv:
if argId > 0:
search = arg
argId+=1
scroll = 0
selected = 0
oldSelected = 0
if search in ["help","-h","-help","-H","--h"]:
print "lss: usage: lss [substring]"
sys.exit()
scr = curses.initscr()
scr.border(10)
curses.curs_set(0)
curses.noecho()
scr.keypad(1)
def listDir(directory):
ret = os.listdir(directory)
dotrets = []
ret2 = []
for thing in ret:
if search in thing:
if thing.startswith("."):
dotrets.append(thing)
else:
ret2.append(thing)
for thing in dotrets:
ret2.append(thing)
return ret2
files = listDir(os.curdir)
def drawLines():
for f in files[0+scroll:20+scroll]:
if f == files[selected]:
scr.addstr(f + '\n', curses.A_STANDOUT)
elif os.path.isdir(f):
scr.addstr(f + '\n', curses.A_BOLD)
else:
scr.addstr(f + '\n')
scr.addstr("\nPress V to open in Vim\n")
scr.addstr("Press Q to quit\n",curses.A_BOLD)
while(True):
scr.clear()
drawLines()
event = scr.getch()
if event==curses.KEY_UP or event==ord('k'):
if selected > 0:
selected -= 1
else:
selected = len(files)-1
scroll = selected - 10
if scroll > 0 and selected < 10+scroll:
scroll -= 1
if event==curses.KEY_DOWN or event==ord('j'):
if selected < len(files)-1:
selected += 1
else:
selected = 0
scroll = 0
if selected > 10+scroll:
scroll += 1
if event==ord('v'):
os.system('vim ' + files[selected])
if event==ord('q'):
break
if event==curses.KEY_RIGHT or event==ord('l'):
search = ""
if os.path.isdir(files[selected]):
directory = files[selected]
files = listDir(directory)
os.chdir(directory)
oldSelected = selected
selected = 0
scroll = 0
else:
os.system('vim' + files[selected])
break
if event==curses.KEY_LEFT or event==ord('h'):
search = ""
files = listDir('..')
os.chdir('..')
selected = oldSelected
scroll = 0
while selected > 10+scroll:
scroll += 1
curses.endwin()