diff --git a/Gemfile b/Gemfile index c3362c9..1d7ec7f 100644 --- a/Gemfile +++ b/Gemfile @@ -1,8 +1,9 @@ -source 'https://rubygems.org' +source "https://rubygems.org" group :development do - gem 'irb' - gem 'byebug' - gem 'rspec' - gem 'rspec-retry' + gem "byebug" + gem "irb" + gem "rspec" + gem "rspec-retry" + gem "standardrb" end diff --git a/Gemfile.lock b/Gemfile.lock index 2189266..7912549 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,13 +1,20 @@ GEM remote: https://rubygems.org/ specs: + ast (2.4.2) byebug (11.1.3) diff-lcs (1.3) io-console (0.5.6) irb (1.2.4) reline (>= 0.0.1) + parallel (1.22.1) + parser (3.1.2.0) + ast (~> 2.4.1) + rainbow (3.1.1) + regexp_parser (2.5.0) reline (0.1.4) io-console (~> 0.5) + rexml (3.2.5) rspec (3.9.0) rspec-core (~> 3.9.0) rspec-expectations (~> 3.9.0) @@ -23,6 +30,27 @@ GEM rspec-retry (0.6.2) rspec-core (> 3.3) rspec-support (3.9.3) + rubocop (1.29.1) + parallel (~> 1.10) + parser (>= 3.1.0.0) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.17.0, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 1.4.0, < 3.0) + rubocop-ast (1.18.0) + parser (>= 3.1.1.0) + rubocop-performance (1.13.3) + rubocop (>= 1.7.0, < 2.0) + rubocop-ast (>= 0.4.0) + ruby-progressbar (1.11.0) + standard (1.12.1) + rubocop (= 1.29.1) + rubocop-performance (= 1.13.3) + standardrb (1.0.1) + standard + unicode-display_width (2.2.0) PLATFORMS ruby @@ -33,6 +61,7 @@ DEPENDENCIES irb rspec rspec-retry + standardrb BUNDLED WITH 2.2.2 diff --git a/Vagrantfile b/Vagrantfile index 43446a1..200bd2c 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -4,13 +4,13 @@ # requires nfs-utils on host Vagrant.configure(2) do |config| - config.vm.provider 'virtualbox' do |v| - v.customize ['modifyvm', :id, '--cpuexecutioncap', '50'] + config.vm.provider "virtualbox" do |v| + v.customize ["modifyvm", :id, "--cpuexecutioncap", "50"] end - config.vm.define 'ubuntu' do |machine| - machine.vm.box = 'ubuntu/focal64' - machine.vm.synced_folder '.', '/home/vagrant/shared' - machine.vm.provision 'shell', path: './spec/provisioning/ubuntu.sh' + config.vm.define "ubuntu" do |machine| + machine.vm.box = "ubuntu/focal64" + machine.vm.synced_folder ".", "/home/vagrant/shared" + machine.vm.provision "shell", path: "./spec/provisioning/ubuntu.sh" end end diff --git a/lib/fingers.rb b/lib/fingers.rb index 336be8d..9186092 100644 --- a/lib/fingers.rb +++ b/lib/fingers.rb @@ -1,41 +1,41 @@ # frozen_string_literal: true -require 'logger' -require 'json' -require 'singleton' -require 'timeout' -require 'socket' -require 'pathname' -require 'tmpdir' -require 'set' +require "logger" +require "json" +require "singleton" +require "timeout" +require "socket" +require "pathname" +require "tmpdir" +require "set" # Top level fingers namespace module Fingers end -require 'tmux' -require 'tmux_format_printer' -require 'huffman' -require 'priority_queue' +require "tmux" +require "tmux_format_printer" +require "huffman" +require "priority_queue" -require 'fingers/version' -require 'fingers/dirs' -require 'fingers/config' +require "fingers/version" +require "fingers/dirs" +require "fingers/config" # commands # TODO dynamically require command? -require 'fingers/commands' -require 'fingers/commands/base' -require 'fingers/commands/check_version' -require 'fingers/commands/load_config' -require 'fingers/commands/send_input' -require 'fingers/commands/start' -require 'fingers/commands/trace_start' +require "fingers/commands" +require "fingers/commands/base" +require "fingers/commands/check_version" +require "fingers/commands/load_config" +require "fingers/commands/send_input" +require "fingers/commands/start" +require "fingers/commands/trace_start" -require 'fingers/action_runner' -require 'fingers/hinter' -require 'fingers/input_socket' -require 'fingers/logger' -require 'fingers/view' -require 'fingers/match_formatter' -require 'fingers/cli' +require "fingers/action_runner" +require "fingers/hinter" +require "fingers/input_socket" +require "fingers/logger" +require "fingers/view" +require "fingers/match_formatter" +require "fingers/cli" diff --git a/lib/fingers/action_runner.rb b/lib/fingers/action_runner.rb index 7e43ccd..9796bd9 100644 --- a/lib/fingers/action_runner.rb +++ b/lib/fingers/action_runner.rb @@ -25,23 +25,23 @@ class Fingers::ActionRunner return @final_shell_command if @final_shell_command @final_shell_command = case action - when ':copy:' - copy - when ':open:' - open - when ':paste:' - paste - when nil - # do nothing - else - shell_action - end + when ":copy:" + copy + when ":open:" + open + when ":paste:" + paste + when nil + # do nothing + else + shell_action + end @final_shell_command = prepend_pane_path(@final_shell_command) end def prepend_pane_path(cmd) - return if (cmd || '').empty? + return if (cmd || "").empty? "cd #{original_pane.pane_current_path}; #{cmd}" end @@ -61,7 +61,7 @@ class Fingers::ActionRunner end def paste - 'tmux paste-buffer' + "tmux paste-buffer" end def shell_action @@ -69,7 +69,7 @@ class Fingers::ActionRunner end def action_env - { 'MODIFIER' => modifier, 'HINT' => hint } + {"MODIFIER" => modifier, "HINT" => hint} end def action @@ -77,33 +77,33 @@ class Fingers::ActionRunner end def system_copy_command - @system_copy_command ||= if program_exists?('pbcopy') - if program_exists?('reattach-to-user-namespace') - 'reattach-to-user-namespace' - else - 'pbcopy' - end - elsif program_exists?('clip.exe') - 'cat | clip.exe' - elsif program_exists?('wl-copy') - 'wl-copy' - elsif program_exists?('xclip') - 'xclip -selection clipboard' - elsif program_exists?('xsel') - 'xsel -i --clipboard' - elsif program_exists?('putclip') - 'putclip' - end + @system_copy_command ||= if program_exists?("pbcopy") + if program_exists?("reattach-to-user-namespace") + "reattach-to-user-namespace" + else + "pbcopy" + end + elsif program_exists?("clip.exe") + "cat | clip.exe" + elsif program_exists?("wl-copy") + "wl-copy" + elsif program_exists?("xclip") + "xclip -selection clipboard" + elsif program_exists?("xsel") + "xsel -i --clipboard" + elsif program_exists?("putclip") + "putclip" + end end def system_open_command - @system_open_command ||= if program_exists?('cygstart') - 'xargs cygstart' - elsif program_exists?('xdg-open') - 'xargs xdg-open' - elsif program_exists?('open') - 'xargs open' - end + @system_open_command ||= if program_exists?("cygstart") + "xargs cygstart" + elsif program_exists?("xdg-open") + "xargs xdg-open" + elsif program_exists?("open") + "xargs open" + end end def program_exists?(program) diff --git a/lib/fingers/cli.rb b/lib/fingers/cli.rb index 4055a40..4020288 100755 --- a/lib/fingers/cli.rb +++ b/lib/fingers/cli.rb @@ -8,26 +8,26 @@ module Fingers end def run - Fingers.benchmark_stamp('boot:end') if ARGV[0] == 'start' + Fingers.benchmark_stamp("boot:end") if ARGV[0] == "start" command_class = case ARGV[0] - when 'start' - Fingers::Commands::Start - when 'check_version' - Fingers::Commands::CheckVersion - when 'send_input' - Fingers::Commands::SendInput - when 'load_config' - Fingers::Commands::LoadConfig - when 'trace_start' - Fingers::Commands::TraceStart - else - raise "Unknown command #{ARGV[0]}" - end + when "start" + Fingers::Commands::Start + when "check_version" + Fingers::Commands::CheckVersion + when "send_input" + Fingers::Commands::SendInput + when "load_config" + Fingers::Commands::LoadConfig + when "trace_start" + Fingers::Commands::TraceStart + else + raise "Unknown command #{ARGV[0]}" + end begin command_class.new(args, cli_path).run - rescue StandardError => e + rescue => e Fingers.logger.error(e) end end diff --git a/lib/fingers/commands/check_version.rb b/lib/fingers/commands/check_version.rb index 02b35e3..401e585 100644 --- a/lib/fingers/commands/check_version.rb +++ b/lib/fingers/commands/check_version.rb @@ -1,19 +1,19 @@ class Fingers::Commands::CheckVersion < Fingers::Commands::Base def run - require 'net/https' + require "net/https" - puts 'Checking version...' - uri = URI('https://api.github.com/repos/morantron/tmux-fingers/tags') + puts "Checking version..." + uri = URI("https://api.github.com/repos/morantron/tmux-fingers/tags") response = Net::HTTP.get_response(uri) json_response = JSON.parse(response.body) - latest_release = json_response.map { |tag| Gem::Version.new(tag['name']) }.max + latest_release = json_response.map { |tag| Gem::Version.new(tag["name"]) }.max current_release = Gem::Version.new(Fingers::VERSION) puts "There is a new tmux-fingers release: #{latest_release}" if latest_release > current_release - rescue StandardError => e - puts 'Could not check version' + rescue => e + puts "Could not check version" end end diff --git a/lib/fingers/commands/load_config.rb b/lib/fingers/commands/load_config.rb index e68775e..21bf19f 100644 --- a/lib/fingers/commands/load_config.rb +++ b/lib/fingers/commands/load_config.rb @@ -1,38 +1,38 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base - DISALLOWED_CHARS = /cimqn/.freeze + DISALLOWED_CHARS = /cimqn/ - FINGERS_FILE_PATH = "#{ENV['HOME']}/.fingersrc" + 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}', - "digit": '[0-9]{4,}', - "url": "((https?://|git@|git://|ssh://|ftp://|file:///)[^ ()'\"]+)", - "path": '(([.\\w\\-~\\$@]+)?(/[.\\w\\-@]+)+/?)' + 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\\-@]+)+/?)' }.freeze 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" }.freeze def run @@ -56,9 +56,9 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base option = option - if option.match(/pattern/) + if /pattern/.match?(option) user_defined_patterns.push(value) - elsif option.match(/format/) + elsif /format/.match?(option) parsed_format = Tmux.instance.parse_format(value) Fingers.config.send("#{option}=".to_sym, parsed_format) @@ -68,21 +68,20 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base end Fingers.config.patterns = clean_up_patterns([ - *enabled_default_patterns, - *user_defined_patterns - ]) + *enabled_default_patterns, + *user_defined_patterns + ]) - Fingers.config.alphabet = ALPHABET_MAP[Fingers.config.keyboard_layout.to_sym].split('') + Fingers.config.alphabet = ALPHABET_MAP[Fingers.config.keyboard_layout.to_sym].split("") fingers_file_require_path = File.expand_path(FINGERS_FILE_PATH, __dir__) Fingers.logger.debug("Config: #{FINGERS_FILE_PATH}") Fingers.logger.debug("fingers_file_require_path: #{fingers_file_require_path}") - if File.exist?(FINGERS_FILE_PATH) `cp #{FINGERS_FILE_PATH} /tmp/fingersrc.rb` - require "/tmp/fingersrc.rb" + require "/tmp/fingersrc" `rm /tmp/fingersrc.rb` end @@ -94,17 +93,17 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base end def setup_bindings - input_mode = 'fingers-mode' + input_mode = "fingers-mode" ruby_bin = "#{RbConfig.ruby} --disable-gems" `tmux bind-key #{Fingers.config.key} run-shell -b "#{ruby_bin} #{cli} start '#{input_mode}' '\#{pane_id}' self >>#{Fingers::Dirs::LOG_PATH} 2>&1"` `tmux bind-key O run-shell -b "#{ruby_bin} #{cli} start '#{input_mode}' '\#{pane_id}' other >>#{Fingers::Dirs::LOG_PATH} 2>&1"` - setup_fingers_mode_bindings if input_mode == 'fingers-mode' + setup_fingers_mode_bindings if input_mode == "fingers-mode" end def setup_fingers_mode_bindings - ('a'..'z').to_a.each do |char| + ("a".."z").to_a.each do |char| next if char.match(DISALLOWED_CHARS) fingers_mode_bind(char, "hint:#{char}:main") @@ -113,17 +112,17 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base fingers_mode_bind("M-#{char}", "hint:#{char}:alt") end - fingers_mode_bind('Space', 'fzf') - fingers_mode_bind('C-c', 'exit') - fingers_mode_bind('q', 'exit') - fingers_mode_bind('Escape', 'exit') + fingers_mode_bind("Space", "fzf") + fingers_mode_bind("C-c", "exit") + fingers_mode_bind("q", "exit") + fingers_mode_bind("Escape", "exit") - fingers_mode_bind('?', 'toggle-help') + fingers_mode_bind("?", "toggle-help") - fingers_mode_bind('Enter', 'noop') - fingers_mode_bind('Tab', 'toggle_multi_mode') + fingers_mode_bind("Enter", "noop") + fingers_mode_bind("Tab", "toggle_multi_mode") - fingers_mode_bind('Any', 'noop') + fingers_mode_bind("Any", "noop") end def enabled_default_patterns @@ -131,7 +130,7 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base end def to_bool(input) - input == '1' + input == "1" end def shell_safe_options @@ -153,12 +152,12 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base end def ensure_cache_folder - require 'fileutils' + require "fileutils" FileUtils.mkdir_p(Fingers::Dirs::CACHE) unless File.exist?(Fingers::Dirs::CACHE) end def fingers_options_names - @fingers_options_names ||= `tmux show-options -g | grep ^@fingers`.split("\n").map { |line| line.split(' ')[0] } + @fingers_options_names ||= `tmux show-options -g | grep ^@fingers`.split("\n").map { |line| line.split(" ")[0] } end def unset_tmux_option!(option) @@ -177,13 +176,13 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base return if errors.empty? - puts '[tmux-fingers] Errors found in tmux.conf:' + puts "[tmux-fingers] Errors found in tmux.conf:" errors.each { |error| puts " - #{error}" } exit(1) end def option_to_method(option) - option.gsub(/^@fingers-/, '').tr('-', '_') + option.gsub(/^@fingers-/, "").tr("-", "_") end def fingers_mode_bind(key, command) diff --git a/lib/fingers/commands/start.rb b/lib/fingers/commands/start.rb index 8d36f46..1025092 100644 --- a/lib/fingers/commands/start.rb +++ b/lib/fingers/commands/start.rb @@ -1,17 +1,17 @@ class PanePrinter def initialize(pane_tty) @pane_tty = pane_tty - @buf = '' - @file = File.open(@pane_tty, 'w') + @buf = "" + @file = File.open(@pane_tty, "w") end def print(msg) @file.print(msg) - #@buf += msg + # @buf += msg end def flush - #@file.print(@buf) + # @file.print(@buf) end end @@ -58,7 +58,7 @@ class Fingers::Commands::Start < Fingers::Commands::Base end def fingers_window - @fingers_window ||= tmux.create_window('[fingers]', 'cat', 80, 24) + @fingers_window ||= tmux.create_window("[fingers]", "cat", 80, 24) end def target_pane @@ -94,8 +94,8 @@ class Fingers::Commands::Start < Fingers::Commands::Base @state.multi_mode = false @state.show_help = false - @state.input = '' - @state.modifier = '' + @state.input = "" + @state.modifier = "" @state.selected_hints = [] @state.selected_matches = [] @state.multi_matches = [] @@ -119,12 +119,12 @@ class Fingers::Commands::Start < Fingers::Commands::Base input_socket = InputSocket.new tmux.disable_prefix - tmux.set_key_table 'fingers' + tmux.set_key_table "fingers" - Fingers.benchmark_stamp('ready-for-input:end') - Fingers.trace_for_tests_do_not_remove_or_the_whole_fabric_of_reality_will_tear_apart_with_unforeseen_consequences('fingers-ready') + Fingers.benchmark_stamp("ready-for-input:end") + Fingers.trace_for_tests_do_not_remove_or_the_whole_fabric_of_reality_will_tear_apart_with_unforeseen_consequences("fingers-ready") - return if Fingers.config.trace_perf == '1' + return if Fingers.config.trace_perf == "1" input_socket.on_input do |input| view.process_input(input) @@ -153,11 +153,11 @@ class Fingers::Commands::Start < Fingers::Commands::Base end def pane_was_zoomed? - target_pane.window_zoomed_flag == '1' + target_pane.window_zoomed_flag == "1" end def teardown - tmux.set_key_table 'root' + tmux.set_key_table "root" tmux.swap_panes(fingers_pane_id, target_pane.pane_id) tmux.kill_pane(fingers_pane_id) @@ -167,7 +167,7 @@ class Fingers::Commands::Start < Fingers::Commands::Base restore_options view.run_action if state.result - Fingers.trace_for_tests_do_not_remove_or_the_whole_fabric_of_reality_will_tear_apart_with_unforeseen_consequences('fingers-finish') + Fingers.trace_for_tests_do_not_remove_or_the_whole_fabric_of_reality_will_tear_apart_with_unforeseen_consequences("fingers-finish") end def compute_target_pane diff --git a/lib/fingers/config.rb b/lib/fingers/config.rb index 4c0501c..1a34df6 100644 --- a/lib/fingers/config.rb +++ b/lib/fingers/config.rb @@ -18,20 +18,20 @@ module Fingers :trace_perf ) do def initialize( - key = 'F', - keyboard_layout = 'qwerty', + key = "F", + keyboard_layout = "qwerty", alphabet = [], patterns = [], - main_action = ':copy:', - ctrl_action = ':open:', - alt_action = '', - shift_action = ':paste:', - hint_position = 'left', - hint_format = Tmux.instance.parse_format('fg=yellow,bold'), - selected_hint_format = Tmux.instance.parse_format('fg=green,bold'), - selected_highlight_format = Tmux.instance.parse_format('fg=green,nobold,dim'), - highlight_format = Tmux.instance.parse_format('fg=yellow,nobold,dim'), - trace_perf = '0' + main_action = ":copy:", + ctrl_action = ":open:", + alt_action = "", + shift_action = ":paste:", + hint_position = "left", + hint_format = Tmux.instance.parse_format("fg=yellow,bold"), + selected_hint_format = Tmux.instance.parse_format("fg=green,bold"), + selected_highlight_format = Tmux.instance.parse_format("fg=green,nobold,dim"), + highlight_format = Tmux.instance.parse_format("fg=yellow,nobold,dim"), + trace_perf = "0" ) super end @@ -43,7 +43,7 @@ module Fingers def self.config $config ||= Fingers.load_from_cache - rescue StandardError + rescue $config ||= ConfigStruct.new end @@ -52,15 +52,13 @@ module Fingers end def self.save_config - File.open(CONFIG_PATH, 'w') do |f| - f.write(Marshal.dump(Fingers.config)) - end + File.write(CONFIG_PATH, Marshal.dump(Fingers.config)) end def self.load_from_cache - Fingers.benchmark_stamp('load-config-from-cache:start') + Fingers.benchmark_stamp("load-config-from-cache:start") result = Marshal.load(File.open(CONFIG_PATH)) - Fingers.benchmark_stamp('load-config-from-cache:end') + Fingers.benchmark_stamp("load-config-from-cache:end") result end diff --git a/lib/fingers/dirs.rb b/lib/fingers/dirs.rb index b27adb6..b707e74 100644 --- a/lib/fingers/dirs.rb +++ b/lib/fingers/dirs.rb @@ -1,11 +1,11 @@ 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 = Pathname.new(Dir.tmpdir) / 'tmux-fingers' + root = Pathname.new(Dir.tmpdir) / "tmux-fingers" - LOG_PATH = FINGERS_REPO_ROOT / 'fingers.log' + LOG_PATH = FINGERS_REPO_ROOT / "fingers.log" CACHE = root / "tmux-#{tmux_pid}" - CONFIG_PATH = CACHE / 'fingers.config' - SOCKET_PATH = CACHE / 'fingers.sock' + CONFIG_PATH = CACHE / "fingers.config" + SOCKET_PATH = CACHE / "fingers.sock" end diff --git a/lib/fingers/hinter.rb b/lib/fingers/hinter.rb index cf9aeac..e4398c8 100644 --- a/lib/fingers/hinter.rb +++ b/lib/fingers/hinter.rb @@ -3,9 +3,8 @@ class ::Fingers::Hinter input:, width:, state:, - patterns: Fingers.config.patterns, + output:, patterns: Fingers.config.patterns, alphabet: Fingers.config.alphabet, - output:, huffman: Huffman.new, formatter: ::Fingers::MatchFormatter.new ) @@ -22,7 +21,7 @@ class ::Fingers::Hinter def run lines[0..-2].each { |line| process_line(line, "\n") } - process_line(lines[-1], '') + process_line(lines[-1], "") STDOUT.flush output.flush @@ -41,16 +40,16 @@ class ::Fingers::Hinter private attr_reader :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 @@ -62,7 +61,7 @@ class ::Fingers::Hinter end def pattern - @pattern ||= Regexp.compile("(#{patterns.join('|')})") + @pattern ||= Regexp.compile("(#{patterns.join("|")})") end def hints @@ -74,9 +73,9 @@ class ::Fingers::Hinter def replace(match) text = match[0] - captured_text = match && match.named_captures['capture'] || text + captured_text = match && match.named_captures["capture"] || text - if match.named_captures['capture'] + if match.named_captures["capture"] match_start, match_end = match.offset(0) capture_start, capture_end = match.offset(:capture) @@ -85,7 +84,6 @@ class ::Fingers::Hinter capture_offset = nil end - if hints_by_text.has_key?(captured_text) hint = hints_by_text[captured_text] else @@ -103,7 +101,7 @@ class ::Fingers::Hinter end def lines - @lines ||= input.force_encoding('UTF-8').split("\n") + @lines ||= input.force_encoding("UTF-8").split("\n") end def n_matches @@ -111,7 +109,7 @@ class ::Fingers::Hinter match_set = ::Set.new - Fingers.benchmark_stamp('counting-matches:start') + Fingers.benchmark_stamp("counting-matches:start") lines.each do |line| line.scan(pattern) do |match| @@ -119,7 +117,7 @@ class ::Fingers::Hinter end end - Fingers.benchmark_stamp('counting-matches:end') + Fingers.benchmark_stamp("counting-matches:end") @n_matches = match_set.length diff --git a/lib/fingers/input_socket.rb b/lib/fingers/input_socket.rb index 9c23ad3..8254720 100644 --- a/lib/fingers/input_socket.rb +++ b/lib/fingers/input_socket.rb @@ -10,7 +10,7 @@ class InputSocket socket = server.accept message = socket.readline - next if message == 'ping' + next if message == "ping" yield message end @@ -23,7 +23,7 @@ class InputSocket end def wait_for_input - send_message 'ping' + send_message "ping" rescue Errno::ENOENT retry end diff --git a/lib/fingers/logger.rb b/lib/fingers/logger.rb index fa1deea..56159ba 100644 --- a/lib/fingers/logger.rb +++ b/lib/fingers/logger.rb @@ -1,4 +1,4 @@ -require 'logger' +require "logger" module Fingers def self.logger @@ -7,7 +7,7 @@ module Fingers @logger = Logger.new( Fingers::Dirs::LOG_PATH ) - @logger.level = Logger.const_get(ENV.fetch('FINGERS_LOG_LEVEL', 'INFO')) + @logger.level = Logger.const_get(ENV.fetch("FINGERS_LOG_LEVEL", "INFO")) @logger end diff --git a/lib/fingers/match_formatter.rb b/lib/fingers/match_formatter.rb index 601078d..bd77780 100644 --- a/lib/fingers/match_formatter.rb +++ b/lib/fingers/match_formatter.rb @@ -17,8 +17,8 @@ class ::Fingers::MatchFormatter def format(hint:, highlight:, selected:, offset: 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 @@ -57,7 +57,7 @@ class ::Fingers::MatchFormatter end def chop_highlight(hint, highlight) - if hint_position == 'right' + if hint_position == "right" highlight[0..-(hint.length + 1)] || "" else highlight[hint.length..-1] || "" diff --git a/lib/fingers/version.rb b/lib/fingers/version.rb index 6bc1c4b..cd07a2b 100644 --- a/lib/fingers/version.rb +++ b/lib/fingers/version.rb @@ -1,3 +1,3 @@ module Fingers - VERSION = '2.0.0'.freeze + VERSION = "2.0.0".freeze end diff --git a/lib/fingers/view.rb b/lib/fingers/view.rb index d380ecc..2e52ff4 100644 --- a/lib/fingers/view.rb +++ b/lib/fingers/view.rb @@ -9,7 +9,7 @@ class Fingers::View end def process_input(input) - command, *args = input.gsub(/-/, '_').split(':') + command, *args = input.tr("-", "_").split(":") send("#{command}_message".to_sym, *args) end @@ -42,10 +42,11 @@ class Fingers::View def toggle_help_message output.print CLEAR_ESCAPE_SEQUENCE - output.print 'Help message' + output.print "Help message" end - def noop_message; end + def noop_message + end def toggle_multi_mode_message prev_state = state.multi_mode @@ -53,7 +54,7 @@ class Fingers::View current_state = state.multi_mode if prev_state == true && current_state == false - state.result = state.multi_matches.join(' ') + state.result = state.multi_matches.join(" ") request_exit! end end @@ -73,7 +74,7 @@ class Fingers::View end def fzf_message - file = File.open('/tmp/fingers_fzf', 'w') + file = File.open("/tmp/fingers_fzf", "w") hinter.matches.each do |match| file.puts match end @@ -86,7 +87,7 @@ class Fingers::View if state.multi_mode state.multi_matches << match state.selected_hints << state.input - state.input = '' + state.input = "" render else state.result = match diff --git a/lib/huffman.rb b/lib/huffman.rb index f97379b..345f046 100644 --- a/lib/huffman.rb +++ b/lib/huffman.rb @@ -111,6 +111,6 @@ class Huffman end def translate_path(path) - path.map { |i| alphabet[i] }.join('') + path.map { |i| alphabet[i] }.join("") end end diff --git a/lib/tmux.rb b/lib/tmux.rb index 27e8fd2..438ad11 100644 --- a/lib/tmux.rb +++ b/lib/tmux.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -# rubocop:disable Metrics/ClassLength class Tmux include Singleton @@ -58,34 +57,34 @@ class Tmux def new_session(name, cmd, width, height) flags = [] - flags.push('-f', config_file) if config_file + flags.push("-f", config_file) if config_file - `env -u TMUX #{tmux} #{flags.join(' ')} new-session -d -s #{name} -x #{width} -y #{height} '#{cmd}'` + `env -u TMUX #{tmux} #{flags.join(" ")} new-session -d -s #{name} -x #{width} -y #{height} '#{cmd}'` end def start_server flags = [] - flags.push('-f', config_file) if config_file + flags.push("-f", config_file) if config_file - `#{tmux} #{flags.join(' ')} start-server &` + `#{tmux} #{flags.join(" ")} start-server &` end def pane_by_id(id) - panes.find { |pane| pane['pane_id'] == id } + panes.find { |pane| pane["pane_id"] == id } end def window_by_id(id) - windows.find { |window| window['window_id'] == id } + windows.find { |window| window["window_id"] == id } end def panes_by_window_id(window_id) - panes.select { |pane| pane['window_id'] == window_id } + panes.select { |pane| pane["window_id"] == window_id } end def pane_exec(pane_id, cmd) send_keys(pane_id, " #{cmd}") - send_keys(pane_id, 'Enter') + send_keys(pane_id, "Enter") end def send_keys(pane_id, keys) @@ -95,7 +94,7 @@ class Tmux def capture_pane(pane_id) pane = pane_by_id(pane_id) - if pane.pane_in_mode == '1' + if pane.pane_in_mode == "1" start_line = -pane.scroll_position.to_i end_line = pane.pane_height.to_i - pane.scroll_position.to_i - 1 @@ -117,7 +116,7 @@ class Tmux def swap_panes(src_id, dst_id) # TODO: -Z not supported on all tmux versions - system(tmux, 'swap-pane', '-d', '-s', src_id, '-t', dst_id) + system(tmux, "swap-pane", "-d", "-s", src_id, "-t", dst_id) end def kill_pane(id) @@ -130,12 +129,12 @@ class Tmux # TODO: this command is version dependant D: def resize_window(window_id, width, height) - system(tmux, 'resize-window', '-t', window_id, '-x', width.to_s, '-y', height.to_s) + system(tmux, "resize-window", "-t", window_id, "-x", width.to_s, "-y", height.to_s) end # TODO: this command is version dependant D: def resize_pane(pane_id, width, height) - system(tmux, 'resize-pane', '-t', pane_id, '-x', width.to_s, '-y', height.to_s) + system(tmux, "resize-pane", "-t", pane_id, "-x", width.to_s, "-y", height.to_s) end def last_pane_id @@ -143,21 +142,21 @@ class Tmux end def set_window_option(name, value) - system(tmux, 'set-window-option', name, value) + system(tmux, "set-window-option", name, value) end def set_key_table(table) - system(tmux, 'set-window-option', 'key-table', table) - system(tmux, 'switch-client', '-T', table) + system(tmux, "set-window-option", "key-table", table) + system(tmux, "switch-client", "-T", table) end def disable_prefix - set_global_option('prefix', 'None') - set_global_option('prefix2', 'None') + set_global_option("prefix", "None") + set_global_option("prefix2", "None") end def set_global_option(name, value) - system(tmux, 'set-option', '-g', name, value) + system(tmux, "set-option", "-g", name, value) end def get_global_option(name) @@ -167,15 +166,15 @@ class Tmux def set_buffer(value) return unless value - system(tmux, 'set-buffer', value) + system(tmux, "set-buffer", value) end def select_pane(id) - system(tmux, 'select-pane', '-t', id) + system(tmux, "select-pane", "-t", id) end def zoom_pane(id) - system(tmux, 'resize-pane', '-Z', '-t', id) + system(tmux, "resize-pane", "-Z", "-t", id) end def parse_format(format) @@ -191,29 +190,29 @@ class Tmux def tmux flags = [] - 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' + "tmux" end def build_tmux_output_format(fields) - fields.map { |field| format("\#{%s}", field: field) }.join(';') + fields.map { |field| format("\#{%s}", field: field) }.join(";") end def parse_tmux_formatted_output(output) output.split("\n").map do |line| - fields = line.split(';') + fields = line.split(";") yield fields end end def socket_flag_value - return ENV['FINGERS_TMUX_SOCKET'] if ENV['FINGERS_TMUX_SOCKET'] + return ENV["FINGERS_TMUX_SOCKET"] if ENV["FINGERS_TMUX_SOCKET"] socket end end -#Tmux = TmuxControl +# Tmux = TmuxControl # rubocop:enable Metrics/ClassLength diff --git a/lib/tmux_format_printer.rb b/lib/tmux_format_printer.rb index ad1a27d..cb3d1d2 100644 --- a/lib/tmux_format_printer.rb +++ b/lib/tmux_format_printer.rb @@ -1,5 +1,5 @@ class TmuxFormatPrinter - FORMAT_SEPARATOR = /[ ,]+/.freeze + FORMAT_SEPARATOR = /[ ,]+/ COLOR_MAP = { black: 0, @@ -13,17 +13,17 @@ class TmuxFormatPrinter }.freeze LAYER_MAP = { - bg: 'setab', - fg: 'setaf' + bg: "setab", + fg: "setaf" }.freeze STYLE_MAP = { - bright: 'bold', - bold: 'bold', - dim: 'dim', - underscore: 'smul', - reverse: 'rev', - italics: 'sitm' + bright: "bold", + bold: "bold", + dim: "dim", + underscore: "smul", + reverse: "rev", + italics: "sitm" }.freeze class ShellExec @@ -39,7 +39,7 @@ class TmuxFormatPrinter def print(input, reset_styles_after: false) @applied_styles = {} - output = '' + output = "" input.split(FORMAT_SEPARATOR).each do |format| output += parse_format(format) @@ -51,7 +51,7 @@ class TmuxFormatPrinter end def parse_format(format) - if format.match(/^(bg|fg)=/) + if /^(bg|fg)=/.match?(format) parse_color(format) else parse_style(format) @@ -65,7 +65,7 @@ class TmuxFormatPrinter color = match[:color].to_sym color_code = match[:color_code] - if match[:color] == 'default' + if match[:color] == "default" @applied_styles.delete(layer) return reset_to_applied_styles! end @@ -81,7 +81,7 @@ class TmuxFormatPrinter def parse_style(format) match = format.match(/(?no)?(?