-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.rb
97 lines (62 loc) · 1.66 KB
/
editor.rb
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
require 'io/console'
class Editor
def initialize
puts "(1) Open File"
puts "(2) Read File"
selection = gets.chomp
if(selection == "1")
open_file
elsif(selection == "2")
read_file
end
end
def open_file
text = []
puts "Enter file name: "
name = gets.chomp
File.readlines(name).each do |line|
text << line
end
system 'cls'
for i in 1..text.length
puts "#{i} #{text[i]}"
end
edit(name, text)
end
def read_file
puts "Enter file name: "
name = gets.chomp
file = File.open(name, "r")
puts file.read
end
def edit(name, text)
passes = 0
loop do
print "#{text.length + 1} "
line = gets.chomp
break if line == "quit"
text = edit_mode(text) if line == "ed"
if line == "ed"
line = ""
end
text << line
passes += 1
end
save(text, name)
end
def save(text, name)
file = File.open(name, "w")
file.puts(text)
end
def edit_mode(text)
line_num = gets.chomp
text[line_num.to_i] = gets.chomp
system 'cls'
puts "*********************************************************************************************************"
for i in 1..text.length
puts "#{i} #{text[i]}"
end
return text
end
end
Editor.new