-
Notifications
You must be signed in to change notification settings - Fork 2
/
femto.rb
executable file
·421 lines (323 loc) · 7.79 KB
/
femto.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/usr/bin/env ruby
require 'io/console'
unless Comparable.instance_methods.include?(:clamp)
class Fixnum
def clamp(min, max)
if min > max
raise ArgumentError, 'min argument must be smaller than max argument'
end
return min if self <= min
return max if self >= max
self
end
end
end
module Femto
class Editor
def initialize(filename)
@filename = filename
data = read_file_data
@line_sep = data["\r\n"] || "\n"
@buffer = Buffer.new(lines_from_data(data))
@cursor = Cursor.new
@history = History.new
end
def self.open(filename)
new(filename).run
end
def run
IO.console.raw do
reset_screen
loop do
render
handle_input
end
end
end
private
attr_reader :buffer, :blank_buffer, :cursor, :history, :line_sep, :filename
def render
clear_screen
print buffer
ANSI.move_cursor(cursor.row, cursor.col)
end
def handle_input
char = read_char
case char
when "\cq" then quit
when "\cs" then save
when "\cp", "\e[A" then up
when "\cn", "\e[B" then down
when "\cf", "\e[C" then right
when "\cb", "\e[D" then left
when "\ca", "\e[7~" then line_home
when "\ce", "\e[8~" then line_end
when "\ch", "\177" then backspace
when "\cd", "\e[3~", "\004" then delete
when "\cu" then delete_before
when "\ck" then delete_after
when "\c_" then history_undo
when "\cr" then history_redo
when "\r" then enter
else
insert_char(char) if char =~ /[[:print:]]/
end
end
def read_char
char = $stdin.getc
return char if char != "\e"
maxlen = 3
begin
char << $stdin.read_nonblock(maxlen)
rescue IO::WaitReadable
return char if maxlen == 2
maxlen -= 1
retry
end
char
end
def quit
reset_screen
exit
end
def up
@cursor = cursor.up(buffer)
end
def down
@cursor = cursor.down(buffer)
end
def right
@cursor = cursor.right(buffer)
end
def left
@cursor = cursor.left(buffer)
end
def backspace
return if cursor.beginning_of_file?
store_snapshot
if cursor.col == 0
cursor_left = buffer.lines[cursor.row].size + 1
@buffer = buffer.join_lines(cursor.row - 1)
cursor_left.times { @cursor = cursor.left(buffer) }
else
@buffer = buffer.delete_char(cursor.row, cursor.col - 1)
@cursor = cursor.left(buffer)
end
end
def delete
return if cursor.end_of_file?(buffer)
store_snapshot
if cursor.end_of_line?(buffer)
@buffer = buffer.join_lines(cursor.row)
else
@buffer = buffer.delete_char(cursor.row, cursor.col)
end
end
def data
data = buffer.lines.join(line_sep).chomp(line_sep)
data << line_sep unless data.empty?
data
end
def save
open(filename, 'w') {|f| f << data }
end
def enter
store_snapshot
@buffer = buffer.break_line(cursor.row, cursor.col)
@cursor = cursor.enter(buffer)
end
def history_undo
return unless history.can_undo?
store_snapshot(false) unless history.can_redo?
@buffer, @cursor = history.undo
end
def history_redo
return unless history.can_redo?
@buffer, @cursor = history.redo
end
def insert_char(char)
store_snapshot
@buffer = buffer.insert_char(char, cursor.row, cursor.col)
@cursor = cursor.right(buffer)
end
def store_snapshot(advance = true)
history.save([buffer, cursor], advance)
end
def line_home
@cursor = cursor.line_home
end
def line_end
@cursor = cursor.line_end(buffer)
end
def delete_before
store_snapshot
@buffer = buffer.delete_before(cursor.row, cursor.col)
line_home
end
def delete_after
store_snapshot
@buffer = buffer.delete_after(cursor.row, cursor.col)
end
def reset_screen
ANSI.move_cursor(0, 0)
ANSI.clear_screen
end
def clear_screen
ANSI.move_cursor(0, 0)
if blank_buffer
print blank_buffer # overwrite screen with spaces
ANSI.move_cursor(0, 0)
end
blank_lines = buffer.lines.map {|line| ' ' * line.size }
@blank_buffer = Buffer.new(blank_lines)
end
def read_file_data
if File.exist?(filename)
File.read(filename)
else
''
end
end
def lines_from_data(data)
if data.empty?
['']
else
data.split(line_sep)
end
end
end
class Buffer
attr_reader :lines
def initialize(lines)
@lines = lines
end
def to_s
lines.map {|line| "#{line}\r\n" }.join
end
def lines_count
lines.size
end
def line_length(row)
lines[row].size
end
def delete_char(row, col)
with_copy {|b| b.lines[row].slice!(col) }
end
def insert_char(char, row, col)
with_copy {|b| b.lines[row].insert(col, char) }
end
def break_line(row, col)
with_copy do |b|
b.lines[row..row] = [b.lines[row][0...col], b.lines[row][col..-1]]
end
end
def delete_before(row, col)
with_copy {|b| b.lines[row][0...col] = '' }
end
def delete_after(row, col)
with_copy {|b| b.lines[row][col..-1] = '' }
end
def join_lines(row)
with_copy {|b| b.lines[row..row + 1] = b.lines[row..row + 1].join }
end
private
def with_copy
Buffer.new(lines.map(&:dup)).tap {|b| yield b }
end
end
class Cursor
attr_reader :row, :col
def initialize(row = 0, col = 0)
@row = row
@col = col
end
def up(buffer)
Cursor.new(row - 1, col).clamp(buffer)
end
def down(buffer)
Cursor.new(row + 1, col).clamp(buffer)
end
def right(buffer)
return Cursor.new(row, col + 1) unless end_of_line?(buffer)
return self if final_line?(buffer)
Cursor.new(row + 1, 0)
end
def left(buffer)
return Cursor.new(row, col - 1) if col > 0
return self if row == 0
Cursor.new(row - 1, buffer.line_length(row - 1))
end
def clamp(buffer)
@row = row.clamp(0, buffer.lines_count - 1)
@col = col.clamp(0, buffer.line_length(row))
self
end
def enter(buffer)
down(buffer).line_home
end
def line_home
Cursor.new(row, 0)
end
def line_end(buffer)
Cursor.new(row, buffer.line_length(row))
end
def end_of_line?(buffer)
col == buffer.line_length(row)
end
def final_line?(buffer)
row == buffer.lines_count - 1
end
def end_of_file?(buffer)
final_line?(buffer) && end_of_line?(buffer)
end
def beginning_of_file?
row == 0 && col == 0
end
end
class History
def initialize
@snapshots = []
@current = -1
end
def save(data, advance = true)
snapshots.slice!(current + 1..-1) # branching; purge redo history
snapshots << data
@current += 1 if advance
end
def can_undo?
!undo_snapshot.nil?
end
def undo
undo_snapshot.tap { @current -= 1 }
end
def can_redo?
!redo_snapshot.nil?
end
def redo
redo_snapshot.tap { @current += 1 }
end
private
attr_reader :snapshots, :current
def undo_snapshot
snapshots[current] if current >= 0
end
def redo_snapshot
snapshots[current + 2]
end
end
module ANSI
def self.clear_screen
print "\e[J"
end
def self.move_cursor(row, col)
print "\e[#{row + 1};#{col + 1}H"
end
end
end
if __FILE__ == $0
begin
Femto::Editor.open(ARGV.fetch(0))
rescue IndexError
puts "Usage: #$0 file"
end
end