fix compatibility issues from 3.0 onwards

This commit is contained in:
Jorge Morante 2023-09-12 15:01:30 +02:00
parent 9cc93cc98f
commit f1ef8013a5
5 changed files with 43 additions and 10 deletions

View File

@ -119,7 +119,7 @@ module Fingers
end end
def tmux def tmux
Tmux.new Tmux.new(Fingers.config.tmux_version)
end end
# This takes care of some path expansion weirdness when opening paths that start with ~ in MacOS # This takes care of some path expansion weirdness when opening paths that start with ~ in MacOS

View File

@ -26,6 +26,8 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base
config = Fingers::Config.new config = Fingers::Config.new
config.tmux_version = `tmux -V`.chomp.split(" ").last
options.each do |option, value| options.each do |option, value|
# TODO generate an enum somehow and use an exhaustive case # TODO generate an enum somehow and use an exhaustive case
case option case option
@ -66,6 +68,7 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base
config.alphabet = ::Fingers::Config::ALPHABET_MAP[Fingers.config.keyboard_layout].split("").reject do |char| config.alphabet = ::Fingers::Config::ALPHABET_MAP[Fingers.config.keyboard_layout].split("").reject do |char|
char.match(DISALLOWED_CHARS) char.match(DISALLOWED_CHARS)
end end
config.save config.save
Fingers.reset_config Fingers.reset_config
@ -169,6 +172,6 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base
end end
def tmux def tmux
Tmux.new Tmux.new(`tmux -V`.chomp.split(" ").last)
end end
end end

View File

@ -122,7 +122,7 @@ module Fingers::Commands
end end
private getter tmux : Tmux do private getter tmux : Tmux do
Tmux.new Tmux.new(Fingers.config.tmux_version)
end end
end end
end end

View File

@ -18,6 +18,7 @@ module Fingers
property highlight_style : String property highlight_style : String
property selected_highlight_style : String property selected_highlight_style : String
property backdrop_style : String property backdrop_style : String
property tmux_version : String
FORMAT_PRINTER = TmuxStylePrinter.new FORMAT_PRINTER = TmuxStylePrinter.new
@ -67,7 +68,8 @@ module Fingers
@selected_hint_style = FORMAT_PRINTER.print("fg=green,bold"), @selected_hint_style = FORMAT_PRINTER.print("fg=green,bold"),
@selected_highlight_style = FORMAT_PRINTER.print("fg=green,dim"), @selected_highlight_style = FORMAT_PRINTER.print("fg=green,dim"),
@highlight_style = FORMAT_PRINTER.print("fg=yellow,dim"), @highlight_style = FORMAT_PRINTER.print("fg=yellow,dim"),
@backdrop_style = FORMAT_PRINTER.print("bg=black,fg=color250") @backdrop_style = FORMAT_PRINTER.print("bg=black,fg=color250"),
@tmux_version = ""
) )
end end

View File

@ -1,4 +1,5 @@
require "json" require "json"
require "semantic_version"
require "./tmux_style_printer" require "./tmux_style_printer"
def to_tmux_string(value) def to_tmux_string(value)
@ -38,7 +39,6 @@ end
# rubocop:disable Metrics/ClassLength # rubocop:disable Metrics/ClassLength
class Tmux class Tmux
class Shell class Shell
def initialize def initialize
@sh = Process.new("/bin/sh", input: :pipe, output: :pipe, error: :close) @sh = Process.new("/bin/sh", input: :pipe, output: :pipe, error: :close)
@ -109,11 +109,29 @@ class Tmux
}) })
@panes : Array(Pane) | Nil @panes : Array(Pane) | Nil
@version : SemanticVersion
#@sh : Shell def self.tmux_version_to_semver(version_string)
match = version_string.match(/(?<major>[1-9]+[0-9]*)\.(?<minor>[0-9]+)(?<patch_letter>[a-z]+)?/)
def initialize raise "Invalid tmux version #{version_string}" unless match
major = match["major"].not_nil!
minor = match["minor"].not_nil!
patch_letter = match["patch_letter"]?
if patch_letter.nil?
patch = 0
else
patch = patch_letter[0].ord - 'a'.ord + 1
end
SemanticVersion.parse("#{major}.#{minor}.#{patch}")
end
def initialize(version_string)
@sh = Shell.new @sh = Shell.new
@version = Tmux.tmux_version_to_semver(version_string)
end end
def panes : Array(Pane) def panes : Array(Pane)
@ -149,9 +167,13 @@ class Tmux
end end
def swap_panes(src_id, dst_id) def swap_panes(src_id, dst_id)
# TODO: -Z not supported on all tmux versions args = ["swap-pane", "-d", "-s", src_id, "-t", dst_id]
system(tmux, ["swap-pane", "-d", "-s", src_id, "-t", dst_id, "-Z"]) if @version >= Tmux.tmux_version_to_semver("3.1")
args << "-Z"
end
system(tmux, args)
end end
def kill_pane(id) def kill_pane(id)
@ -197,10 +219,16 @@ class Tmux
def set_buffer(value) def set_buffer(value)
return unless value return unless value
if @version >= Tmux.tmux_version_to_semver("3.2")
args = ["load-buffer", "-w", "-"]
else
args = ["load-buffer", "-"]
end
# To avoid shell escaping nightmares, we'll use Process and write directly to stdin # To avoid shell escaping nightmares, we'll use Process and write directly to stdin
cmd = Process.new( cmd = Process.new(
tmux, tmux,
["load-buffer", "-w", "-"], args,
input: :pipe, input: :pipe,
output: :pipe, output: :pipe,
error: :pipe, error: :pipe,