-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtextbrowse.py
191 lines (170 loc) · 6.88 KB
/
textbrowse.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""
Text file browsing (and displaying) / Art gallery script for x/84
-----------------------------------------------------------------
This script lets the user browse the filesystem and look at text/ansi files.
The root dir is set by changing the startfolder variable.
You can also pass a textfile as an argument to the script.
Enjoy!
"""
from x84.bbs import getsession, echo, getch, gosub, getterminal, showart, Lightbar
from common import waitprompt
import os
import codecs
__author__ = 'Hellbeard'
__version__ = 2.02
LIGHTBAR_X = 5
LIGHTBAR_Y = 8
STARTFOLDER = os.path.join(os.path.dirname(__file__)+'/art/textfiles/', )
# ---------------------------------------------------
def banner():
artfile = os.path.join(os.path.dirname(__file__), 'art', 'textbrowse.ans')
banner = ''
for line in showart(artfile, center=True):
banner = banner + line
return banner
# ---------------------------------------------------
def displayfile(filename):
term = getterminal()
session = getsession()
asciitext = []
text = []
offset = 0
quit = False
istext = False
dirty = True
echo(term.clear)
if os.path.splitext(filename)[1][1:].lower() == 'txt': # text files are opened with codecs
file = codecs.open(filename,encoding='cp437')
for line in file:
text.append(line.rstrip())
istext = True
else:
for line in showart(filename): # ansi / ascii files are opened with showart
text.append(line)
while not quit:
if dirty:
echo(term.move(0,0)+term.normal)
for i in range (0, term.height-1):
if len(text) > i+offset:
if istext == True: # atleast we can prevent text files from wrapping..
echo(term.clear_eol+term.move_x(max(0,(term.width/2)-40))+text[i+offset][:term.width]+'\r\n')
else:
echo(term.clear_eol+term.move_x(max(0,(term.width/2)-40))+text[i+offset])
event, data = session.read_events(('input', 'refresh'))
if event == 'refresh':
echo(term.clear()) # to ensure there are no leftovers when resizing the window
dirty = True
if event == 'input':
dirty = True
session.buffer_input(data, pushback=True)
inp = term.inkey(0)
while inp:
if inp.lower() == u'q' or inp.code == term.KEY_ESCAPE or inp.code == term.KEY_ENTER:
quit = True
break
elif inp.code == term.KEY_HOME and offset > 0:
offset = 0
elif inp.code == term.KEY_END and len(text) > term.height:
offset = len(text) - term.height+1
elif inp.code == term.KEY_DOWN and len(text) > offset+term.height-1:
offset = offset + 1
elif inp.code == term.KEY_UP and offset > 0:
offset = offset -1
elif (inp.code == term.KEY_LEFT or inp.code == term.KEY_PGUP) and offset > 0:
if offset > term.height:
offset = offset - term.height+2
else:
offset = 0
elif (inp.code == term.KEY_RIGHT or inp.code == term.KEY_PGDOWN) and offset+term.height-1 < len(text):
if (offset+term.height*2)-1 < len(text):
offset = offset + term.height-2
else:
offset = max(0, len(text) - term.height+1)
else:
dirty = False
inp = term.inkey(0)
# ---------------------------------------------------
def getfilelist(folder):
term = getterminal()
lb_names = []
filenames = []
for fn in sorted(os.listdir(folder)):
filenames.append(fn)
if os.path.isfile(folder+fn):
fn = ' '+ fn + ' ' *(52-len(fn))+term.cyan+'[ File ]'
lb_names.append(fn)
else:
fn = u' '+ fn + '/' + ' ' *(51-len(fn))+term.cyan+'[ Directory ]'
lb_names.append(fn)
filelist = zip (filenames,lb_names)
# converts the lightbar names and file names into tuples. keys / names for
# the lightbar class to handle
return filelist
# ---------------------------------------------------
def update_lightbar(lbar, term, filelist):
if term.width > 79:
lbar.height = 14
else:
lbar.height = max(4,term.height)
if term.height > 22:
lbar.yloc = LIGHTBAR_Y
else:
lbar.yloc = 0
lbar.width = min(69,term.width)
lbar.xloc = max(0,LIGHTBAR_X+(term.width/2)-40)
lbar.update(filelist)
echo(u''.join([lbar.refresh()]))
# ---------------------------------------------------
def main(file=None, invert=False, Showart=True):
""" Main procedure. """
if file != None:
displayfile(file)
return
session = getsession()
term = getterminal()
session.activity = 'Reading text files'
currentfolder = []
stored_lbar_pos = []
filelist = getfilelist(STARTFOLDER)
dirty = True
inp = None
lbar = Lightbar(height = 0, width = 0, xloc = 0, yloc = 0, colors={'highlight': term.bold_white_on_red})
# sets up a lightbar. update_lightbar will give it it's actual values.
echo(term.clear+banner()+term.hide_cursor)
while 1:
if dirty:
update_lightbar(lbar,term,filelist)
dirty = False
while not inp:
inp = getch(0.2)
if session.poll_event('refresh'):
echo(term.clear)
if term.width > 79: echo(banner())
update_lightbar(lbar,term,filelist)
dirty = True
lbar.process_keystroke(inp)
if lbar.quit:
echo(term.normal_cursor)
return
echo(lbar.refresh_quick())
if inp == term.KEY_ENTER:
if lbar.selection[1] == '( .. ) GO BACK' or os.path.isdir(STARTFOLDER+u''.join(currentfolder)+lbar.selection[0]):
if lbar.selection[1] == '( .. ) GO BACK':
del currentfolder[-1]
filelist = getfilelist(STARTFOLDER+u''.join(currentfolder))
lbar.update(filelist)
lbar.goto(stored_lbar_pos[-1])
del stored_lbar_pos[-1]
else:
currentfolder.append(lbar.selection[0]+u'/')
stored_lbar_pos.append(lbar.index)
filelist = getfilelist(STARTFOLDER+u''.join(currentfolder))
lbar.goto(0)
if len(currentfolder) > 0:
filelist.insert(0,['( .. ) GO BACK','( .. ) GO BACK'])
lbar.update(filelist)
else:
displayfile(STARTFOLDER+u''.join(currentfolder)+lbar.selection[0])
echo(term.clear+banner())
dirty = True
inp = None