~fancycade/tox.cr

tox.cr/editor.cr -rw-r--r-- 7.0 KiB
320a7a13 — fancycade Changed scripts to commands + no-debug for release 2 years ago
                                                                                
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
# Fancy Cade hereby disclaims all copyright interest in the program (which edits text in the terminal) written by fancycade.

# fancycade 2019

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

require "termbox"

include Termbox

class Editor
  @end_row : Int32 # Or else compiler complains

  def initialize
    @w = Window.new
    @rows = [] of Array(Char)
    @start_row = 0
    @end_row = @w.height - 1
    @cursor_x = 0
    @cursor_y = 0
    @path = ""
    @copy_buffer = [] of Char
  end

  def read_file(path : String)
    @path = path
    if File.exists? path
      i = 0
      @rows << [] of Char
      File.read(path).chars.each do |c|
        @rows[i] << c

        if c == '\n'
          @rows << [] of Char
          i += 1
        end
      end

      @rows.pop # Remove extra row
    else
      @rows << ['\n']
    end
  end

  def run
    loop do
      ev = @w.poll

      if ev.type == Termbox::EVENT_KEY
        if ev.key == 0
          insert_char(ev.ch.chr)
        else
          case ev.key
          when Termbox::KEY_CTRL_Q
            break
          when Termbox::KEY_ARROW_LEFT
            cursor_left()
          when Termbox::KEY_ARROW_RIGHT
            cursor_right()
          when Termbox::KEY_ARROW_DOWN
            cursor_down()
          when Termbox::KEY_ARROW_UP
            cursor_up()
          when Termbox::KEY_SPACE
            insert_char(' ')
          when Termbox::KEY_TAB
            insert_char('\t')
          when Termbox::KEY_BACKSPACE2
            remove_char()
          when Termbox::KEY_ENTER
            insert_new_line()
          when Termbox::KEY_CTRL_S
            save_file()
          when Termbox::KEY_CTRL_A
            cursor_start_of_row()
          when Termbox::KEY_CTRL_E
            cursor_end_of_row()
          when Termbox::KEY_CTRL_Z
            screen_up()
          when Termbox::KEY_CTRL_V
            screen_down()
          when Termbox::KEY_CTRL_K
            kill_line()
          when Termbox::KEY_CTRL_Y
            yank_line()
          else
          end
        end
      elsif ev.type == 2 # Resize
      end

      render()
    end

    @w.shutdown
  end

  def render_row(row : Array(Char), y : Int32)
    temp_x = 0

    row.each_with_index do |c, i|
      break if i == @w.width

      if c != '\0'
        if c == '\t'
          @w.put_raw(Cell.new(' ', Position.new(temp_x, y), COLOR_DEFAULT, COLOR_GREEN))
          temp_x += 8
        elsif c == '\n'
          # @w.put_raw(Cell.new(' ', Position.new(temp_x, y), COLOR_DEFAULT, COLOR_BLUE))
        else
          @w.put_raw(Cell.new(c, Position.new(temp_x, y), COLOR_DEFAULT, COLOR_DEFAULT))
        end
      end

      temp_x += 1
    end
  end

  def render_cursor
    temp_x = 0
    y = @start_row + @cursor_y
    row = @rows[y]
    row.each_with_index do |c, i|
      break if i == @cursor_x
      temp_x += 8 if c == '\t'
      temp_x += 1
    end
    @w.cursor(Position.new(temp_x, @cursor_y))
  end

  def render
    @w.clear
    @w.set_output_mode(OUTPUT_NORMAL)
    y = 0
    loop do
      break if y == @w.height || y == @rows.size
      row = @rows[@start_row + y]
      render_row(row, y)
      y += 1
    end
    render_cursor()
    @w.render
  end

  def cursor_left
    if @cursor_x - 1 >= 0
      @cursor_x -= 1
    end
  end

  def cursor_right
    row = @rows[@start_row + @cursor_y]
    if @cursor_x + 1 < row.size
      @cursor_x += 1
    end
  end

  def cursor_down
    y = @cursor_y + 1
    if @start_row + y < @rows.size
      if y < @w.height
        @cursor_y += 1
      elsif y == @w.height
        if @rows.size - @start_row > @w.height
          @start_row += 1
          @end_row += 1
        end
      end
      cursor_end_of_row()
    end
  end

  def cursor_up
    if @start_row + @cursor_y > 0
      y = @cursor_y - 1
      if y < 0
        @start_row -= 1
      else
        @cursor_y -= 1
      end
      cursor_end_of_row()
    end
  end

  def cursor_end_of_row
    row = @rows[@start_row + @cursor_y]
    @cursor_x = row.size - 1
  end

  def cursor_start_of_row
    @cursor_x = 0
  end

  def insert_char(c)
    if @rows.empty?
      @rows << [c]
    else
      row = @rows[@start_row + @cursor_y]
      row.insert(@cursor_x, c)
    end
    cursor_right()
  end

  def remove_char
    y = @start_row + @cursor_y
    row = @rows[y]

    if @cursor_x > 0
      row.delete_at(@cursor_x - 1)
      cursor_left()

      if row.empty?
        @rows.delete_at(y)
        cursor_up()
      end
    elsif @cursor_x == 0 && y > 0
      row2 = @rows[y - 1]
      row2.pop
      row2 = row2.concat(row)
      @rows.delete_at(y)
      if @start_row > 0
        @start_row -= 1
        cursor_end_of_row()
      else
        cursor_up()
      end
    end
  end

  def insert_new_line
    row = @rows[@start_row + @cursor_y]
    if @cursor_x == row.size - 1
      @rows.insert(@start_row + @cursor_y + 1, ['\n'])
    else
      sub_row = row[0..@cursor_x - 1]
      sub_row << '\n'
      sub_row2 = row[@cursor_x..-1]
      @rows[@start_row + @cursor_y] = sub_row
      @rows.insert(@start_row + @cursor_y + 1, sub_row2)
    end
    cursor_down()
  end

  def save_file
    File.write(@path, @rows.flatten.join)
  end

  def screen_down
    if @end_row < @rows.size - 1
      new_end = @end_row + @w.height
      if new_end > @rows.size
        @start_row = @end_row + (@rows.size - new_end)
      else
        @start_row = @end_row
      end

      @end_row = @start_row + @w.height - 1
      if @end_row >= @rows.size
        @end_row = @rows.size - 1
      end

      @cursor_y = 0
      cursor_end_of_row()
    end
  end

  def screen_up
    if @start_row + @cursor_y > 0
      new_start = @start_row - @w.height
      if new_start > 0
        @start_row = new_start
      else
        @start_row = 0
      end
      @end_row = @start_row + @w.height - 1
      cursor_end_of_row()
    end
  end

  def kill_line
    row = @rows[@start_row + @cursor_y]
    @copy_buffer = row[@cursor_x..row.size]
    row.delete_at(@cursor_x..row.size)
    if row.empty?
      @rows.delete_at(@start_row + @cursor_y)
      if @start_row > 0
        @start_row -= 1
        cursor_end_of_row()
      else
        cursor_up()
      end
    else
      row << '\n'
      cursor_end_of_row()
    end
  end

  def yank_line
    row = @rows[@start_row + @cursor_y]
    row.pop
    @copy_buffer.each do |c|
      row << c
    end
    cursor_end_of_row()
  end
end

if ARGV.empty?
  raise "Missing filepath"
else
  e = Editor.new

  e.read_file(ARGV[0])
  e.cursor_end_of_row
  e.render

  e.run
end