-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext_editor.rb
More file actions
115 lines (94 loc) · 1.99 KB
/
text_editor.rb
File metadata and controls
115 lines (94 loc) · 1.99 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
class TextEditor
def initialize
@save_file = "text_editor_save.txt"
@text = []
run
end
private
def run
greet
prompt_load
puts
puts 'Type "quit" to stop entering text.'
puts
loop do
line = gets.chomp
break if line == "quit"
@text << line
end
print
prompt_edit
prompt_save
end
def greet
greeting = %q(
_____ _ _____ _____ _______ _______
/ __ \| | |_ _| |_ _| ___\ \ / /_ _|
| / \/| | | | | | | |__ \ V / | |
| | | | | | | | | __| / \ | |
| \__/\| |_____| |_ | | | |___/ /^\ \ | |
\____/\_____/\___/ \_/ \____/\/ \/ \_/
)
puts greeting
puts
end
def prompt_load
input = nil
until input == "N" || input == "L"
puts "Would you like to start a (N)ew file or (L)oad an existing file?"
puts
input = gets.chomp.upcase
end
load if input == "L"
end
def load
if File.exist?(@save_file)
file = File.open(@save_file, "r")
file.each_line {|line| @text << line}
else
puts "No saved file exists. Starting new file."
end
puts
end
def print
puts
puts "You wrote:"
@text.each_with_index {|sentence, line| puts "#{line+1} #{sentence}"}
puts
end
def prompt_edit
puts "Would you like to edit a line? (Y/N)"
puts
response = gets.chomp.upcase
if response == "Y"
line = nil
until (1..@text.length).include? line
puts
puts "Which line would you like to edit?"
line = gets.chomp.to_i
end
edit(line)
end
end
def edit(line)
puts
puts "Was:"
puts @text[line-1]
puts
puts "Enter new text:"
@text[line-1] = gets.chomp
puts
print
end
def prompt_save
puts "Would you like to save? (Y/N)"
puts
input = gets.chomp.upcase
save if input = "Y"
end
def save
file = File.open(@save_file, "w")
file.puts(@text)
end
end
TextEditor.new