This commit is contained in:
Jorge Morante 2023-04-13 08:31:52 +02:00
parent c41116cdda
commit 0b0cc48a63
29 changed files with 166 additions and 174 deletions

View File

@ -1,22 +0,0 @@
require "./commands/*"
module Fingers
class Cli
def run
command, *args = ARGV
cmd = case command
when "start"
Fingers::Commands::Start.new(args)
when "load-config"
Fingers::Commands::LoadConfig.new(args)
when "send-input"
Fingers::Commands::SendInput.new(args)
when "version"
Fingers::Commands::Version.new(args)
end
cmd.run if cmd
end
end
end

View File

@ -1,10 +0,0 @@
module Fingers
abstract class Printer
abstract def print(msg : String)
abstract def flush()
end
abstract class Formatter
abstract def format(hint : String, highlight : String, selected : Bool, offset : Tuple(Int32, Int32) | Nil)
end
end

View File

@ -25,7 +25,7 @@ class TestFormatter < ::Fingers::Formatter
end
describe Fingers::Hinter do
input = "
input = "
ola ke ase
ke ase ola
ke olaola ke

View File

@ -16,4 +16,3 @@ describe Fingers::InputSocket do
end
end
end

View File

@ -5,12 +5,12 @@ def setup(
hint_format : String = "#[fg=yellow,bold]",
highlight_format : String = "#[fg=yellow]",
hint_position : String = "left",
selected_hint_format : String= "#[fg=green,bold]",
selected_hint_format : String = "#[fg=green,bold]",
selected_highlight_format : String = "#[fg=green]",
selected : Bool = false,
offset : Tuple(Int32, Int32) | Nil = nil,
hint : String = "a",
highlight : String = "yolo",
highlight : String = "yolo"
)
formatter = Fingers::MatchFormatter.new(
highlight_format: highlight_format,

View File

@ -663,7 +663,6 @@ expected_alot = [
alphabet_a = ["a", "s", "d", "f"]
alphabet_b = ["a", "s", "d", "f", "j", "k", "l", "g", "h"]
describe Huffman do
it "should work for 5" do
huffman = Huffman.new

View File

@ -20,7 +20,7 @@ describe PriorityQueue do
pr, str = pair
pq.push(pr.to_i, str.to_s)
end
until pq.empty?
until pq.empty?
results.push(pq.pop)
end

View File

@ -23,5 +23,4 @@ describe TmuxFormatPrinter do
result.should eq expected
end
end

View File

@ -9,4 +9,3 @@ describe Tmux do
puts panes
end
end

View File

@ -1,4 +1,4 @@
require "fingers/cli"
require "./fingers/cli"
module Fingers
VERSION = "0.1.0"

View File

@ -41,7 +41,7 @@ module Fingers
when ":paste:"
paste
when nil
# do nothing
# do nothing
else
shell_action
end
@ -70,7 +70,7 @@ module Fingers
end
def action_env
{ "MODIFIER" => modifier, "HINT" => hint }
{"MODIFIER" => modifier, "HINT" => hint}
end
private property action : String | Nil do

22
src/fingers/cli.cr Normal file
View File

@ -0,0 +1,22 @@
require "./commands/*"
module Fingers
class Cli
def run
command, *args = ARGV
cmd = case command
when "start"
Fingers::Commands::Start.new(args)
when "load-config"
Fingers::Commands::LoadConfig.new(args)
when "send-input"
Fingers::Commands::SendInput.new(args)
when "version"
Fingers::Commands::Version.new(args)
end
cmd.run if cmd
end
end
end

View File

@ -11,35 +11,35 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base
FINGERS_FILE_PATH = "#{ENV["HOME"]}/.fingersrc"
DEFAULT_PATTERNS = {
"ip": "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}",
"uuid": "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
"sha": "[0-9a-f]{7,128}",
"ip": "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}",
"uuid": "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
"sha": "[0-9a-f]{7,128}",
"digit": "[0-9]{4,}",
#"url": "((https?://|git@|git://|ssh://|ftp://|file:///)[^ ()"\"]+)",
"path": "(([.\\w\\-~\\$@]+)?(/[.\\w\\-@]+)+/?)"
# "url": "((https?://|git@|git://|ssh://|ftp://|file:///)[^ ()"\"]+)",
"path": "(([.\\w\\-~\\$@]+)?(/[.\\w\\-@]+)+/?)",
}
ALPHABET_MAP = {
"qwerty": "asdfqwerzxcvjklmiuopghtybn",
"qwerty-homerow": "asdfjklgh",
"qwerty-left-hand": "asdfqwerzcxv",
"qwerty-right-hand": "jkluiopmyhn",
"azerty": "qsdfazerwxcvjklmuiopghtybn",
"azerty-homerow": "qsdfjkmgh",
"azerty-left-hand": "qsdfazerwxcv",
"azerty-right-hand": "jklmuiophyn",
"qwertz": "asdfqweryxcvjkluiopmghtzbn",
"qwertz-homerow": "asdfghjkl",
"qwertz-left-hand": "asdfqweryxcv",
"qwertz-right-hand": "jkluiopmhzn",
"dvorak": "aoeuqjkxpyhtnsgcrlmwvzfidb",
"dvorak-homerow": "aoeuhtnsid",
"dvorak-left-hand": "aoeupqjkyix",
"dvorak-right-hand": "htnsgcrlmwvz",
"colemak": "arstqwfpzxcvneioluymdhgjbk",
"colemak-homerow": "arstneiodh",
"colemak-left-hand": "arstqwfpzxcv",
"colemak-right-hand": "neioluymjhk"
"qwerty": "asdfqwerzxcvjklmiuopghtybn",
"qwerty-homerow": "asdfjklgh",
"qwerty-left-hand": "asdfqwerzcxv",
"qwerty-right-hand": "jkluiopmyhn",
"azerty": "qsdfazerwxcvjklmuiopghtybn",
"azerty-homerow": "qsdfjkmgh",
"azerty-left-hand": "qsdfazerwxcv",
"azerty-right-hand": "jklmuiophyn",
"qwertz": "asdfqweryxcvjkluiopmghtzbn",
"qwertz-homerow": "asdfghjkl",
"qwertz-left-hand": "asdfqweryxcv",
"qwertz-right-hand": "jkluiopmhzn",
"dvorak": "aoeuqjkxpyhtnsgcrlmwvzfidb",
"dvorak-homerow": "aoeuhtnsid",
"dvorak-left-hand": "aoeupqjkyix",
"dvorak-right-hand": "htnsgcrlmwvz",
"colemak": "arstqwfpzxcvneioluymdhgjbk",
"colemak-homerow": "arstneiodh",
"colemak-left-hand": "arstqwfpzxcv",
"colemak-right-hand": "neioluymjhk",
}
def run
@ -49,7 +49,7 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base
setup_bindings
end
#private
# private
def parse_tmux_conf
options = shell_safe_options
@ -93,9 +93,9 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base
end
config.patterns = clean_up_patterns([
*enabled_default_patterns,
*user_defined_patterns
])
*enabled_default_patterns,
*user_defined_patterns,
])
config.alphabet = ALPHABET_MAP[Fingers.config.keyboard_layout].split("")
@ -162,7 +162,7 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base
# TODO crystal does not support responds_to in runtime i think
true
#Fingers.config.responds_to?(option_method.to_sym) || option.match(/^@fingers-pattern-\d+$/)
# Fingers.config.responds_to?(option_method.to_sym) || option.match(/^@fingers-pattern-\d+$/)
end
def ensure_cache_folder

View File

@ -21,11 +21,10 @@ module Fingers::Commands
def flush
@file.flush
#@file.print(@buf)
# @file.print(@buf)
end
end
class Start < Base
@original_options : Hash(String, String) = {} of String => String
@ -39,8 +38,8 @@ module Fingers::Commands
teardown
#tmux.swap_panes(target_pane.pane_id, fingers_window.pane_id)
#tmux.kill_window(fingers_window.window_id)
# 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!
@ -72,7 +71,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|
@ -121,7 +120,7 @@ module Fingers::Commands
Fingers::Hinter.new(
input: tmux.capture_pane(target_pane.pane_id),
width: target_pane.pane_width.to_i,
#state: state,
# state: state,
output: pane_printer
)
end

View File

@ -33,7 +33,7 @@ module Fingers
@hint_format = FORMAT_PRINTER.print("fg=yellow,bold"),
@selected_hint_format = FORMAT_PRINTER.print("fg=green,bold"),
@selected_highlight_format = FORMAT_PRINTER.print("fg=green,nobold,dim"),
@highlight_format = FORMAT_PRINTER.print("fg=yellow,nobold,dim"),
@highlight_format = FORMAT_PRINTER.print("fg=yellow,nobold,dim")
)
end

View File

@ -1,13 +1,13 @@
# TODO maybe use some xgd shite here?
module Fingers::Dirs
TMUX_PID = (ENV["TMUX"] || ",0000").split(",")[1]
TMUX_PID = (ENV["TMUX"] || ",0000").split(",")[1]
FINGERS_REPO_ROOT = Pathname.new(__dir__).parent.parent
ROOT = Path["~/.tmux"].expand(home: true)
LOG_PATH = ROOT / "fingers.log"
CACHE = ROOT / "cr-tmux-#{TMUX_PID}"
LOG_PATH = ROOT / "fingers.log"
CACHE = ROOT / "cr-tmux-#{TMUX_PID}"
CONFIG_PATH = CACHE / "fingers.config"
SOCKET_PATH = CACHE / "fingers.sock"
end

View File

@ -11,12 +11,12 @@ module Fingers
@lines : Array(String) | Nil
@pattern : Regex | Nil
@hints : Array(String) | Nil
@n_matches: Int32 | Nil
@n_matches : Int32 | Nil
def initialize(
input : String,
width : Int32,
#state,
# state,
output : Printer,
patterns = Fingers.config.patterns,
alphabet = Fingers.config.alphabet,
@ -27,7 +27,7 @@ module Fingers
@width = width
@hints_by_text = {} of String => String
@lookup_table = {} of String => String
#@state = state
# @state = state
@output = output
@formatter = formatter
@huffman = huffman
@ -39,7 +39,7 @@ module Fingers
lines[0..-2].each { |line| process_line(line, "\n") }
process_line(lines[-1], "")
#STDOUT.flush
# STDOUT.flush
output.flush
build_lookup_table!
@ -53,19 +53,19 @@ module Fingers
@matches ||= @hints_by_text.keys.uniq.flatten
end
#private
# private
private getter :hints,
:hints_by_text,
:input,
:lookup_table,
:width,
#:state,
:formatter,
:huffman,
:output,
:patterns,
:alphabet
:hints_by_text,
:input,
:lookup_table,
:width,
# :state,
:formatter,
:huffman,
:output,
:patterns,
:alphabet
def build_lookup_table!
@lookup_table = hints_by_text.invert
@ -89,18 +89,17 @@ module Fingers
def replace(match)
text = match[0]
#captured_text = match && match.named_captures["capture"] || text
# captured_text = match && match.named_captures["capture"] || text
captured_text = text
#if match.named_captures["capture"]
#match_start, match_end = match.offset(0)
#capture_start, capture_end = match.offset(:capture)
#capture_offset = [capture_start - match_start, capture_end - capture_start]
#else
#capture_offset = nil
#end
# if match.named_captures["capture"]
# match_start, match_end = match.offset(0)
# capture_start, capture_end = match.offset(:capture)
# capture_offset = [capture_start - match_start, capture_end - capture_start]
# else
# capture_offset = nil
# end
if hints_by_text.has_key?(captured_text)
hint = hints_by_text[captured_text]
@ -116,7 +115,7 @@ module Fingers
formatter.format(
hint: hint,
highlight: text,
#selected: state.selected_hints.include?(hint),
# selected: state.selected_hints.include?(hint),
selected: false,
offset: nil
)
@ -128,10 +127,10 @@ module Fingers
def n_matches : Int32
return @n_matches.as(Int32) if !@n_matches.nil?
match_set = Set(String).new
#Fingers.benchmark_stamp('counting-matches:start')
# Fingers.benchmark_stamp('counting-matches:start')
lines.each do |line|
line.scan(pattern) do |match|
@ -140,7 +139,7 @@ module Fingers
end
end
#Fingers.benchmark_stamp('counting-matches:end')
# Fingers.benchmark_stamp('counting-matches:end')
@n_matches = match_set.size

View File

@ -21,8 +21,8 @@ module Fingers
def format(hint : String, highlight : String, selected : Bool, offset : Tuple(Int32, Int32) | Nil)
before_offset(offset, highlight) +
format_offset(selected, hint, within_offset(offset, highlight)) +
after_offset(offset, highlight)
format_offset(selected, hint, within_offset(offset, highlight)) +
after_offset(offset, highlight)
end
private getter :hint_format, :highlight_format, :selected_hint_format, :selected_highlight_format, :hint_position, :reset_sequence

10
src/fingers/types.cr Normal file
View File

@ -0,0 +1,10 @@
module Fingers
abstract class Printer
abstract def print(msg : String)
abstract def flush
end
abstract class Formatter
abstract def format(hint : String, highlight : String, selected : Bool, offset : Tuple(Int32, Int32) | Nil)
end
end

View File

@ -51,19 +51,19 @@ class Huffman
puts root.weight
#traverse_inline(root)
# traverse_inline(root)
traverse_tree(root) do |node, path|
#puts "node #{node.weight} path: #{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
# private
#attr_reader :alphabet, :n, :heap
# attr_reader :alphabet, :n, :heap
def setup!(alphabet, n)
@alphabet = alphabet

View File

@ -1,19 +1,19 @@
class PriorityQueue(T)
@q : Hash(Int32, Array(T))
def initialize(data=nil)
def initialize(data = nil)
@q = Hash(Int32, Array(T)).new do |h, k|
h[k] = [] of T
end
data.each {|priority, item| @q[priority] << item} if data
data.each { |priority, item| @q[priority] << item } if data
@priorities = @q.keys.sort
end
def push(priority : Int32, item : T)
@q[priority].push(item)
@priorities = @q.keys.sort
end
def pop
p = @priorities.last
item = @q[p].shift
@ -23,36 +23,36 @@ class PriorityQueue(T)
end
item
end
def peek
unless empty?
@q[@priorities[0]][0]
end
end
def empty?
@priorities.empty?
end
def each
@q.each do |priority, items|
items.each {|item| yield priority, item}
items.each { |item| yield priority, item }
end
end
def dup
@q.each_with_object(self.class.new) do |(priority, items), obj|
items.each {|item| obj.push(priority, item)}
items.each { |item| obj.push(priority, item) }
end
end
def merge(other)
raise TypeError unless self.class == other.class
raise TypeError unless self.class == other.class
pq = dup
other.each {|priority, item| pq.push(priority, item)}
pq # return a new object
other.each { |priority, item| pq.push(priority, item) }
pq # return a new object
end
def inspect
@q.inspect
end

View File

@ -41,7 +41,7 @@ class Tmux
property pane_height : Int32
property pane_current_path : String
property pane_in_mode : Bool
#property scroll_position : Int32
# property scroll_position : Int32
property window_zoomed_flag : Bool
end
@ -57,30 +57,30 @@ class Tmux
# TODO make a macro or something
PANE_FORMAT = build_tmux_format({
pane_id: String,
window_id: String,
pane_width: Int32,
pane_height: Int32,
pane_id: String,
window_id: String,
pane_width: Int32,
pane_height: Int32,
pane_current_path: String,
pane_in_mode: Bool,
#scroll_position: Int32,
pane_in_mode: Bool,
# scroll_position: Int32,
window_zoomed_flag: Bool,
})
WINDOW_FORMAT = build_tmux_format({
window_id: String,
window_width: Int32,
window_id: String,
window_width: Int32,
window_height: Int32,
pane_id: String,
pane_tty: String,
pane_id: String,
pane_tty: String,
})
@panes : Array(Pane) | Nil
#def refresh!
#@panes = nil
#@windows = nil
#end
# def refresh!
# @panes = nil
# @windows = nil
# end
def panes : Array(Pane)
`#{tmux} list-panes -a -F '#{PANE_FORMAT}'`.chomp.split("\n").map do |pane|
@ -99,7 +99,7 @@ class Tmux
end
def new_session(name, cmd, width, height)
flags: Array(String) = [] of String
flags : Array(String) = [] of String
flags.push("-f", config_file) if config_file
@ -140,14 +140,14 @@ class Tmux
return "" unless pane
#if pane.pane_in_mode
#start_line = -pane.scroll_position.to_i
#end_line = pane.pane_height.to_i - pane.scroll_position.to_i - 1
# if pane.pane_in_mode
# start_line = -pane.scroll_position.to_i
# end_line = pane.pane_height.to_i - pane.scroll_position.to_i - 1
#`#{tmux} capture-pane -J -p -t "#{pane_id}" -S #{start_line} -E #{end_line}`
#else
`#{tmux} capture-pane -J -p -t '#{pane_id}'`.chomp
#end
# `#{tmux} capture-pane -J -p -t "#{pane_id}" -S #{start_line} -E #{end_line}`
# else
`#{tmux} capture-pane -J -p -t '#{pane_id}'`.chomp
# end
end
def create_window(name, cmd, _pane_width, _pane_height)
@ -232,9 +232,9 @@ class Tmux
def tmux
flags = [] of String
#flags.push("-L", socket_flag_value) if socket_flag_value
# flags.push("-L", socket_flag_value) if socket_flag_value
#return "tmux #{flags.join(" ")}" unless flags.empty?
# return "tmux #{flags.join(" ")}" unless flags.empty?
"tmux"
end

View File

@ -1,4 +1,3 @@
class TmuxFormatPrinter
abstract class Shell
abstract def exec(cmd)
@ -7,28 +6,28 @@ class TmuxFormatPrinter
FORMAT_SEPARATOR = /[ ,]+/
COLOR_MAP = {
black: 0,
red: 1,
green: 2,
yellow: 3,
blue: 4,
black: 0,
red: 1,
green: 2,
yellow: 3,
blue: 4,
magenta: 5,
cyan: 6,
white: 7
cyan: 6,
white: 7,
}
LAYER_MAP = {
bg: "setab",
fg: "setaf"
fg: "setaf",
}
STYLE_MAP = {
bright: "bold",
bold: "bold",
dim: "dim",
bright: "bold",
bold: "bold",
dim: "dim",
underscore: "smul",
reverse: "rev",
italics: "sitm"
reverse: "rev",
italics: "sitm",
}
class ShellExec < Shell
@ -123,7 +122,6 @@ class TmuxFormatPrinter
@shell
end
#private
#attr_reader :shell
# private
# attr_reader :shell
end