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 thenreturn edit.textinput(pane, t)
if Display_settings.mode == nil thenlocal pane = Surface[Cursor_pane.col][Cursor_pane.row]if pane.show_cursor thenreturn edit.textinput(pane, t)endelseif Display_settings.mode == 'palette' thenDisplay_settings.palette = Display_settings.palette..tDisplay_settings.palette_text = App.newText(love.graphics.getFont(), Display_settings.palette)elseprint(Display_settings.mode)assert(false)
if Display_settings.mode == nil thenhandle_normal_mode_keychord(chord, key)elseif Display_settings.mode == 'palette' thenhandle_command_palette_keychord(chord, key)elseprint(Display_settings.mode)assert(false)endendfunction handle_command_palette_keychord(chord, key)if chord == 'escape' thenDisplay_settings.mode = nilelseif chord == 'backspace' thenlocal 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 commandlocal options = options()if #options > 0 then-- TODOprint(options[1])endDisplay_settings.mode = nilendendfunction draw_command_palette()local pwidth,pheight = 400, 300local 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)-- backgroundApp.color(Command_palette_background_color)love.graphics.rectangle('fill', left, top, width, height, 10, 10) -- rounded cornersApp.color(Command_palette_border_color)love.graphics.rectangle('line', left, top, width, height, 10, 10) -- rounded cornerslove.graphics.line(left, top+5+Line_height+5, left+width, top+5+Line_height+5)-- input boxApp.color(Command_palette_command_color)draw_palette_input(left+10, top+5)-- alternativesApp.color(Command_palette_alternatives_color)local y = top+5+Line_height+5+5for _,cmd in ipairs(options()) dolove.graphics.print(cmd, left+5,y)y = y+Line_heightif y >= top+height thenbreakendendendfunction 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)endfunction draw_cursor(x, y)-- blink every 0.5sif math.floor(Cursor_time*2)%2 == 0 thenApp.color(Cursor_color)love.graphics.rectangle('fill', x,y, 3,Line_height)endendfunction options()if Display_settings.palette == '' thenreturn Commandselseif Display_settings.palette:sub(1,1) == '/' thenreturn {}elselocal results = filter_options(Commands, Display_settings.palette)append(results, file_options(Display_settings.palette))return resultsendendfunction filter_options(candidates, prefix)local result = {}for _,cand in ipairs(candidates) doif cand:find(prefix) == 1 thentable.insert(result, cand)endendreturn resultendfunction file_options(prefix)local path = Directorylocal dir = dirname(prefix)local visible_dir = dirif dir ~= './' thenpath = path..'/'..direlsevisible_dir = ''endlocal files = love.filesystem.getDirectoryItems(path)local base = basename(prefix)return concat_all(visible_dir, filter_options(reorder(path, files), base))endfunction reorder(dir, files)local result = {}local info = {}for _,file in ipairs(files) doinfo[file] = love.filesystem.getInfo(dir..'/'..file)end-- files before directoriesfor _,file in ipairs(files) doif info[file].type ~= 'directory' thentable.insert(result, file)endendfor _,file in ipairs(files) doif info[file].type == 'directory' thentable.insert(result, file..'/')endendreturn resultendfunction handle_normal_mode_keychord(chord, key)
function concat_all(dir, files)if dir == '' then return files endfor i,file in ipairs(files) dofiles[i] = dir..fileendreturn filesendfunction append(arr, b)for _,x in ipairs(b) dotable.insert(arr, x)endend
function test_dirname()check_eq(dirname('a/b'), 'a/', 'F - test_dirname')check_eq(dirname('x'), './', 'F - test_dirname/current')endfunction basename(path)return string.gsub(path, ".*/(.*)", "%1")end