-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjournal.rb
More file actions
171 lines (145 loc) · 4.54 KB
/
journal.rb
File metadata and controls
171 lines (145 loc) · 4.54 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
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
# frozen_string_literal: true
require_relative 'entry'
# Journal managers the many entires from Entry, sorting them together into a Journal
# it can save, load and even export journals and entries
class Journal
attr_reader :entries, :filename
def initialize(filename)
@filename = filename
@entries = []
end
def add_entry(entry)
# Takes an Entry object, adds it to @entries array.
@entries << entry
end
def save_to_file
Dir.mkdir('data') unless Dir.exist?('data')
File.open(File.join('data', @filename), 'w') do |file|
@entries.each do |entry|
line = "#{entry.date}|#{entry.title}|#{entry.body}\n===END_ENTRY===\n"
file.write(line)
end
end
rescue StandardError => e
puts "Couldn't save: #{e.message}".red
end
def load_from_file
return unless File.exist?("data/#{@filename}")
file = File.read("data/#{@filename}")
# For each line:
entries = file.split("===END_ENTRY===\n")
# Split by "|" to get date, title, body
entries.each do |entry|
next if entry.strip.empty?
parts = entry.split('|')
date = parts[0]
title = parts[1]
body = parts[2]
# Create Entry object
entry = Entry.new(body, title, date)
# Add to @entries array
@entries << entry
end
rescue StandardError => e
puts "Could not find file #{e.message}".red
end
def display_all
@entries.each(&:display)
end
def search(term)
# search take the users input and looks for what they typed.
# They can search for a sentence, a data, a title etc.
# This may need to display researchs in order if there are a few.
matches = []
search_term = term.downcase
@entries.each do |entry|
date_match = entry.date.downcase.include?(search_term)
title_match = entry.title.downcase.include?(search_term)
body_match = entry.body.downcase.include?(search_term)
matches << entry if date_match || title_match || body_match
end
matches
end
def edit_entry(entry)
puts 'If you do not wish to change following questions, simply click enter'.light_green
print "New title?(current #{entry.title} ".light_green
new_title = gets.chomp
print "New body? (current #{entry.body}) ".light_green
new_body = gets.chomp
entry.title = new_title unless new_title.empty?
entry.body = new_body unless new_body.empty?
end
def delete_entry(entry)
# I think delete_entry needs to take in the exact entry name for certainty
@entries.delete(entry)
end
def export(format)
base_name = File.basename(@filename, '.*')
case format
when 'plaintext(txt)'
actual_format = 'txt'
export_as_plain_text("#{base_name}_exported.#{actual_format}")
when 'markdown'
actual_format = 'md'
export_as_markdown("#{base_name}_exported.#{actual_format}")
when 'pdf'
export_as_pdf("#{base_name}_exported.pdf")
else
puts "Unknown format: #{format}".red
end
end
private
def export_as_plain_text(filename)
line_width = 80
File.open(filename, 'w') do |file|
@entries.each do |entry|
spaces = line_width - entry.date.length - entry.title.length
separator = '* * * * *'
spaces_before_stars = (line_width - separator.length) / 2
centered_stars = "#{' ' * spaces_before_stars}#{separator}"
header = "#{entry.date}#{' ' * spaces}#{entry.title}"
output = <<~ENTRY
#{header}
#{entry.body}
#{centered_stars}
ENTRY
file.write(output)
end
end
puts "Exported to #{filename}!".green
rescue StandardError => e
puts "Export failed: #{e.message}".red
end
def export_as_markdown(filename)
File.open(filename, 'w') do |file|
@entries.each do |entry|
markdown = <<~MD
## #{entry.title}
*#{entry.date}*
#{entry.body}
---
MD
file.write(markdown)
end
end
puts "Exported to #{filename}!".green
rescue StandardError => e
puts "Export failed #{e.messgae}".red
end
def export_as_pdf(filename)
Prawn::Document.generate(filename) do |pdf|
@entries.each do |entry|
pdf.text entry.title, size: 18, style: :bold
pdf.text entry.date, size: 10, style: :italic
pdf.move_down 10
pdf.text entry.body
pdf.move_down 10
pdf.text '-' * 50 # ← Use regular hyphen, not em-dash!
pdf.move_down 20
end
end
puts "Exported to #{filename}!".green
rescue StandardError => e
puts "Export failed: #{e.message}".red
end
end