Compare commits

3 Commits
master ... dev

Author SHA1 Message Date
744e3a4885 Added comments 2017-08-11 02:07:41 +02:00
0594320abc Removed unneeded prints 2017-08-06 16:24:52 +02:00
a83f2c6926 Added comments 2017-08-06 16:24:19 +02:00
3 changed files with 63 additions and 14 deletions

View File

@@ -1,9 +1,10 @@
import urwid
from lineWalker import LineWalker
import uuid import uuid
from time import sleep from time import sleep
import urwid
import mainUI import mainUI
import pdb from lineWalker import LineWalker
class EditorUI(object): class EditorUI(object):
@@ -13,8 +14,16 @@ class EditorUI(object):
self.filename = filename self.filename = filename
# Default footer, showing possible key sequences
self.default_footer = urwid.AttrWrap(urwid.Text("<Alt + y> Save as | <Alt + q> Close"), "standard") self.default_footer = urwid.AttrWrap(urwid.Text("<Alt + y> Save as | <Alt + q> Close"), "standard")
# Text field for new filemes. Defining it here and seperately, so we may access it later
self.filename_field = urwid.Edit("Enter new filename: ")
# Footer shown during filename selection
self.filename_footer = urwid.AttrWrap(self.filename_field, "filename")
# Defining view components
self.walker = LineWalker(filename) self.walker = LineWalker(filename)
self.content = urwid.ListBox(self.walker) self.content = urwid.ListBox(self.walker)
@@ -26,68 +35,104 @@ class EditorUI(object):
def open(self, loop = None, callback = None, quicknote = False): def open(self, loop = None, callback = None, quicknote = False):
# If we are edting a (new) quicknote, show this header
if quicknote: if quicknote:
self.central_frame.set_header(urwid.AttrWrap(urwid.Text("Notology Quicknote\n" + self.filename, align = "center"), "header")) self.central_frame.set_header(urwid.AttrWrap(urwid.Text("Notology Quicknote\n" + self.filename, align = "center"), "header"))
# Remember the callback function (if any) we got, so we may return to the last view, once we have finished our editing
self.callback = callback self.callback = callback
self.palette = [("standard", "light blue", "black"), self.palette = [("standard", "light blue", "black"),
("warning", "yellow", "black"), ("warning", "yellow", "black"),
("header", "black", "dark blue")] ("header", "black", "dark blue")]
# Check if the UI is fresh by checking if a loop has been passed. If not, create a new one, otherwise use the existing one
fresh = False fresh = False
if loop == None: if loop == None:
self.loop = urwid.MainLoop(self.central_frame, self.palette, unhandled_input = self.keypress_handler) self.loop = urwid.MainLoop(self.central_frame, self.palette, unhandled_input = self.keypress_handler)
fresh = True fresh = True
else: else:
# Update the existing loop with the attributes we would otherwise have set in the above constructor
loop.widget = self.central_frame loop.widget = self.central_frame
loop.unhandled_input = self.keypress_handler loop.unhandled_input = self.keypress_handler
# We can't just do loop.palette = [...]! We have to 'register' our local palette.
loop.screen.register_palette(self.palette) loop.screen.register_palette(self.palette)
loop.draw_screen()
# This is our new loop! Remember it
self.loop = loop self.loop = loop
# Use our new loop to redraw the screen, as it is running already
self.loop.draw_screen()
if fresh: if fresh:
# If this loop is new, we have to start it first. Duh!
self.loop.run() self.loop.run()
return 0 return 0
def keypress_handler(self, key): def keypress_handler(self, key):
# What we're gonna do, if we encounter any keystrokes
if key == "meta q":
if key == "meta q":
# Quit the editor. If we have no callback function, exit the main loop and thus quit the program. Else, call the callback function and "go back"
if not self.callback: if not self.callback:
raise urwid.ExitMainLoop() self.exit_program()
else: else:
#mainUI.MainUI().open(self.loop)
self.callback(self.loop) self.callback(self.loop)
elif key == "meta y": elif key == "meta y":
# Save the file
self.save(self.filename, verbose = True) self.save(self.filename, verbose = True)
elif key == "delete": elif key == "delete":
# delete at end of line # delete at end of line
self.walker.combine_focus_with_next() self.walker.combine_focus_with_next()
elif key == "backspace": elif key == "backspace":
# backspace at beginning of line # backspace at beginning of line
self.walker.combine_focus_with_prev() self.walker.combine_focus_with_prev()
elif key == "enter": elif key == "enter":
# start new line # start new line
self.walker.split_focus() self.walker.split_focus()
# move the cursor to the new line and reset pref_col # move the cursor to the new line and reset pref_col
self.loop.process_input(["down", "home"]) self.loop.process_input(["down", "home"])
elif key == "right": elif key == "right":
# Check if there is a char right to the cursor, if yes, move there
w, pos = self.walker.get_focus() w, pos = self.walker.get_focus()
w, pos = self.walker.get_next(pos) w, pos = self.walker.get_next(pos)
if w: if w:
self.listbox.set_focus(pos, 'above') self.listbox.set_focus(pos, 'above')
self.loop.process_input(["home"]) self.loop.process_input(["home"])
elif key == "left": elif key == "left":
# Check if there is a char left to the cursor, if yes, move there
w, pos = self.walker.get_focus() w, pos = self.walker.get_focus()
w, pos = self.walker.get_prev(pos) w, pos = self.walker.get_prev(pos)
if w: if w:
self.listbox.set_focus(pos, 'below') self.listbox.set_focus(pos, 'below')
self.loop.process_input(["end"]) self.loop.process_input(["end"])
else: else:
# Everything else is fine as well
return return
return True return True
def keypress_handler_filename(self, key):
# A dedicated keypress handler for the filename text field
if key == "enter":
# If return is hit, we are finished here
self.filename = self.filename_field.get_edit_text()
def save(self, save_filename, verbose = False): def save(self, save_filename, verbose = False):
walker = self.walker walker = self.walker
lines = [] lines = []
@@ -115,6 +160,12 @@ class EditorUI(object):
sleep(2) sleep(2)
self.central_frame.footer = self.default_footer self.central_frame.footer = self.default_footer
def exit_program(self):
# Exit this program
raise urwid.ExitMainLoop()
def re_tab(s): def re_tab(s):
"""Return a tabbed string from an expanded one.""" """Return a tabbed string from an expanded one."""
l = [] l = []

View File

@@ -20,7 +20,6 @@ def main():
try: try:
opts, args = getopt.getopt(sys.argv[1:], "q", ["quicknote"]) opts, args = getopt.getopt(sys.argv[1:], "q", ["quicknote"])
except getopt.GetoptError as err: except getopt.GetoptError as err:
# print help information and exit:
print(err) print(err)
sys.exit(2) sys.exit(2)
if not opts: if not opts:
@@ -28,10 +27,8 @@ def main():
for o, a in opts: for o, a in opts:
if o in ("-q", "--quicknote"): if o in ("-q", "--quicknote"):
print("New quicknote")
new_quicknote() new_quicknote()
else: else:
assert False, "unhandled option" assert False, "unhandled option"
#print(mainUI.MainUI().open())
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@@ -52,8 +52,9 @@ class MainUI(object):
file_path_lookup[filename] = fullpath file_path_lookup[filename] = fullpath
with open(fullpath, "r") as fhandle: with open(fullpath, "r") as fhandle:
# Get creation time # Get creation timei
appendstring = str(datetime.datetime.strptime(time.ctime(os.path.getctime(fullpath)), "%a %b %d %H:%M:%S %Y")) + "\n" timestamp = time.localtime(os.path.getctime(fullpath))
appendstring = time.strftime("%a %b %d %H:%M:%S %Y", timestamp) + "\n"
if preview: if preview:
line = fhandle.readline() line = fhandle.readline()
line = line.rstrip() line = line.rstrip()