Skip to content

Commit 42d4ecb

Browse files
authored
+ features and - bugs (see description)
* Custom color palette (default: GNOME) * Click file path as string to open * Infobar is now modular and more resistant to resizing * ".lua" will now be automatically added to path * Fixed crashing due to getting an boolean instead of table with virtualAPIs * Popup length calculation will ignore cosu escapes
1 parent 649c143 commit 42d4ecb

File tree

1 file changed

+201
-64
lines changed

1 file changed

+201
-64
lines changed

cosu.lua

Lines changed: 201 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,25 @@ cosuConf.bDoubleClickButton = false
2626
cosuConf.nTabSpace = 4 --[[ Normaly 4 spaces. ]]
2727
cosuConf.bJumpAtEndToBegin = true
2828
cosuConf.bShadows = true
29+
cosuConf.tPalette = {
30+
true,
31+
["black"] = 0x171421,
32+
["blue"] = 0x2A7BDE,
33+
["brown"] = 0xA2734C,
34+
["cyan"] = 0x2AA1B3,
35+
["gray"] = 0x5E5C64,
36+
["green"] = 0x26A269,
37+
["lightBlue"] = 0x33C7DE,
38+
["lightGray"] = 0xD0CFCC,
39+
["lime"] = 0x33D17A,
40+
["magenta"] = 0xC061CB,
41+
["orange"] = 0xE9AD0C,
42+
["pink"] = 0xF66151,
43+
["purple"] = 0xA347BA,
44+
["red"] = 0xC01C28,
45+
["white"] = 0xFFFFFF,
46+
["yellow"] = 0xF3F03E
47+
}
2948
--[[ Color palette for .lua files ]]
3049
local colorMatch = { }
3150
colorMatch["popupBG"]=colors.lightGray
@@ -119,7 +138,7 @@ local sGithub = {
119138
["api"]="https://api.github.com/repos/1turtle/consult/releases/latest",
120139
["latest"]="https://github.com/1Turtle/consult/releases/latest/download/cosu.lua"
121140
}
122-
local sVersion = "1.3.0"
141+
local sVersion = "1.4.0"
123142
local tArgs = { ... }
124143
local init = function() return end
125144
local sPath = ""
@@ -193,6 +212,33 @@ local function formatText(sText,nLength,nColumns)
193212
return table.unpack(sText)
194213
end
195214

215+
local function cToStr(cFG, cBG)
216+
return "{&"..(blits[cFG] or ' ')..(blits[cBG] or ' ')
217+
end
218+
219+
local function fLen(text)
220+
local len = 0
221+
if text:find('{&') then
222+
for word in string.gmatch(text, '([^{&]+)') do
223+
len = len+#word:sub(3)
224+
end
225+
else
226+
return #text
227+
end
228+
return len
229+
end
230+
231+
local function swapColors()
232+
if not cosuConf.tPalette[1] then return end
233+
for color, code in pairs(cosuConf.tPalette) do
234+
if type(color) == "string" then
235+
local tmp = colors.packRGB(term.getPaletteColor(colors[color]))
236+
term.setPaletteColor(colors[color], code)
237+
cosuConf.tPalette[color] = tmp
238+
end
239+
end
240+
end
241+
196242
--[[ +++ Popup handler +++ ]]
197243
local update,info,help,file,error,options,exit,specialChar
198244

@@ -282,6 +328,62 @@ function update(event, ...)
282328
end
283329
end
284330

331+
function openLink(event, ...)
332+
if event == "close" then
333+
for index,pop in pairs(tPopup) do
334+
if pop.name == "Link" then
335+
table.remove(tPopup, index)
336+
end
337+
end
338+
elseif event == "create" then
339+
local link = tostring(({...})[1])
340+
link = link:sub(2,#link-1)
341+
if fs.isDir(link)
342+
or link == "" or link == "..." or link == "." or not fs.exists(link) or link:find('ö') or link:find('ä') or link:find('ü') then return end
343+
local line = "Open the following "..(fs.isDir(link) and "directory" or "file").."?"
344+
local slink = link
345+
if #slink >= #line then
346+
slink = ".."..slink:sub(-(#line-3))
347+
end
348+
table.insert(tPopup, 1, {
349+
["status"] = true,
350+
["name"] = "Link",
351+
["link"] = link,
352+
["size"] = {
353+
['x'] = nil,['y'] = nil
354+
},
355+
["text"] = {
356+
{
357+
line,
358+
cToStr(' ', colorMatch.bg)..(' '):rep(#line)
359+
}
360+
},
361+
["label"] = {
362+
{
363+
content = cToStr(colorMatch.special,colorMatch.bg)..slink,
364+
x=math.floor((#line-#slink)/2+0.5),y=2,
365+
}
366+
},
367+
["button"] = {
368+
{ ['x']=#line-3, ['y']=4, ["label"]="Open", ["status"]=false, ["func"]=function(pop)
369+
local link = pop.link
370+
openLink("close")
371+
if multishell then
372+
if fs.isDir(link) then
373+
multishell.launch(_ENV, shell.resolveProgram("shell"))
374+
else
375+
multishell.launch(_ENV, shell.getRunningProgram(), link)
376+
end
377+
tActiveKeys["CTRL"] = false
378+
end
379+
end},
380+
{ ['x']=#line-6, ['y']=4, ["label"]="No", ["status"]=false, ["func"]=function() openLink("close") end }
381+
}
382+
})
383+
tCursor.selectedItem = 1
384+
end
385+
end
386+
285387
function info(event, ...)
286388
if event == "close" then
287389
for index,pop in pairs(tPopup) do
@@ -1013,7 +1115,7 @@ function loadAPIVirtual(sLine)
10131115
elseif typeOfLoad=="peripheral.wrap" then
10141116
ok,out = pcall(peripheral.wrap, sAPI)
10151117
end
1016-
if ok and type(out)~="nil" then
1118+
if ok and type(out)=="table" then
10171119
for var,value in pairs(out) do
10181120
if type(sSubVar)=="string" and #sSubVar>1 then
10191121
if var==sSubVar then
@@ -1234,13 +1336,16 @@ function draw.dropdownBG(nW,nH, nX, nY, tLineBreaks)
12341336
end
12351337

12361338
function draw.fText(text)
1339+
local tx,bg = term.getTextColor(),term.getBackgroundColor()
12371340
if text:find('{&') then
12381341
for word in string.gmatch(text, '([^{&]+)') do
1239-
term.setTextColor(blitInvert[word:sub(1,1):lower()] or colorMatch.popupFont)
1240-
term.write(word:sub(2))
1342+
term.setTextColor(blitInvert[word:sub(1,1):lower()] or tx)
1343+
term.setBackgroundColor(blitInvert[word:sub(2,2):lower()] or bg)
1344+
term.write(word:sub(3))
12411345
end
12421346
else
1243-
term.setTextColor(colorMatch.popupFont)
1347+
draw.switchFGBG(tx,bg)
1348+
term.setTextColor(tx)
12441349
term.write(text)
12451350
end
12461351
end
@@ -1254,8 +1359,9 @@ function draw.popup(popup,index)
12541359
for _,tBlock in pairs(popup.text) do
12551360
for _,sLine in pairs(tBlock) do
12561361
size.h = size.h + 1
1257-
if #sLine > size.w then
1258-
size.w = #sLine
1362+
local len = fLen(sLine)
1363+
if len > size.w then
1364+
size.w = len
12591365
end
12601366
end
12611367
size.h = size.h + 1
@@ -1275,8 +1381,8 @@ function draw.popup(popup,index)
12751381
--[[ Border/BG ]]
12761382
draw.switchFGBG(colorMatch["popupFrame"], colorMatch["popupBG"])
12771383
draw.dropdownBG(size.w,size.h+2, popup.x,popup.y-1,{})
1278-
--[[ Text (& Line breaks) ]]
12791384
draw.switchFGBG(colorMatch["popupFont"], colorMatch["popupBG"])
1385+
--[[ Text (& Line breaks) ]]
12801386
local sBuffer = ("\140"):rep(size.w)
12811387
local lCount = 0
12821388
if type(popup.text)=="table" then
@@ -1285,6 +1391,7 @@ function draw.popup(popup,index)
12851391
lCount = lCount + 1
12861392
term.setCursorPos(popup.x+1, popup.y+lCount)
12871393
draw.fText(sLine)
1394+
draw.switchFGBG(colorMatch["popupFont"], colorMatch["popupBG"])
12881395
end
12891396
lCount = lCount + 1
12901397
term.setCursorPos(popup.x+1, popup.y+lCount)
@@ -1362,16 +1469,11 @@ function draw.popup(popup,index)
13621469
if type(popup.label)=="table" then
13631470
for _,label in pairs(popup.label) do
13641471
term.setCursorPos(popup.x+label.x, popup.y+label.y)
1365-
if type(label.bg)=="number" then
1366-
term.setBackgroundColor(label.bg)
1367-
else term.setBackgroundColor(colorMatch.popupBG) end
13681472
local str = label.content
1369-
if type(str) == "function" then
1370-
str = str()
1371-
end
1473+
if type(str) == "function" then str = str() end
13721474
str = tostring(str)
13731475
if type(label.fg)=="number" then
1374-
str = "{&"..blits[label.fg or colorMatch.popupFont]..str
1476+
str = "{&"..blits[label.fg or colorMatch.popupFont]..blits[label.bg or colorMatch.popupBG]..str
13751477
end
13761478
draw.fText(str)
13771479
end
@@ -1455,59 +1557,63 @@ function draw.toolbar()
14551557
term.write('\133')
14561558
end
14571559

1560+
local tInfobar = {
1561+
{ --[[ Left side ]]
1562+
function()
1563+
local sShowPath=sPath
1564+
if sPath == "" then
1565+
sShowPath="/*.lua"
1566+
end
1567+
if #sShowPath > w then
1568+
sShowPath = sShowPath:sub(-w+1)
1569+
elseif #sShowPath > w/2 then
1570+
sShowPath = sShowPath:sub(-math.floor(w/2))
1571+
end
1572+
return cToStr(colorMatch.special,colorMatch.bg).." "..sShowPath..cToStr(colorMatch.bg,cosuConf.cAccentColor)..'\151'
1573+
end
1574+
},{ --[[ Right side ]]
1575+
function() return cToStr(colorMatch.cAccentText).."Col "..cToStr(colorMatch.text)..tCursor.x.." "end,
1576+
function() return cToStr(colorMatch.cAccentText).."Ln "..cToStr(colorMatch.text)..tCursor.y..cToStr(colorMatch.cAccentText).."," end,
1577+
function()
1578+
--[[ Calculate size ]]
1579+
local size,sizeType = 0,""
1580+
for _,v in pairs(tContent) do
1581+
size = size+#v+1
1582+
end
1583+
--[[ Size in kB/Byte as string ]]
1584+
if math.floor(size/1000) == 0 then
1585+
size = tostring(size-1)
1586+
sizeType = "Byte"
1587+
else
1588+
local sNumber = tostring((size-1)/1000)
1589+
local nX = sNumber:find("%.")
1590+
if nX then sNumber = sNumber:sub(1,nX+2) end
1591+
size = sNumber
1592+
sizeType = "kB"
1593+
end
1594+
return cToStr(colorMatch.special).."["..cToStr(colorMatch.text)..size..cToStr(colorMatch.cAccentText)..sizeType..cToStr(colorMatch.special).."]"
1595+
end,
1596+
}
1597+
}
14581598
function draw.infobar()
1459-
--[[ Calculate size ]]
1460-
local size,sizeType = 0,""
1461-
for _,v in pairs(tContent) do
1462-
size = size+#v+1
1463-
end
1464-
--[[ Size in kB/Byte as string ]]
1465-
if math.floor(size/1000) == 0 then
1466-
size = tostring(size-1)
1467-
sizeType = "Byte"
1468-
else
1469-
local sNumber = tostring((size-1)/1000)
1470-
local nX = sNumber:find("%.")
1471-
if nX then sNumber = sNumber:sub(1,nX+2) end
1472-
size = sNumber
1473-
sizeType = "kB"
1474-
end
1475-
--[[ Draw size and cursor pos ]]
1476-
term.setCursorPos(1,h)
1599+
local nX = 1
1600+
term.setCursorPos(nX,h)
14771601
term.setBackgroundColor(cosuConf.cAccentColor)
14781602
term.clearLine()
1479-
local nRightBegin = w-(#tostring(tCursor.x)+#tostring(tCursor.y)+#size+#sizeType+12)
1480-
term.setCursorPos(nRightBegin,h)
1481-
draw.switchFGBG(colorMatch["special"], cosuConf.cAccentColor)
1482-
term.write("[")
1483-
term.setTextColor(colors.white)
1484-
term.write(size)
1485-
term.setTextColor(colorMatch.cAccentText)
1486-
term.write(sizeType)
1487-
term.setTextColor(colorMatch["special"])
1488-
term.write("] ")
1489-
term.setTextColor(colorMatch.cAccentText)
1490-
term.write("Ln ")
1491-
term.setTextColor(colors.white)
1492-
term.write(tCursor.y)
1493-
term.setTextColor(colorMatch.cAccentText)
1494-
term.write(",Col ")
1495-
term.setTextColor(colors.white)
1496-
term.write(tCursor.x)
1497-
term.setCursorPos(1,h)
1498-
term.setBackgroundColor(colorMatch["bg"])
1499-
term.setTextColor(colorMatch["special"])
1500-
local sShowPath=sPath
1501-
if sPath == "" then
1502-
sShowPath="/*.lua"
1603+
for _,info in pairs(tInfobar[1]) do
1604+
term.setCursorPos(nX,h)
1605+
draw.fText(info())
1606+
nX = term.getCursorPos()+1
15031607
end
1504-
if #sShowPath>=nRightBegin then
1505-
sShowPath="..."..sShowPath:sub(-nRightBegin+3)
1608+
1609+
local nSubX = w+2
1610+
for _,content in pairs(tInfobar[2]) do
1611+
local line = content()
1612+
nSubX = nSubX-fLen(line)-1
1613+
if nSubX < nX then break end
1614+
term.setCursorPos(nSubX,h)
1615+
draw.fText(line)
15061616
end
1507-
term.write(' '..sShowPath)
1508-
term.setTextColor(cosuConf.cAccentColor)
1509-
draw.switchFGBG()
1510-
term.write('\151')
15111617
end
15121618

15131619
function draw.handler()
@@ -1702,6 +1808,28 @@ function input.insert.mouseClick(nButton, nX, nY)
17021808
tCursor.x = nX + tScroll.x
17031809
if tCursor.x > #tContent[tCursor.y] then tCursor.x = #tContent[tCursor.y]+1 end
17041810
tCursor.lastX = tCursor.x
1811+
1812+
if tActiveKeys["CTRL"] then
1813+
local line = tContent[tCursor.y+tScroll.y]
1814+
if line:find('\"') or line:find('\'') then
1815+
local nXStart,nXEnd
1816+
for i=tCursor.x+tScroll.x,1,-1 do
1817+
if line:sub(i,i) == '\"' or line:sub(i,i) == '\'' then
1818+
nXStart = i
1819+
break
1820+
end
1821+
end
1822+
for i=tCursor.x+tScroll.x,#line,1 do
1823+
if line:sub(i,i) == '\"' or line:sub(i,i) == '\'' then
1824+
nXEnd = i
1825+
break
1826+
end
1827+
end
1828+
if nXStart and nXEnd then
1829+
openLink("create", line:sub(nXStart,nXEnd))
1830+
end
1831+
end
1832+
end
17051833
end
17061834
end
17071835
end
@@ -2147,6 +2275,7 @@ function input.handle.mouse(event)
21472275
end
21482276

21492277
function init()
2278+
swapColors()
21502279
--[[ Check args ]]
21512280
if #tArgs > 0 then
21522281
--[[ Collect file informations ]]
@@ -2166,6 +2295,9 @@ function init()
21662295
file.close()
21672296
else
21682297
table.insert(tContent, "")
2298+
if sPath:sub(#sPath-3) ~= ".lua" then
2299+
sPath = sPath..".lua"
2300+
end
21692301
end
21702302
else
21712303
table.insert(tContent, "")
@@ -2233,6 +2365,11 @@ end
22332365

22342366

22352367
--[[ +++ Actual start of the program LOL +++ ]]
2368+
for i,name in pairs(tArgs) do
2369+
if name == "return" then
2370+
return 0
2371+
end
2372+
end
22362373
if not init() then
22372374
return false
22382375
end
@@ -2279,7 +2416,7 @@ local sDir = '/'..fs.getDir(shell.getRunningProgram())..'/'
22792416
local sCurID = tostring( (type(multishell)~="nil") and multishell.getCurrent() or "" )
22802417
fs.delete(sDir..".tmp"..sCurID)
22812418

2282-
2419+
swapColors()
22832420
term.setBackgroundColor(colors.black)
22842421
term.setTextColor(colors.white)
22852422
shell.run("clear")

0 commit comments

Comments
 (0)