-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rb
More file actions
109 lines (98 loc) · 3.47 KB
/
main.rb
File metadata and controls
109 lines (98 loc) · 3.47 KB
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
# frozen_string_literal: true
require 'bundler/setup'
require 'artii'
require 'colorize'
require 'prawn'
require 'tty-prompt'
require_relative 'journal'
require_relative 'journal_manager'
def show_menu
slant_font = Artii::Base.new font: 'slant'
doom_font = Artii::Base.new font: 'doom'
manager = JournalManager.new
prompt = TTY::Prompt.new(active_color: :green)
puts slant_font.asciify('ALIEN JOURNAL').light_green
option = prompt.select('Would like to start a new journal, load your existing journal?',
['New Journal', 'Load existing Journal']).downcase
case option
when 'new journal'
print 'What would you like to name your journal? '.light_green
journal_name = gets.chomp
# This is very explicit, but it's important my program knows which files to look for internall.
# txt seems a simple choice for this for now.
if journal_name.empty?
journal = manager.load_journal('My Journal.txt')
else
journal_name += '.txt' unless journal_name.end_with?('.txt')
journal = manager.load_journal(journal_name)
end
when 'load existing journal'
journal = manager.swap_journal
end
loop do
choice = prompt.select('Please select action',
['Write new entry', 'View all entries', 'Search entries', 'Edit entry', 'Delete entry', 'Export journal to file', 'Load different journal',
'Quit']).downcase
case choice
when 'write new entry'
puts "Please select a title for entry if you'd like one(Press Enter to skip): ".light_green
title = gets.chomp
puts 'What is on your mind today? '.light_green
body = gets.chomp
entry = Entry.new(body, title)
journal.add_entry(entry)
puts 'Entry saved, survivor'.green
when 'view all entries'
journal.display_all
when 'search entries'
print 'Search for: '.light_green
query = gets.chomp
search_query(journal, query)
when 'edit entry'
print 'Search for query to edit: '.light_green
query = gets.chomp
results = search_query(journal, query)
if results.any?
print "which entry to edit? (1-#{results.length}): ".light_green
choice = gets.chomp.to_i - 1
# need to delete at the entry index and add the edit to the same index
journal.edit_entry(results[choice])
puts 'Here is your edited entry: '.light_green
results[choice].display
end
when 'delete entry'
print 'Search for entry to delete: '.light_green
query = gets.chomp
results = search_query(journal, query)
if results.any?
print "Which entry to delete? (1-#{results.length}): ".light_green
choice = gets.chomp.to_i - 1
journal.delete_entry(results[choice])
puts 'Deleted!'.light_green.bold
end
when 'export journal to file'
format = prompt.select('Please select format', ['PDF', 'Markdown', 'Plaintext(txt)']).downcase
journal.export(format)
when 'load different journal'
journal = manager.swap_journal
when 'quit'
journal.save_to_file
puts doom_font.asciify('YOU SURVIVED ONE MORE DAY').light_green
exit(0)
end
end
end
def search_query(journal, query)
results = journal.search(query)
if results.empty?
puts 'No matches found.'.red
else
# Display with numbers
results.each_with_index do |entry, index|
puts "\n--- Entry #{index + 1} ---".light_green
entry.display
end
end
results
end
show_menu