-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.rb
238 lines (196 loc) · 4.99 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
require 'curses'
class Buffer
attr_accessor :data
def initialize
@data = []
end
def add_line(line)
self.data << line
end
def set_line(line, line_number)
self.data[line_number] = line
end
def at(number)
data[number]
end
def insert_new_line(y, x)
old_line = at(y)
line1, line2 = '', nil
if(x != 0)
line1 = old_line[0...x]
line2 = old_line[x..old_line.size]
data.insert(y, line1)
set_line(line2, y + 1)
else
data.insert(y, line1)
end
end
end
class Editor
attr_accessor :offset, :buffer
NEW_LINE = 10
LEFT = Curses::Key::LEFT
RIGHT = Curses::Key::RIGHT
UP = 259
DOWN = Curses::Key::DOWN
BACKSPACE = 127
DC = Curses::Key::DC
def initialize
@offset = 0
end
def main_window
@main_window ||= Window.new
end
def initialize_app
Curses.init_screen()
Curses.start_color()
Curses.init_pair(1, Curses::COLOR_WHITE, Curses::COLOR_BLACK)
Curses.init_pair(2, Curses::COLOR_YELLOW, Curses::COLOR_BLACK)
Curses.init_pair(3, Curses::COLOR_GREEN, Curses::COLOR_BLACK)
Curses.init_pair(4, Curses::COLOR_CYAN, Curses::COLOR_BLACK)
Curses.noecho()
main_window.color_on(1)
main_window.buffer = Buffer.new
end
def load_file
return puts "No file specified" if(ARGV.first.nil?)
file = File.open(ARGV.first, 'r')
while (l = file.gets)
main_window.buffer.add_line(l.split("\n").first)
end
end
def run
initialize_app
load_file
main_window.redraw_screen
while 1
key_input = main_window.get_character
case key_input
when NEW_LINE
main_window.handle_return_key(key_input)
when LEFT
main_window.move(:left)
when RIGHT
main_window.move(:right)
when 259
main_window.move(:up)
when DOWN
main_window.move(:down)
when DC
#main_window.setpos(main_window.cury, current_x - 1)
when BACKSPACE
main_window.backspace
else
main_window.insert_text(key_input)
end
end
main_window.close
end
end
class Window
attr_accessor :main_window, :buffer, :previous_x, :previous_y, :debug_line
def initialize
self.main_window = Curses::Window.new(0, 0, 0, 0)
main_window.keypad(true)
end
def handle_return_key(key_input)
if key_input == 10
buffer.insert_new_line(main_window.cury, current_x)
old_pos = [main_window.cury, current_x]
redraw_screen
main_window.setpos(old_pos.first + 1, old_pos.last)
end
end
def get_character
main_window.getch
end
def current_x
main_window.curx
end
def current_y
main_window.cury
end
alias :line_number :current_y
def next_line
buffer[current_y + 1]
end
def previous_line
buffer[current_y - 2]
end
def current_line
buffer[current_y - 1]
end
def color_on(value)
main_window.attron(Curses.color_pair(value))
end
def move(direction)
case direction
when :left
current_x < 2 ? set_position(0, line_number) : set_position(current_x - 1, line_number)
when :right
set_position(current_x + 1, current_y)
when :up
current_y < 2 ? set_position(current_x, 0) : set_position(current_x, current_y - 1)
when :down
current_y > 52 ? set_position(current_x, 52) : set_position(current_x, current_y + 1)
end
write_status_bar
end
def backspace
if current_x-1 >= 0
buffer[main_window.cury-1][current_x-1] = ''
old_pos = [main_window.cury, current_x]
main_window.setpos(main_window.cury, 0)
main_window.clrtoeol
main_window.addstr(buffer[main_window.cury - 1])
main_window.setpos(old_pos.first, old_pos.last- 1)
end
end
def insert_text(text)
old_pos = [current_x, current_y]
old_string = buffer.at(current_y)
old_string += " " * (current_x - old_string.size) if(old_string.size < current_x)
new_string = old_string.insert(current_x, text.to_s)
buffer.set_line(new_string, current_y)
redraw_screen
set_position(old_pos.first + 1, old_pos.last)
end
def redraw_screen
main_window.clear
visible_lines = (0...52)
old_pos = [current_x, current_y]
visible_lines.each do |num|
writeln(buffer.at(current_y))
move_to_next_line
main_window.clrtoeol
end
write_status_bar
set_position(old_pos.first, old_pos.last)
end
def move_to_next_line
@lines ||= []
@lines << current_y
set_position(0, current_y + 1)
end
def set_position(x, y)
self.previous_x = current_x
self.previous_y = current_y
main_window.setpos(y, x)
end
def set_to_previous_position
set_position(previous_x, previous_y)
end
private
def writeln(line)
return if line == nil
main_window.addstr(line)
end
def write_status_bar
# set_position(0, 54)
# main_window.clrtoeol
# writeln("Line: #{previous_y + 1} | Column: #{previous_x + 1} | Debug: '#{debug_line}'... ")
# set_to_previous_position
# self.debug_line = ""
end
end
Editor.new.run