WIP
This commit is contained in:
parent
3e25153bbd
commit
362f416a24
|
@ -1,3 +1,4 @@
|
|||
require "./fingers/logger"
|
||||
require "./fingers/cli"
|
||||
|
||||
module Fingers
|
||||
|
|
|
@ -48,14 +48,12 @@ module Fingers
|
|||
end
|
||||
|
||||
def copy
|
||||
# return unless ENV['DISPLAY']
|
||||
return unless system_copy_command
|
||||
|
||||
system_copy_command
|
||||
end
|
||||
|
||||
def open
|
||||
# return unless ENV['DISPLAY']
|
||||
return unless system_open_command
|
||||
|
||||
system_open_command
|
||||
|
@ -117,7 +115,6 @@ module Fingers
|
|||
end
|
||||
|
||||
def program_exists?(program)
|
||||
puts "trying to find #{program}"
|
||||
Process.find_executable(program)
|
||||
end
|
||||
|
||||
|
|
|
@ -61,8 +61,6 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base
|
|||
config = Fingers::Config.new
|
||||
|
||||
options.each do |option, value|
|
||||
puts "#{option} => #{value}"
|
||||
|
||||
# TODO generate an enum somehow and use an exhaustive case
|
||||
case option
|
||||
when "key"
|
||||
|
@ -98,9 +96,8 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base
|
|||
])
|
||||
|
||||
config.alphabet = ALPHABET_MAP[Fingers.config.keyboard_layout].split("")
|
||||
|
||||
puts config
|
||||
config.save
|
||||
|
||||
Fingers.reset_config
|
||||
end
|
||||
|
||||
|
@ -160,9 +157,8 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base
|
|||
def valid_option?(option)
|
||||
option_method = option_to_method(option)
|
||||
|
||||
# TODO crystal does not support responds_to in runtime i think
|
||||
# TODO validate option
|
||||
true
|
||||
# Fingers.config.responds_to?(option_method.to_sym) || option.match(/^@fingers-pattern-\d+$/)
|
||||
end
|
||||
|
||||
def ensure_cache_folder
|
||||
|
|
|
@ -21,7 +21,6 @@ module Fingers::Commands
|
|||
|
||||
def flush
|
||||
@file.flush
|
||||
# @file.print(@buf)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -37,9 +36,6 @@ module Fingers::Commands
|
|||
handle_input
|
||||
|
||||
teardown
|
||||
|
||||
# tmux.swap_panes(target_pane.pane_id, fingers_window.pane_id)
|
||||
# tmux.kill_window(fingers_window.window_id)
|
||||
end
|
||||
|
||||
private def track_options_to_restore!
|
||||
|
@ -71,7 +67,7 @@ module Fingers::Commands
|
|||
private def handle_input
|
||||
input_socket = InputSocket.new
|
||||
|
||||
# tmux.disable_prefix
|
||||
tmux.disable_prefix
|
||||
tmux.set_key_table "fingers"
|
||||
|
||||
input_socket.on_input do |input|
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
# TODO maybe use some xgd shite here?
|
||||
|
||||
module Fingers::Dirs
|
||||
TMUX_PID = (ENV["TMUX"] || ",0000").split(",")[1]
|
||||
FINGERS_REPO_ROOT = Pathname.new(__dir__).parent.parent
|
||||
TMUX_PID = (ENV["TMUX"] || ",0000").split(",")[1]
|
||||
|
||||
ROOT = Path["~/.tmux"].expand(home: true)
|
||||
ROOT = Path["/tmp"] / "tmux-#{TMUX_PID}"
|
||||
|
||||
LOG_PATH = ROOT / "fingers.log"
|
||||
CACHE = ROOT / "cr-tmux-#{TMUX_PID}"
|
||||
CONFIG_PATH = CACHE / "fingers.config"
|
||||
CACHE = ROOT / "tmux-fingers"
|
||||
CONFIG_PATH = CACHE / "config.json"
|
||||
SOCKET_PATH = CACHE / "fingers.sock"
|
||||
end
|
||||
|
|
|
@ -130,8 +130,6 @@ module Fingers
|
|||
|
||||
match_set = Set(String).new
|
||||
|
||||
# Fingers.benchmark_stamp('counting-matches:start')
|
||||
|
||||
lines.each do |line|
|
||||
line.scan(pattern) do |match|
|
||||
# TODO hey cuidao
|
||||
|
@ -139,8 +137,6 @@ module Fingers
|
|||
end
|
||||
end
|
||||
|
||||
# Fingers.benchmark_stamp('counting-matches:end')
|
||||
|
||||
@n_matches = match_set.size
|
||||
|
||||
match_set.size
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
require "log"
|
||||
|
||||
module Fingers
|
||||
Log.setup(:debug, Log::IOBackend.new(File.new(Dirs::LOG_PATH, "a+")))
|
||||
end
|
|
@ -22,8 +22,6 @@ class Huffman
|
|||
end
|
||||
|
||||
def generate_hints(alphabet : Array(String), n : Int32)
|
||||
puts "generating for n: #{n} alphabet: #{alphabet}"
|
||||
|
||||
setup!(alphabet: alphabet, n: n)
|
||||
|
||||
return alphabet if n <= alphabet.size
|
||||
|
@ -39,7 +37,6 @@ class Huffman
|
|||
end
|
||||
|
||||
smallest = get_smallest(n_branches)
|
||||
puts "smallest: #{smallest.map { |node| node.weight }}"
|
||||
new_node = new_node_from(smallest)
|
||||
|
||||
queue.push(new_node.weight, new_node)
|
||||
|
@ -49,29 +46,20 @@ class Huffman
|
|||
|
||||
root = queue.pop
|
||||
|
||||
puts root.weight
|
||||
|
||||
# traverse_inline(root)
|
||||
|
||||
traverse_tree(root) do |node, path|
|
||||
# puts "node #{node.weight} path: #{path}"
|
||||
result.push(translate_path(path)) if node.children.empty?
|
||||
end
|
||||
|
||||
result.sort_by { |hint| hint.size }
|
||||
end
|
||||
|
||||
# private
|
||||
|
||||
# attr_reader :alphabet, :n, :heap
|
||||
|
||||
def setup!(alphabet, n)
|
||||
private def setup!(alphabet, n)
|
||||
@alphabet = alphabet
|
||||
@n = n
|
||||
@queue = build_heap
|
||||
end
|
||||
|
||||
def initial_number_of_branches
|
||||
private def initial_number_of_branches
|
||||
result = 1
|
||||
|
||||
(1..(n.to_i // arity.to_i + 1)).to_a.each do |t|
|
||||
|
@ -85,11 +73,11 @@ class Huffman
|
|||
result
|
||||
end
|
||||
|
||||
def arity
|
||||
private def arity
|
||||
alphabet.size
|
||||
end
|
||||
|
||||
def build_heap
|
||||
private def build_heap
|
||||
queue = PriorityQueue(HuffmanNode).new
|
||||
|
||||
n.times { |i| queue.push(-i.to_i, HuffmanNode.new(weight: -i, children: [] of HuffmanNode)) }
|
||||
|
@ -97,15 +85,13 @@ class Huffman
|
|||
queue
|
||||
end
|
||||
|
||||
def get_smallest(n : Int32) : Array(HuffmanNode)
|
||||
puts "n: #{n}"
|
||||
puts "queue.size: #{queue.size}"
|
||||
private def get_smallest(n : Int32) : Array(HuffmanNode)
|
||||
result = [] of HuffmanNode
|
||||
[n, queue.size].min.times.each { result.push(queue.pop) }
|
||||
result
|
||||
end
|
||||
|
||||
def new_node_from(nodes)
|
||||
private def new_node_from(nodes)
|
||||
weight = nodes.sum do |node|
|
||||
node.weight
|
||||
end
|
||||
|
@ -113,7 +99,7 @@ class Huffman
|
|||
HuffmanNode.new(weight: weight, children: nodes)
|
||||
end
|
||||
|
||||
def traverse_tree(node, path = [] of Int32, &block : (HuffmanNode, Array(Int32)) -> Nil)
|
||||
private def traverse_tree(node, path = [] of Int32, &block : (HuffmanNode, Array(Int32)) -> Nil)
|
||||
yield node, path
|
||||
|
||||
node.children.each_with_index do |child, index|
|
||||
|
@ -121,14 +107,6 @@ class Huffman
|
|||
end
|
||||
end
|
||||
|
||||
def traverse_inline(node, path = [] of Int32)
|
||||
puts "[inline] node: #{node} #{node.weight}, path: #{path}"
|
||||
|
||||
node.children.each_with_index do |child, index|
|
||||
traverse_inline(child, [*path, index])
|
||||
end
|
||||
end
|
||||
|
||||
def translate_path(path)
|
||||
path.map { |i| alphabet[i] }.join("")
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue