Currently only searches prefixes of available alternatives.
DGMHQDVOII6WF2OJNHP42AYYMCMAIM266I3KHED74OBYMIWWAQXQC
EWK46OTWI2QK7ZZGYOCWWBHXHBXZQWR2FDW6LRTTKCN4K3ZFE5JAC
V5PFN4BCA6B5PVK2OJ5HFW2AVMYMHQ7HXMVGUWLUQDWAMQFWBWYQC
S4U35JJQWC3CBNKHAEZ2DYEGUT5L3UYVAO6PJ5R7HSTD53KWVAXQC
2L5MEZV344TOZLVY3432RHJFIRVXFD6O3GWLL5O4CV66BGAFTURQC
OTIBCAUJ3KDQJLVDN3A536DLZGNRYMGJLORZVR3WLCGXGO6UGO6AC
DUQDA3U7VNWZSKRVACS6G3FTEB5VXRR7FJQU5NYZ4EFSGL3XUU5QC
3QQZ7W4EJ7G4HQM5IYWXICMAHVRGERY4X6AOC6LOV5NSZ4OBICSAC
4BX4GJEWW7Z5LA4SJUXADYLAHOYFL4IBOYH4J4DJYRAVKKGGFHGQC
TZ3ZNGRMZ3DG33VVEZI74W5W5T73JYMEIYQ3SO6Y4A6FN3OUZIKQC
YTSPVDZHEN5LLNMGIBUBLPWFWSFM3SOHBRGWYSDEVFKRTH24ARRQC
VHQCNMARPMNBSIUFLJG7HVK4QGDNPCGNVFLHS3I4IGNVSV5MRLYQC
local pane = Surface[Cursor_pane.col][Cursor_pane.row]
if pane.show_cursor then
return edit.textinput(pane, t)
if Display_settings.mode == nil then
local pane = Surface[Cursor_pane.col][Cursor_pane.row]
if pane.show_cursor then
return edit.textinput(pane, t)
end
elseif Display_settings.mode == 'palette' then
Display_settings.palette = Display_settings.palette..t
Display_settings.palette_text = App.newText(love.graphics.getFont(), Display_settings.palette)
else
print(Display_settings.mode)
assert(false)
if Display_settings.mode == nil then
handle_normal_mode_keychord(chord, key)
elseif Display_settings.mode == 'palette' then
handle_command_palette_keychord(chord, key)
else
print(Display_settings.mode)
assert(false)
end
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 options = options()
if #options > 0 then
-- TODO
print(options[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(options()) 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 options()
if Display_settings.palette == '' then
return Commands
elseif Display_settings.palette:sub(1,1) == '/' then
return {}
else
local results = filter_options(Commands, Display_settings.palette)
append(results, file_options(Display_settings.palette))
return results
end
end
function filter_options(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_options(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_options(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_normal_mode_keychord(chord, key)
function concat_all(dir, files)
if dir == '' then return files end
for i,file in ipairs(files) do
files[i] = dir..file
end
return files
end
function append(arr, b)
for _,x in ipairs(b) do
table.insert(arr, x)
end
end
function test_dirname()
check_eq(dirname('a/b'), 'a/', 'F - test_dirname')
check_eq(dirname('x'), './', 'F - test_dirname/current')
end
function basename(path)
return string.gsub(path, ".*/(.*)", "%1")
end