-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
599 lines (537 loc) · 14.3 KB
/
main.lua
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
local love = require 'love'
local lib = require 'lib'
Config = {
controls = {
switch = {
normal = 'escape',
insert = 'i',
--command = ':'
},
commands = {
runCommand = 'return',
quit = 'q',
ignoreUnsaved = '!',
read = 'r',
write = 'w',
help = 'h'
},
cursor = {
up = 'up',
down = 'down',
left = 'left',
right = 'right'
},
editor = {
paste = 'p',
copy = 'y',
cut = 'c'
}
},
coloring = {
background = { 0, 0, 0 },
text = { 255, 255, 255 },
lineNumbers = { 200, 200, 200 },
cursor = {
insert = { 240, 240, 240 },
command = { 240, 240, 240 }
}
},
editor = {
showLineNumbers = true,
cursorMoveInterval = 5,
moveOverNewlines = false,
truncateFilepath = true,
hideUser = true,
showCursor = {
insert = true,
command = true
}
}
}
Modes = {
Normal = 1,
Command = 2,
Insert = 3,
Help = 4
}
---@param mode number
---@return string
function GetModeName(mode)
if mode == Modes.Normal then
return "NORMAL"
elseif mode == Modes.Command then
return "COMMAND"
elseif mode == Modes.Insert then
return "INSERT"
elseif mode == Modes.Help then
return "HELP"
end
return "UNKNOWN"
end
KeysToChars = {
['return'] = '\n',
['tab'] = '\t',
['space'] = ' ',
['lshift'] = 0,
['rshift'] = 0,
['lctrl'] = 0,
['rctrl'] = 0,
['lalt'] = 0,
['ralt'] = 0,
['lgui'] = 0,
['rgui'] = 0,
['up'] = 0,
['down'] = 0,
['left'] = 0,
['right'] = 0,
['f1'] = 0,
['f2'] = 0,
['f3'] = 0,
['f4'] = 0,
['f5'] = 0,
['f6'] = 0,
['f7'] = 0,
['f8'] = 0,
['f9'] = 0,
['f10'] = 0,
['f11'] = 0,
['f12'] = 0,
}
ShiftedKeysToChars = {
['1'] = '!',
['2'] = '@',
['3'] = '#',
['4'] = '$',
['5'] = '%',
['6'] = '^',
['7'] = '&',
['8'] = '*',
['9'] = '(',
['0'] = ')',
['a'] = 'A',
['b'] = 'B',
['c'] = 'C',
['d'] = 'D',
['e'] = 'E',
['f'] = 'F',
['g'] = 'G',
['h'] = 'H',
['i'] = 'I',
['j'] = 'J',
['k'] = 'K',
['l'] = 'L',
['m'] = 'M',
['n'] = 'N',
['o'] = 'O',
['p'] = 'P',
['q'] = 'Q',
['r'] = 'R',
['s'] = 'S',
['t'] = 'T',
['u'] = 'U',
['v'] = 'V',
['w'] = 'W',
['x'] = 'X',
['y'] = 'Y',
['z'] = 'Z',
['`'] = '~',
[','] = '<',
['.'] = '>',
['/'] = '?',
['\''] = '"',
[';'] = ':',
['['] = '{',
[']'] = '}',
['\\'] = '|',
['-'] = '_',
['='] = '+'
}
---@param key string
---@param shifted boolean
---@return string | number
function ConvertKeyToChar(key, shifted)
local ch = nil
if shifted then
ch = ShiftedKeysToChars[key]
else
ch = KeysToChars[key]
end
if ch == nil and shifted then
ch = KeysToChars[key]
end
return ch or key
end
---@param s string
---@param splitter string
---@return table
function string.split(s, splitter)
local t = {}
local acc = ''
for i = 1, #s do
local ch = s:sub(i, i)
if ch == splitter then
if acc ~= '' then
t[#t + 1] = acc
acc = ''
end
else
acc = acc .. ch
end
end
if acc ~= '' then
t[#t + 1] = acc
end
return t
end
---@param s string
---@return string
function string.strip(s)
while s:sub(0, 1) == '\n' or s:sub(0, 1) == '\t' or s:sub(0, 1) == ' ' do
s = s:sub(2)
end
while s:sub(#s, #s) == '\n' or s:sub(#s, #s) == '\t' or s:sub(#s, #s) == ' ' do
s = s:sub(0, #s - 1)
end
return s
end
function Scandir(directory)
local t, popen = {}, io.popen
local pfile = popen('ls -a "' .. directory .. '"')
if pfile == nil then
return {}
end
for filename in pfile:lines() do
t[#t + 1] = filename
end
pfile:close()
return t
end
function love.load()
WindowData = {
width = 800,
height = 600
}
love.window.setTitle('Luext')
love.window.setMode(WindowData.width, WindowData.height, {})
MainFont = love.graphics.newFont('0xProtoNerdFontMono-Regular.ttf', 16)
Text = ""
Saved = true
Filename = nil
CursorPos = 0
Selected = { start = -1, length = -1 }
CommandCursorPos = 0
Shifting, MovingLeft, MovingRight = false, false, false
CursorMoveTick = 0
CurrentMode = Modes.Normal
CommandAcc = ""
CommandHistory = {}
CommandHistoryPos = 0
MsgRegister = ""
end
function InsertText(text)
Text = Text:sub(0, CursorPos) .. text .. Text:sub(CursorPos + 1)
CursorPos = CursorPos + #text
end
---@return string
function GetHistory()
return CommandHistory[#CommandHistory - CommandHistoryPos] or ""
end
---@param msg string
function SetMsg(msg)
MsgRegister = msg
end
function ClearMsg()
SetMsg("")
end
function CanCopy()
return Selected.start > -1 and Selected.length > -1
end
function love.update()
if (
MovingLeft or MovingRight and
not (MovingLeft and MovingRight) and
((Text:sub(CursorPos + 1, CursorPos + 1) ~= '\n' and Text:sub(CursorPos - 1, CursorPos - 1) ~= '\n') or Config.editor.moveOverNewlines or CurrentMode == Modes.Command)
) then
if CursorMoveTick == Config.editor.cursorMoveInterval then
if CurrentMode == Modes.Command then
if MovingLeft and CommandCursorPos > 0 then
CommandCursorPos = CommandCursorPos - 1
elseif MovingRight and CommandCursorPos < #CommandAcc then
CommandCursorPos = CommandCursorPos + 1
end
else
CursorPos = lib.Clamp(CursorPos, 0, #Text)
if MovingLeft and CursorPos > 0 then
CursorPos = CursorPos - 1
elseif MovingRight and CursorPos < #Text then
CursorPos = CursorPos + 1
end
end
CursorMoveTick = 0
else
CursorMoveTick = CursorMoveTick + 1
end
else
CursorMoveTick = Config.editor.cursorMoveInterval
end
end
function love.draw()
do
lib.DrawRect("fill", Config.coloring.background, 0, 0, WindowData.width, WindowData.height)
end
if CurrentMode == Modes.Help then
else
do
local x, y, textXOffset = 0, 0, 0
if Config.editor.showLineNumbers then
textXOffset = 28
end
local drewNumberForLine = false
for i = 1, #Text do
local ch = Text:sub(i, i)
if (x * 8) + textXOffset > WindowData.width - 5 or ch == '\n' then
y = y + 1
x = 0
drewNumberForLine = false
end
if ch ~= '\n' then
local revert = lib.SetColor(Config.coloring.text)
love.graphics.print(ch, MainFont, (x * 8) + x + textXOffset, y * 16)
revert()
x = x + 1
end
local zeroes = ""
if y < 100 then
zeroes = "0"
elseif y < 10 then
zeroes = "00"
end
if Config.editor.showLineNumbers and not drewNumberForLine then
local revert = lib.SetColor(Config.coloring.lineNumbers)
love.graphics.print(zeroes .. tostring(y), MainFont, 0, y * 16)
revert()
drewNumberForLine = true
end
if CursorPos == 0 and not Config.editor.showLineNumbers then
lib.DrawRect("fill", Config.coloring.cursor.insert, textXOffset + 5, y * 16, 2,
16) -- draw insert cursor before the first character
elseif i == CursorPos then
lib.DrawRect("fill", Config.coloring.cursor.insert, (x * 8) + x + textXOffset,
y * 16, 2, 16) -- draw insert cursor
end
end
end
lib.DrawRect("fill", { 0, 0, 0 }, 0, WindowData.height - 57, WindowData.width, 38)
love.graphics.print(GetModeName(CurrentMode), MainFont, 2, WindowData.height - 37)
if CurrentMode == Modes.Command then
love.graphics.print(":", MainFont, 2, WindowData.height - 21)
local x = 0
for i = 1, #CommandAcc do
local ch = CommandAcc:sub(i, i)
local revert = lib.SetColor(Config.coloring.text)
love.graphics.print(ch, MainFont, (x * 8) + x + 9, WindowData.height - 21)
revert()
x = x + 1
if CommandCursorPos == 0 then
lib.DrawRect("fill", Config.coloring.cursor.command, 9, WindowData.height - 21, 2,
16) -- draw insert cursor before the first character
elseif i == CommandCursorPos then
lib.DrawRect("fill", Config.coloring.cursor.command, (x * 8) + x + 9,
WindowData.height - 21, 2, 16) -- draw insert cursor
end
end
else
love.graphics.print(MsgRegister, MainFont, 2, WindowData.height - 21)
end
end
end
---@param command string
function ProcessCommand(command)
if command == "" then
return
end
command = command:strip()
if CommandHistory[#CommandHistory] ~= command then
CommandHistory[#CommandHistory + 1] = command
end
local args = command:split(' ')
local com = args[1]
com = com:strip()
for i = 1, #com do
local ch = com:sub(i, i)
if ch == Config.controls.commands.quit then
if not Saved and com:sub(i + 1, i + 1) ~= Config.controls.commands.ignoreUnsaved then
MsgRegister = "Unsaved changes"
return
end
love.event.quit()
elseif ch == Config.controls.commands.read then
if not Saved and com:sub(i + 1, i + 1) ~= Config.controls.commands.ignoreUnsaved then
MsgRegister = "Unsaved changes"
return
end
if #args > 1 then
Filename = lib.ResolvePath(args[2])
end
if Filename == nil or Filename == '' then
MsgRegister = "No filename given"
return
end
local file = io.open(Filename, 'r')
if file == nil then
MsgRegister = "Failed to open file '" ..
lib.FormatPath(Filename, Config.editor.truncateFilepath, Config.editor.hideUser) .. "'"
return
end
Text = file:read('a')
file:close()
ClearMsg()
Saved = true
elseif ch == Config.controls.commands.write then
if #args > 1 then
Filename = lib.ResolvePath(args[2])
end
if Filename == nil or Filename == '' then
MsgRegister = "No filename given"
return
end
local file = io.open(Filename, "w")
if file == nil then
MsgRegister = "Failed to open file '" ..
lib.FormatPath(Filename, Config.editor.truncateFilepath, Config.editor.hideUser) .. "'"
return
end
file:write(Text)
file:close()
Saved = true
MsgRegister = "Saved as '" ..
lib.FormatPath(Filename, Config.editor.truncateFilepath, Config.editor.hideUser) .. "'"
elseif ch == Config.controls.commands.help then
CurrentMode = Modes.Help
return
else
MsgRegister = "'" .. ch .. "' is not a command"
return
end
end
end
function love.keypressed(k)
if k == 'lshift' or k == 'rshift' then
Shifting = true
end
--print(k, Shifting, CurrentMode)
if CurrentMode == Modes.Normal then
if k == ';' and Shifting then
CurrentMode = Modes.Command
elseif k == Config.controls.switch.insert then
CurrentMode = Modes.Insert
elseif k == Config.controls.cursor.up then
local s, _, _ = lib.GetCurrentLine(Text, CursorPos)
local linePos = CursorPos - s
if s > 0 then
CursorPos = s - 2
local s2, _, _ = lib.GetCurrentLine(Text, CursorPos)
CursorPos = s2 + linePos
end
elseif k == Config.controls.cursor.down then
local s, e, _ = lib.GetCurrentLine(Text, CursorPos)
local linePos = CursorPos - s
if e < #Text then
CursorPos = e + linePos + 1
end
elseif k == Config.controls.cursor.left then
MovingLeft = true
elseif k == Config.controls.cursor.right then
MovingRight = true
elseif k == Config.controls.editor.paste then
local clipboard = love.system.getClipboardText()
if clipboard ~= '' and clipboard ~= nil then
InsertText(clipboard)
end
end
elseif CurrentMode == Modes.Command then
local converted = ConvertKeyToChar(k, Shifting)
if k == Config.controls.switch.normal then
CommandAcc = ""
CurrentMode = Modes.Normal
--[[
elseif k == Config.controls.cursor.up then
if CommandHistoryPos < #CommandHistory then
CommandHistoryPos = CommandHistoryPos + 1
if CommandHistoryPos > #CommandHistory then
CommandHistoryPos = 0
end
end
CommandAcc = GetHistory()
elseif k == Config.controls.cursor.down then
if CommandHistoryPos > 0 then
CommandHistoryPos = CommandHistoryPos - 1
end
CommandAcc = GetHistory()
]]
elseif k == Config.controls.cursor.left then
MovingLeft = true
elseif k == Config.controls.cursor.right then
MovingRight = true
elseif k == Config.controls.commands.runCommand then
ProcessCommand(CommandAcc)
CommandAcc = ""
CurrentMode = Modes.Normal
elseif k == 'backspace' then
if #CommandAcc > 0 then
CommandAcc = CommandAcc:sub(0, #CommandAcc - 1)
CommandCursorPos = CommandCursorPos - 1
end
elseif converted ~= ':' then
if type(converted) == "string" then
CommandAcc = CommandAcc:sub(0, CommandCursorPos) ..
converted .. CommandAcc:sub(CommandCursorPos + 1)
CommandCursorPos = CommandCursorPos + 1
end
end
elseif CurrentMode == Modes.Insert then
if k == Config.controls.switch.normal then
CurrentMode = Modes.Normal
elseif k == 'backspace' then
if #Text > 0 then
CursorPos = CursorPos - 1
Text = Text:sub(0, CursorPos) .. Text:sub(CursorPos + 2)
end
elseif k == Config.controls.cursor.up then
local s, e, _ = lib.GetCurrentLine(Text, CursorPos)
local linePos = e - CursorPos
if s ~= 0 then
CursorPos = (s - 2) - linePos
end
elseif k == Config.controls.cursor.up then
local s, e, _ = lib.GetCurrentLine(Text, CursorPos)
local linePos = e - CursorPos
if s ~= 0 then
CursorPos = (s - 2) - linePos
end
elseif k == Config.controls.cursor.left then
MovingLeft = true
elseif k == Config.controls.cursor.right then
MovingRight = true
else
local converted = ConvertKeyToChar(k, Shifting)
Saved = false
if type(converted) == "string" then
InsertText(converted)
end
end
elseif CurrentMode == Modes.Help then
if k == Config.controls.switch.normal then
CurrentMode = Modes.Normal
end
end
end
function love.keyreleased(k)
if k == 'lshift' or k == 'rshift' then
Shifting = false
elseif k == Config.controls.cursor.left then
MovingLeft = false
elseif k == Config.controls.cursor.right then
MovingRight = false
end
end