fastest version so far
This commit is contained in:
parent
6517ba2209
commit
93d5957f00
|
@ -1,5 +1,7 @@
|
|||
require "spec"
|
||||
require "../../../src/fingers/hinter"
|
||||
require "../../../src/fingers/state"
|
||||
require "../../../src/fingers/commands/load_config"
|
||||
|
||||
record StateDouble, selected_hints : Array(String)
|
||||
|
||||
|
@ -25,14 +27,11 @@ class TestFormatter < ::Fingers::Formatter
|
|||
end
|
||||
|
||||
describe Fingers::Hinter do
|
||||
input = "
|
||||
ola ke ase
|
||||
ke ase ola
|
||||
ke olaola ke
|
||||
ke ola ase
|
||||
|
||||
beep beep
|
||||
"
|
||||
input = 50.times.map do
|
||||
10.times.map do
|
||||
rand.to_s.split(".").last
|
||||
end.join(" ")
|
||||
end.join("\n")
|
||||
|
||||
width = 40
|
||||
|
||||
|
@ -40,13 +39,14 @@ beep beep
|
|||
|
||||
formatter = TestFormatter.new
|
||||
|
||||
patterns = ["ola"]
|
||||
patterns = Fingers::Commands::LoadConfig::DEFAULT_PATTERNS.values.to_a
|
||||
alphabet = "asdf".split("")
|
||||
|
||||
hinter = Fingers::Hinter.new(
|
||||
input: input,
|
||||
width: width,
|
||||
patterns: patterns,
|
||||
state: ::Fingers::State.new,
|
||||
alphabet: alphabet,
|
||||
output: output,
|
||||
formatter: formatter,
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
require "file_utils"
|
||||
require "./base"
|
||||
require "../dirs"
|
||||
require "../config"
|
||||
require "../../tmux"
|
||||
|
|
|
@ -22,9 +22,15 @@ class Huffman
|
|||
end
|
||||
|
||||
def generate_hints(alphabet : Array(String), n : Int32)
|
||||
setup!(alphabet: alphabet, n: n)
|
||||
cached_result = read_from_cache(alphabet, n)
|
||||
Log.info { "from_cache: #{cached_result}" }
|
||||
return cached_result unless cached_result.nil?
|
||||
|
||||
return alphabet if n <= alphabet.size
|
||||
if n <= alphabet.size
|
||||
return alphabet
|
||||
end
|
||||
|
||||
setup!(alphabet: alphabet, n: n)
|
||||
|
||||
first_node = true
|
||||
|
||||
|
@ -50,7 +56,11 @@ class Huffman
|
|||
result.push(translate_path(path)) if node.children.empty?
|
||||
end
|
||||
|
||||
result.sort_by { |hint| hint.size }
|
||||
final_result = result.sort_by { |hint| hint.size }
|
||||
|
||||
save_to_cache(alphabet, n, final_result)
|
||||
|
||||
final_result
|
||||
end
|
||||
|
||||
private def setup!(alphabet, n)
|
||||
|
@ -73,6 +83,21 @@ class Huffman
|
|||
result
|
||||
end
|
||||
|
||||
private def read_from_cache(alphabet, n) : Array(String) | Nil
|
||||
File.read(cache_key(alphabet, n)).chomp.split(":")
|
||||
rescue File::NotFoundError
|
||||
Log.info { "hint cache miss" }
|
||||
nil
|
||||
end
|
||||
|
||||
private def save_to_cache(alphabet, n, result)
|
||||
File.write(cache_key(alphabet, n), result.join(":"))
|
||||
end
|
||||
|
||||
private def cache_key(alphabet, n)
|
||||
Fingers::Dirs::CACHE / "#{alphabet.join("")}-#{n}"
|
||||
end
|
||||
|
||||
private def arity
|
||||
alphabet.size
|
||||
end
|
||||
|
|
|
@ -90,7 +90,11 @@ class Tmux
|
|||
end
|
||||
|
||||
def find_pane_by_id(id) : Pane | Nil
|
||||
panes.find { |pane| pane.pane_id == id }
|
||||
output = `#{tmux} display-message -t '#{id}' -F '#{PANE_FORMAT}' -p`.chomp
|
||||
|
||||
return nil if output.empty?
|
||||
|
||||
Pane.from_json(output)
|
||||
end
|
||||
|
||||
def windows
|
||||
|
|
Loading…
Reference in New Issue