-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
92 lines (80 loc) · 2.58 KB
/
main.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
import arrow
import os.path
from os import path
import json
import entry_db
import journal_entry
from edit import EditDisplay
from io import StringIO
import urwid
def menu(self):
db = entry_db.EntryDB("entries.db")
print(greeting(db))
while True:
print("1. Make Entry\n2. Browse\n3. Quit")
select = input()
if select == "1":
input_text = multi_input("How are you feeling today?")
entry = journal_entry.JournalEntry(input_text)
db.add_entry(entry)
db.flush_to_disk()
elif select == "2":
browse_menu(db)
break
elif select == "3":
return quit()
def browse_menu(self,db):
while True:
print("What entries would you like to view?")
browse_select = input("1. Browse all\n2. Text Search \n3. Date Search\n4. Back")
if browse_select == "1":
display_entries(db.get_all_entries())
elif browse_select == "2":
display_entries(search(db))
elif browse_select == "3":
display_entries(search_date(db))
elif browse_select == "4":
menu()
def display_entries(self,entries):
for entry in entries:
if entry.creation_date().day == arrow.now().day:
print(f"{entry.creation_date().humanize()} {entry.content()}")
else:
print(f"{entry.creation_date().format('YYYY-MM-DD HH:mm:ss')} {entry.content()}")
def search_date(self,db):
usr_date = input("Enter day (MM-DD-YY):")
usr_date = arrow.get(usr_date, 'MM-DD-YY')
return db.search_date(usr_date.floor('day'),usr_date.ceil('day'))
def search(self,db):
term = input("Search:")
return db.search(term)
def multi_input(self,prompt=None):
#custom input function for multi-line entries
if prompt:
print(prompt)
content = []
while True:
try:
line = input()
content.append(line)
except EOFError:
return "/n".join(content)
class SaveHandler():
def __init__(self,entry,editor):
self.entry = entry
self.editor = editor
def unhandled_keypress(self,k):
if k == "f5":
self.entry.set_content(self.editor.get_text())
else:
self.editor.unhandled_keypress(k)
def main():
db = entry_db.EntryDB("entries.db")
entry = db.get_all_entries()[0]
editor = EditDisplay(StringIO(entry.content()))
handler = SaveHandler(entry,editor)
loop = urwid.MainLoop(editor.view, editor.palette,
unhandled_input=handler.unhandled_keypress)
loop.run()
db.flush_to_disk()
main()