about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-09-18 01:53:36 -0700
committerKartik K. Agaram <vc@akkartik.com>2022-09-18 01:53:36 -0700
commitd141822e6cf2516d60fe168dd4f620389f0ab5da (patch)
treeb4209a6074f554f042ef70b32bfad8e6ad297c38
parent141d41aec536a960096e777cb9f6ceef08f121a1 (diff)
downloadview.love-d141822e6cf2516d60fe168dd4f620389f0ab5da.tar.gz
filter candidates in file navigator
-rw-r--r--commands.lua42
-rw-r--r--source.lua5
2 files changed, 46 insertions, 1 deletions
diff --git a/commands.lua b/commands.lua
index 71d74f0..583d1ef 100644
--- a/commands.lua
+++ b/commands.lua
@@ -53,6 +53,10 @@ function add_hotkey_to_menu(s)
 end
 
 function source.draw_file_navigator()
+  App.color(Menu_command_color)
+  local filter_text = to_text(File_navigation.filter)
+  App.screen.draw(filter_text, 5, 5)
+  draw_cursor(5 + App.width(filter_text), 5)
   if File_navigation.num_lines == nil then
     File_navigation.num_lines = source.num_lines_for_file_navigator(File_navigation.candidates)
   end
@@ -71,6 +75,27 @@ function source.draw_file_navigator()
   end
 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,Editor_state.line_height)
+  end
+end
+
+function source.file_navigator_candidates()
+  if File_navigation.filter == '' then
+    return File_navigation.all_candidates
+  end
+  local result = {}
+  for _,filename in ipairs(File_navigation.all_candidates) do
+    if starts_with(filename, File_navigation.filter) then
+      table.insert(result, filename)
+    end
+  end
+  return result
+end
+
 function source.num_lines_for_file_navigator(candidates)
   local result = 1
   local x = 5
@@ -115,10 +140,22 @@ function keychord_pressed_on_file_navigator(chord, key)
   log(2, {name='file_navigator_state', files=File_navigation.candidates, index=File_navigation.index})
   if chord == 'escape' then
     Show_file_navigator = false
+    File_navigation.index = 1
+    File_navigation.filter = ''
+    File_navigation.candidates = File_navigation.all_candidates
   elseif chord == 'return' then
     local candidate = guess_source(File_navigation.candidates[File_navigation.index]..'.lua')
     source.switch_to_file(candidate)
     Show_file_navigator = false
+    File_navigation.index = 1
+    File_navigation.filter = ''
+    File_navigation.candidates = File_navigation.all_candidates
+  elseif chord == 'backspace' then
+    local len = utf8.len(File_navigation.filter)
+    local byte_offset = Text.offset(File_navigation.filter, len)
+    File_navigation.filter = string.sub(File_navigation.filter, 1, byte_offset-1)
+    File_navigation.index = 1
+    File_navigation.candidates = source.file_navigator_candidates()
   elseif chord == 'left' then
     if File_navigation.index > 1 then
       File_navigation.index = File_navigation.index-1
@@ -243,3 +280,8 @@ function file_index(fy, fx, fwidth)
   log_end('file index')
   return best_guess
 end
+
+function textinput_on_file_navigator(t)
+  File_navigation.filter = File_navigation.filter..t
+  File_navigation.candidates = source.file_navigator_candidates()
+end
diff --git a/source.lua b/source.lua
index 69b2d4f..6dd6d1d 100644
--- a/source.lua
+++ b/source.lua
@@ -11,7 +11,7 @@ function source.initialize_globals()
   Focus = 'edit'
   Show_file_navigator = false
   File_navigation = {
-    candidates = {
+    all_candidates = {
       'main',
       'run',
       'run_tests',
@@ -45,7 +45,9 @@ function source.initialize_globals()
       'json',
     },
     index = 1,
+    filter = '',
   }
+  File_navigation.candidates = File_navigation.all_candidates  -- modified with filter
 
   Menu_status_bar_height = 5 + --[[line height in tests]] 15 + 5
 
@@ -308,6 +310,7 @@ end
 function source.textinput(t)
   Cursor_time = 0  -- ensure cursor is visible immediately after it moves
   if Show_file_navigator then
+    textinput_on_file_navigator(t)
     return
   end
   if Focus == 'edit' then