GQBUV2XOMEPMTXMPCBQWGGIUXGQDX77VTGPFIG6YT7G64ASOYHXQC
CZY3IDERLI6MTKKKMX6QLLERSPM2ZJ57NGQRKILJBM7S5PYPQ3XAC
BB4JL5NTURTH4VDDUOUYNM66GJX3VMNNDE4FFK47CV6SN2YEN4ZQC
UH4YWHW5NDKNR7RS664UG4PRJNZIPNWAD5JWBEUB22JHOY2SWZKAC
CE4LZV4TNXJT54CVGM3QANCBP42TMLMZWF2DBSMUYKAHILXIZEMQC
OTIBCAUJ3KDQJLVDN3A536DLZGNRYMGJLORZVR3WLCGXGO6UGO6AC
DGMHQDVOII6WF2OJNHP42AYYMCMAIM266I3KHED74OBYMIWWAQXQC
R5QXEHUIZLELJGGCZAE7ATNS3CLRJ7JFRENMGH4XXH24C5WABZDQC
end
function handle_command_palette_keychord(chord, key)
if chord == 'escape' then
Display_settings.mode = nil
elseif chord == 'backspace' then
local len = utf8.len(Display_settings.palette)
local byte_offset = Text.offset(Display_settings.palette, len)
Display_settings.palette = string.sub(Display_settings.palette, 1, byte_offset-1)
Display_settings.palette_text = App.newText(love.graphics.getFont(), Display_settings.palette)
elseif chord == 'return' then
-- TODO: handle search command
local candidates = candidates()
if #candidates > 0 then
-- TODO
print(candidates[1])
end
Display_settings.mode = nil
end
end
function draw_command_palette()
local pwidth,pheight = 400, 300
local left = math.max(App.screen.width/2-pwidth/2, 0)
local top = math.max(App.screen.height/2-pheight/2, 0)
local width = math.min(pwidth, App.screen.width)
local height = math.min(pheight, App.screen.height)
-- background
App.color(Command_palette_background_color)
love.graphics.rectangle('fill', left, top, width, height, 10, 10) -- rounded corners
App.color(Command_palette_border_color)
love.graphics.rectangle('line', left, top, width, height, 10, 10) -- rounded corners
love.graphics.line(left, top+5+Line_height+5, left+width, top+5+Line_height+5)
-- input box
App.color(Command_palette_command_color)
draw_palette_input(left+10, top+5)
-- alternatives
App.color(Command_palette_alternatives_color)
local y = top+5+Line_height+5+5
for _,cmd in ipairs(candidates()) do
love.graphics.print(cmd, left+5,y)
y = y+Line_height
if y >= top+height then
break
end
end
end
function draw_palette_input(x, y)
love.graphics.draw(Display_settings.palette_text, x,y)
x = x+App.width(Display_settings.palette_text)
draw_cursor(x, y)
end
function draw_cursor(x, y)
-- blink every 0.5s
if math.floor(Cursor_time*2)%2 == 0 then
App.color(Cursor_color)
love.graphics.rectangle('fill', x,y, 3,Line_height)
end
end
function candidates()
if Display_settings.palette == '' then
return Commands
elseif Display_settings.palette:sub(1,1) == '/' then
return {}
else
local results = filter_candidates(Commands, Display_settings.palette)
append(results, file_candidates(Display_settings.palette))
return results
end
function filter_candidates(candidates, prefix)
local result = {}
for _,cand in ipairs(candidates) do
if cand:find(prefix) == 1 then
table.insert(result, cand)
end
end
return result
end
function file_candidates(prefix)
local path = Directory
local dir = dirname(prefix)
local visible_dir = dir
if dir ~= './' then
path = path..'/'..dir
else
visible_dir = ''
end
local files = love.filesystem.getDirectoryItems(path)
local base = basename(prefix)
return concat_all(visible_dir, filter_candidates(reorder(path, files), base))
end
function reorder(dir, files)
local result = {}
local info = {}
for _,file in ipairs(files) do
info[file] = love.filesystem.getInfo(dir..'/'..file)
end
-- files before directories
for _,file in ipairs(files) do
if info[file].type ~= 'directory' then
table.insert(result, file)
end
end
for _,file in ipairs(files) do
if info[file].type == 'directory' then
table.insert(result, file..'/')
end
end
return result
end
function handle_command_palette_keychord(chord, key)
if chord == 'escape' then
Display_settings.mode = nil
elseif chord == 'backspace' then
local len = utf8.len(Display_settings.palette)
local byte_offset = Text.offset(Display_settings.palette, len)
Display_settings.palette = string.sub(Display_settings.palette, 1, byte_offset-1)
Display_settings.palette_text = App.newText(love.graphics.getFont(), Display_settings.palette)
elseif chord == 'return' then
-- TODO: handle search command
local candidates = candidates()
if #candidates > 0 then
-- TODO
print(candidates[1])
end
Display_settings.mode = nil
end
end
function draw_command_palette()
local pwidth,pheight = 400, 300
local left = math.max(App.screen.width/2-pwidth/2, 0)
local top = math.max(App.screen.height/2-pheight/2, 0)
local width = math.min(pwidth, App.screen.width)
local height = math.min(pheight, App.screen.height)
-- background
App.color(Command_palette_background_color)
love.graphics.rectangle('fill', left, top, width, height, 10, 10) -- rounded corners
App.color(Command_palette_border_color)
love.graphics.rectangle('line', left, top, width, height, 10, 10) -- rounded corners
love.graphics.line(left, top+5+Line_height+5, left+width, top+5+Line_height+5)
-- input box
App.color(Command_palette_command_color)
draw_palette_input(left+10, top+5)
-- alternatives
App.color(Command_palette_alternatives_color)
local y = top+5+Line_height+5+5
for _,cmd in ipairs(candidates()) do
love.graphics.print(cmd, left+5,y)
y = y+Line_height
if y >= top+height then
break
end
end
end
function draw_palette_input(x, y)
love.graphics.draw(Display_settings.palette_text, x,y)
x = x+App.width(Display_settings.palette_text)
draw_cursor(x, y)
end
function draw_cursor(x, y)
-- blink every 0.5s
if math.floor(Cursor_time*2)%2 == 0 then
App.color(Cursor_color)
love.graphics.rectangle('fill', x,y, 3,Line_height)
end
end
function candidates()
if Display_settings.palette == '' then
return Commands
elseif Display_settings.palette:sub(1,1) == '/' then
return {}
else
local results = filter_candidates(Commands, Display_settings.palette)
append(results, file_candidates(Display_settings.palette))
return results
end
end
function filter_candidates(candidates, prefix)
local result = {}
for _,cand in ipairs(candidates) do
if cand:find(prefix) == 1 then
table.insert(result, cand)
end
end
return result
end
function file_candidates(prefix)
local path = Directory
local dir = dirname(prefix)
local visible_dir = dir
if dir ~= './' then
path = path..'/'..dir
else
visible_dir = ''
end
local files = love.filesystem.getDirectoryItems(path)
local base = basename(prefix)
return concat_all(visible_dir, filter_candidates(reorder(path, files), base))
end
function reorder(dir, files)
local result = {}
local info = {}
for _,file in ipairs(files) do
info[file] = love.filesystem.getInfo(dir..'/'..file)
end
-- files before directories
for _,file in ipairs(files) do
if info[file].type ~= 'directory' then
table.insert(result, file)
end
end
for _,file in ipairs(files) do
if info[file].type == 'directory' then
table.insert(result, file..'/')
end
end
return result
end