-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
321 lines (290 loc) · 7.5 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
--[[
SLATE
scrapSavage's Lovely Advanced Text Editor
Changing this file with auto updates enabled will overwrite changes.
]]--
poke(0x4000, get(fetch("/system/fonts/lil_mono.font"))) -- set font
local gui
local code_editor
local m = {x=0,y=0,b=0}
local focused_file = 1
local open_files = {}
local focused = false
local tab_x_offset = 38
window({
x=90,
y=35,
width=300,
height=200
})
button_gfx = {
save=userdata"[gfx]08080707700007077070070000700777777007077770070777700777777000000000[/gfx]",
run=userdata"[gfx]08080700000007770000077777000777777007777700077700000700000000000000[/gfx]",
term=userdata"[gfx]08080777777007000070070000700700007007777770000770000777777000000000[/gfx]",
nav=userdata"[gfx]08080000000000007770077777700777777007777770077777700777777000000000[/gfx]"
}
local save_button
local run_button
local term_button
local nav_button
menuitem({
id=0,
label="\^:386C603010001000 Help",
action=function()
local help = fetch("https://raw.githubusercontent.com/scrapSavage/SLATE/main/help.txt")
store("help.txt",help,{})
add(open_files,{path=pwd().."/help.txt",name="help.txt",state=fetch("help.txt"),dec="",saved=true})
set_active_tab(#open_files)
end
})
function close_tab(idx)
if idx==focused_file then
if focused_file-1==0 then
if #open_files<2 then
exit()
else
deli(open_files,focused_file)
focused_file=1
code_editor.syntax_highlighting = (string.sub(open_files[focused_file].name,-4)!=".txt")
code_editor:set_text(open_files[focused_file].state)
end
else
deli(open_files,focused_file)
focused_file-=1
code_editor.syntax_highlighting = (string.sub(open_files[focused_file].name,-4)!=".txt")
code_editor:set_text(open_files[focused_file].state)
end
else
deli(open_files,idx)
if idx<focused_file then focused_file-=1 end
end
end
local bottom_bar
local current_pulldown
function _init()
add(open_files,{path="/ram/cart/main.lua",name="main.lua",state=fetch("/ram/cart/main.lua"),dec="",saved=true})
gui = create_gui()
code_editor = gui:attach_text_editor({
x=0,y=14,
width=150,
height=200,
width_rel=1,
height_rel=1,
height_add=-25,
show_line_numbers=true,
syntax_highlighting=true,
markup=false,
embed_pods=true,
has_search=true
})
code_editor:attach_scrollbars({autohide=true})
code_editor:set_keyboard_focus(true)
code_editor:set_text(open_files[focused_file].state)
-- quick buttons
--save
save_button = gui:attach{
cursor = "pointer",
x = 0, y = 0,
width=10, height=10,
draw = function(self)
end,
tap = function(self)
open_files[focused_file].state = table.concat(code_editor:get_text(),"\n")
store(open_files[focused_file].path,open_files[focused_file].state,{})
notify("Saved "..open_files[focused_file].path)
self.y=0
open_files[focused_file].saved = true
end,
click = function(self)
self.y=1
end
}
--run
run_button = gui:attach{
cursor = "pointer",
x = 10, y = 0,
width=10, height=10,
draw = function(self)
end,
tap = function(self)
create_process(open_files[focused_file].path)
self.y=0
end,
click = function(self)
self.y=1
end
}
--term
term_button = gui:attach{
cursor = "pointer",
x = 20, y = 0,
width=10, height=10,
draw = function(self)
end,
tap = function(self)
create_process("/system/apps/terminal.lua")
-- I really want this to make the working
-- directory the open file, but I just
-- can't figure it out.
-- if you know how to, please let me know!
self.y=0
end,
click = function(self)
self.y=1
end
}
--nav
nav_button = gui:attach{
cursor = "pointer",
x = 30, y = 0,
width=10, height=10,
draw = function(self)
end,
tap = function(self)
local file = open_files[focused_file]
local open_path = file.path:sub(0,#file.path-#file.name)
create_process("/system/apps/filenav.p64", {argv={open_path}})
self.y=0
end,
click = function(self)
self.y=1
end
}
--clicking tabs
gui:attach{
cursor = "pointer",
x=tab_x_offset, y=0,
width=1000, height=10,
click = function(self)
if get_tab_at_mouse() then
if m.b==4 then
close_tab(get_tab_at_mouse())
end
if m.b==1 then set_active_tab(get_tab_at_mouse())end
if m.b==2 then
if current_pulldown then gui:detach(current_pulldown) end
local this_tab = get_tab_at_mouse()
local x = get_tab_x(this_tab)
local pulldown = gui:attach_pulldown({
width=64,height=16,y=13
})
current_pulldown=pulldown
pulldown.x = x
pulldown:attach_pulldown_item({
label="Close tab",
action=function()
close_tab(this_tab)
end
})
end
end
end
}
code_editor.tap=function()
if current_pulldown then
gui:detach(current_pulldown)
end
end
--bottom bar
bottom_bar = gui:attach{
x=0,y=32,
width=1000, height=14,
draw = function(self)
rectfill(0,0,1000,18,16)
local colnum, rownum = code_editor.get_cursor()
print("Col: "..tostr(colnum),2,2,1)
end
}
end
function get_tab_at_mouse()
local offset = 0
for i=1,#open_files do
local file = open_files[i]
if mouse_aabb(tab_x_offset+offset,0,tab_x_offset+offset+txtw(file.name..file.dec)+2,10) then
return i
end
offset+=txtw(file.name..file.dec)+4
end
return false
end
-- I know this is stupid I got confused from all
-- the godot dev I was doing, I'll fix it later
function tern(con,a,b)
return con and a or b
end
function tab(x,w,yoff,sel,txt)
rectfill(x,3+yoff,x+w,10,tern(sel,16,1))
line(x+1,2+yoff,x+w-1,2+yoff)
print(txt,x+2,3+yoff,7)
end
function txtw(txt)
return print(txt,0,-1000)
end
function mouse_aabb(x1,y1,x2,y2)
local mx,my = mouse()
return (mx>=x1 and mx<=x2 and my>=y1 and my<=y2 and focused)
end
function _draw()
m.x,m.y,m.b = mouse()
gui:update_all()
if (not code_editor:search_box_is_open()) code_editor:set_keyboard_focus(true)
gui:draw_all()
window({
title=(open_files[focused_file].path.." - SLATE")
})
rectfill(0,0,1000,10,12)
rectfill(0,11,1000,13,16)
spr(button_gfx.save,1,2+save_button.y)
spr(button_gfx.run,10,2+run_button.y)
spr(button_gfx.term,19,2+term_button.y)
spr(button_gfx.nav,28,2+nav_button.y)
open_files[focused_file].dec = tern(open_files[focused_file].saved,""," *")
local offset = 0
for i=1,#open_files do
local file = open_files[i]
local foc = i==focused_file
tab(tab_x_offset+offset,txtw(file.name..file.dec)+2,tern(foc,-1,0),foc,file.name..file.dec)
offset+=txtw(file.name..file.dec)+4
end
bottom_bar.y=get_display():height()-11
end
function get_tab_x(idx)
local x=32
for i=1,#open_files do
if i==idx then return x end
x+=txtw(open_files[i].name..open_files[i].dec)+4
end
return x
end
function set_active_tab(idx)
code_editor.syntax_highlighting = (string.sub(open_files[idx].name,-4)!=".txt")
open_files[focused_file].state = table.concat(code_editor:get_text(),"\n")
focused_file = idx
code_editor:set_text(open_files[focused_file].state)
end
on_event("drop_items",function(msg)
for i=1,#msg.items do
local item = msg.items[i]
if item.pod_type == "file_reference" then
if item.attrib == "file" then
add(open_files,{path=item.fullpath,name=item.filename,state=fetch(item.fullpath),saved=true,dec=""})
set_active_tab(#open_files)
else
create_process("/system/apps/filenav.p64", {argv={item.fullpath}})
notify(item.filename.." is a folder.")
end
else
notify("Couldn't open "..item.filename)
end
end
end)
on_event("gained_focus",function(msg)
focused=true
end)
on_event("lost_focus",function(msg)
focused=false
end)
-- for that pretty little star
on_event("keydown",function()
open_files[focused_file].saved=false
end)