libtmux 0.1.0.alpha.1 → 0.1.0.alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/assets/logo.png ADDED
Binary file
data/assets/logo.svg ADDED
@@ -0,0 +1,2 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <svg xmlns="http://www.w3.org/2000/svg" width="512" height="512" viewBox="0 0 135.46667 135.46666" role="img" aria-label="libtmux for Ruby" class="libtmux-logo" data-language="ruby" data-variant="library" focusable="false"><title>libtmux for Ruby</title><g transform="translate(-210.28959,-409.98108)" data-part="artwork"><g transform="matrix(1.024,0,0,1.0240011,-5.0469507,-9.8399997)" data-part="primary-tile"><path d="M 227.78906,0 213.98242,3.6992188 V 460.62109 H 241.4082 V 15.564453 Z" transform="matrix(0.26458333,0,0,0.26458333,210.28959,409.98108)" fill="#2D0002" data-part="primary-edge"></path><path d="m 210.28981,417.13553 v 113.86425 l 60.26885,2.60089 V 409.98109 h -53.11441 c -3.95129,0 -7.15444,3.20315 -7.15444,7.15444 z" fill="#701516" data-part="primary"></path></g><g transform="matrix(1.024,0,0,1.0240011,-5.0469507,-9.8399997)" data-part="lower-tile"><path d="m 486.38086,246.03906 -5.24805,4.99805 v 207.2207 l 5.24805,-0.93164 L 500,465.18945 V 261.60352 Z" transform="matrix(0.26458333,0,0,0.26458333,210.28959,409.98108)" fill="#67221F" data-part="lower-edge"></path><path d="m 278.70874,475.07917 v 64.8875 l 60.26883,-8.98455 v -55.90295 z" fill="#984640" data-part="lower"></path></g><path d="m 210.28959,533.88585 v 4.23589 c 9e-5,4.04619 3.2803,7.32617 7.32649,7.32598 h 28.63105 l 81.26967,-1.0223 14.5495,-10.53906 v 0 z" fill="#231F20" data-part="base"></path><g transform="matrix(1.024,0,0,1.0240011,-5.0469507,-9.8399997)" data-part="upper-tile"><path d="m 459.33984,0 -25.33593,2.1074219 h 21.38281 c 14.93397,0 27.04101,12.1070061 27.04101,27.0410161 V 129.83008 l 6.17579,13.24609 L 500,112.43359 V 42.603516 C 499.99913,8.8176763 474.27408,-6.8031496e-4 459.33984,0 Z" transform="matrix(0.26458333,0,0,0.26458333,210.28959,409.98108)" fill="#854A46" data-part="upper-side"></path><path d="m 331.82331,409.98109 h -53.11442 v 56.93261 h 60.26885 v -49.77817 c 0,-3.95129 -3.20315,-7.15444 -7.15443,-7.15444 z" fill="#B6706A" data-part="upper"></path><path d="m 278.7089,466.9137 h 60.26885 l 3.6035,4.11782 h -60.26932 z" fill="#733A36" data-part="upper-bottom"></path></g><g transform="matrix(1.024,0,0,1.0240011,-5.0469507,-9.8399997)" data-part="gear-group"><path d="m 298.35661,419.37889 c -3.307,0 -6.01087,2.63649 -6.0946,5.94228 l -0.33026,13.04677 c -0.0503,1.98883 -1.3048,3.74588 -3.16843,4.44175 -1.63527,0.6105 -3.23561,1.29298 -4.79768,2.04293 -1.82786,0.87796 -4.00547,0.54134 -5.47527,-0.85609 l -9.17457,-8.72115 c -2.39692,-2.27863 -6.17361,-2.23122 -8.512,0.10759 l -14.3945,14.39447 c -1.27625,1.27604 -1.86544,2.98029 -1.77147,4.65491 -0.0118,0.22021 -0.0123,0.44029 0,0.66053 -0.0118,0.22023 -0.0123,0.44029 0,0.66053 -0.0118,0.22021 -0.0123,0.44029 0,0.66053 -0.0118,0.22024 -0.0123,0.44029 0,0.66053 -0.0118,0.22024 -0.0123,0.44029 0,0.66053 -0.0118,0.22024 -0.0123,0.44029 0,0.66053 -0.0118,0.22024 -0.0123,0.44029 0,0.66053 -0.0838,1.61437 0.46467,3.25669 1.6634,4.51768 l 8.99461,9.46208 c 0.23074,0.24278 0.42181,0.51038 0.59418,0.78721 -0.60023,1.35769 -1.15279,2.74067 -1.64984,4.15071 -0.6727,1.90958 -2.45019,3.20822 -4.47443,3.25942 l -12.65165,0.31971 c -3.30618,0.0837 -5.94279,2.7886 -5.94279,6.0956 v 0.66053 0.66053 0.66053 0.66056 2.64112 15.07361 0.66053 0.66053 0.66053 0.66053 0.66053 0.66053 0.66053 0.66053 c 0,3.30703 2.63661,6.01136 5.94279,6.09511 l 13.04677,0.32975 c 1.98885,0.0503 3.74539,1.30529 4.44124,3.16895 0.53531,1.43379 1.12957,2.83886 1.7725,4.21655 -0.17026,0.27145 -0.35881,0.53398 -0.58564,0.77264 l -8.72163,9.17405 c -1.19922,1.26148 -1.74723,2.90475 -1.66291,4.51969 -0.0124,0.22024 -0.0118,0.44029 0,0.66053 v 0.0457 h 0.0875 l 10.03868,-9.51839 29.63949,8.95286 h 43.47603 l 13.935,-16.69426 v -0.0216 c -8.04912,9.72841 -20.24315,15.90068 -33.88319,15.7844 -22.83086,-0.19494 -41.62302,-18.24181 -43.0251,-40.7806 1.34121,-22.94291 20.4588,-41.11551 43.78415,-40.91631 13.2902,0.11325 25.20451,6.28323 33.12414,15.86034 v -3.90741 -20.27843 -11.02398 l -3.97024,3.7742 c -1.44419,1.37264 -3.57681,1.72648 -5.38882,0.89729 -1.57465,-0.72024 -3.18724,-1.3729 -4.83384,-1.95295 -1.90955,-0.67268 -3.2077,-2.45012 -3.25892,-4.47392 l -0.32023,-12.65265 c -0.0836,-3.30579 -2.78757,-5.94228 -6.09457,-5.94228 z" fill="#999999" data-part="gear-edge"></path><path d="m 298.35715,419.3789 c -3.30703,0 -6.01139,2.63697 -6.09511,5.94277 l -0.33075,13.04677 c -0.0504,1.98885 -1.30431,3.74539 -3.16795,4.44126 -1.63521,0.6105 -3.23553,1.29298 -4.79716,2.04293 -1.8283,0.87755 -4.0065,0.54134 -5.47627,-0.85609 l -9.17408,-8.72115 c -2.39692,-2.27863 -6.17372,-2.23122 -8.51252,0.10759 l -14.39447,14.39498 c -2.33842,2.33839 -2.38624,6.1146 -0.10759,8.51152 l 8.9951,9.46211 c 1.37305,1.44416 1.72558,3.57678 0.89681,5.38881 -0.72068,1.57466 -1.37293,3.18676 -1.95295,4.83335 -0.67271,1.90958 -2.45022,3.20823 -4.47444,3.25942 l -12.65164,0.32023 c -3.30621,0.0837 -5.9428,2.78759 -5.9428,6.09459 v 20.35786 c 0,3.30703 2.63659,6.01087 5.9428,6.09459 l 13.04677,0.33027 c 1.98843,0.0504 3.74578,1.30479 4.44123,3.16846 0.61094,1.63521 1.29301,3.2356 2.04295,4.79765 0.87797,1.82789 0.54145,4.00591 -0.85559,5.47527 l -8.72164,9.17457 c -1.37514,1.44653 -1.88755,3.39384 -1.57592,5.22595 h 0.7465 24.32055 61.26465 3.60327 c 3.95126,0 7.15424,-3.20297 7.15424,-7.15427 v -2.056 -8.04952 c -8.05257,9.71866 -20.25002,15.87899 -33.88271,15.76278 -23.61284,-0.2016 -42.90943,-19.4986 -43.11104,-43.11106 -0.20729,-24.31847 19.55063,-44.07731 43.86912,-43.8696 13.28515,0.11319 25.2041,6.27146 33.12463,15.84172 v -18.88347 -11.02398 l -3.97073,3.77469 c -1.44417,1.37264 -3.57578,1.72599 -5.38782,0.8968 -1.57506,-0.72024 -3.18776,-1.37292 -4.83435,-1.95295 -1.90958,-0.67309 -3.20729,-2.45063 -3.25892,-4.47441 l -0.32023,-12.65167 c -0.0833,-3.3058 -2.78767,-5.94277 -6.09509,-5.94277 z" fill="#E6E6E6" data-part="gear"></path></g></g></svg>
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module LibTmux
6
+ module Internal
7
+ # What the connected tmux daemon can do, in one table. Each gate in the
8
+ # library reads its release floor or affected releases from here, and
9
+ # the root capabilities.json manifest for the docs site is generated
10
+ # from the same records, so behavior and documentation cannot diverge.
11
+ # Quirks that need no caller decision stay out of the table. It sits
12
+ # beside TmuxVersion, which holds the supported minimum.
13
+ module Capabilities
14
+ SCHEMA = "libtmux-capabilities/1"
15
+ PORT = "ruby"
16
+ ERROR = "LibTmux::UnsupportedFeatureError"
17
+
18
+ # A tmux command and the single-letter flags a record depends on.
19
+ Command = Data.define(:command, :flags) do
20
+ def initialize(command:, flags: [])
21
+ super(command: command, flags: flags.freeze)
22
+ end
23
+
24
+ def to_h
25
+ flags.empty? ? {"command" => command} : {"command" => command, "flags" => flags}
26
+ end
27
+ end
28
+
29
+ # Something the library can do from tmux release +since+ on. Below it,
30
+ # or on an +except+ release, +handling+ says what the library does.
31
+ Capability = Data.define(:id, :name, :summary, :since, :handling, :error, :apis, :commands, :except) do
32
+ def initialize(id:, name:, summary:, since:, handling:, apis:, commands:, error: nil, except: [])
33
+ super(id: id, name: name, summary: summary, since: since, handling: handling, error: error,
34
+ apis: apis.freeze, commands: commands.freeze, except: except.freeze)
35
+ end
36
+
37
+ # True when +release+ (an Internal::TmuxVersion::Release) offers it.
38
+ def available?(release)
39
+ release >= Internal::TmuxVersion.parse(since) && !except.include?(release.text)
40
+ end
41
+
42
+ def to_h
43
+ {"id" => id, "name" => name, "summary" => summary, "since" => since,
44
+ "handling" => handling.to_s}.tap do |hash|
45
+ hash["error"] = error if error
46
+ hash["except"] = except unless except.empty?
47
+ hash["apis"] = apis
48
+ hash["commands"] = commands.map(&:to_h)
49
+ end
50
+ end
51
+ end
52
+
53
+ # A defect on exact tmux releases that the library refuses or repairs.
54
+ Workaround = Data.define(:id, :name, :summary, :releases, :handling, :apis, :commands) do
55
+ def initialize(id:, name:, summary:, releases:, handling:, apis:, commands:)
56
+ super(id: id, name: name, summary: summary, releases: releases.freeze, handling: handling,
57
+ apis: apis.freeze, commands: commands.freeze)
58
+ end
59
+
60
+ # True when +release+ is one of the affected releases.
61
+ def applies?(release)
62
+ releases.include?(release.text)
63
+ end
64
+
65
+ def to_h
66
+ {"id" => id, "name" => name, "summary" => summary, "releases" => releases,
67
+ "handling" => handling.to_s, "apis" => apis, "commands" => commands.map(&:to_h)}
68
+ end
69
+ end
70
+
71
+ CAPABILITIES = [
72
+ Capability.new(id: "control-mode", name: "Control mode", since: "3.7", handling: :refuse, error: ERROR,
73
+ summary: "Attach a library-managed control client and stream events.",
74
+ apis: ["LibTmux::Server#open_control", "LibTmux::ControlConnection.open"],
75
+ commands: [Command.new(command: "attach-session")]),
76
+ Capability.new(id: "buffer-delete-named", name: "Named buffer deletion", since: "3.4", handling: :refuse, error: ERROR,
77
+ summary: "Delete one paste buffer by name without risking another buffer.",
78
+ apis: ["LibTmux::Server#delete_buffer", "LibTmux::Server#run"],
79
+ commands: [Command.new(command: "delete-buffer", flags: ["-b"])]),
80
+ Capability.new(id: "buffer-rename", name: "Buffer rename", since: "3.4", handling: :refuse, error: ERROR,
81
+ summary: "Rename a paste buffer without risking another buffer.",
82
+ apis: ["LibTmux::Server#run"],
83
+ commands: [Command.new(command: "set-buffer", flags: ["-b", "-n"])]),
84
+ Capability.new(id: "layout-without-checksum", name: "Layouts without a checksum", since: "3.4",
85
+ handling: :refuse, error: ERROR,
86
+ summary: "Apply a layout string or a named layout that tmux validates before use.",
87
+ apis: ["LibTmux::Window#select_layout", "LibTmux::Server#run"],
88
+ commands: [Command.new(command: "select-layout")]),
89
+ Capability.new(id: "layout-mirrored-main", name: "Mirrored main layouts", since: "3.5",
90
+ handling: :refuse, error: ERROR,
91
+ summary: "Apply main-horizontal-mirrored and main-vertical-mirrored.",
92
+ apis: ["LibTmux::Window#select_layout"],
93
+ commands: [Command.new(command: "select-layout")]),
94
+ Capability.new(id: "layout-json", name: "JSON layout strings", since: "3.8-rc2",
95
+ handling: :refuse, error: ERROR,
96
+ summary: "Apply a layout saved in the JSON form tmux reports.",
97
+ apis: ["LibTmux::Window#select_layout"],
98
+ commands: [Command.new(command: "select-layout")]),
99
+ Capability.new(id: "clipboard-query", name: "Clipboard query", since: "3.6a", handling: :refuse, error: ERROR,
100
+ summary: "Ask an attached client's terminal for its clipboard with refresh-client -l.",
101
+ apis: ["LibTmux::Server#run", "LibTmux::ControlConnection#exchange"],
102
+ commands: [Command.new(command: "refresh-client", flags: ["-l"])])
103
+ ].freeze
104
+
105
+ WORKAROUNDS = [
106
+ Workaround.new(id: "break-pane-misnames-windows", name: "break-pane window naming", releases: ["3.7"],
107
+ handling: :repair,
108
+ summary: "break-pane gets the new window wrong on exactly tmux 3.7; Pane#break_out names it again, and raw break-pane is refused.",
109
+ apis: ["LibTmux::Pane#break_out", "LibTmux::Server#run", "LibTmux::ControlConnection#exchange"],
110
+ commands: [Command.new(command: "break-pane")]),
111
+ Workaround.new(id: "split-window-percent-size", name: "split-window -p", releases: ["3.4"],
112
+ handling: :refuse,
113
+ summary: "split-window -p is broken on tmux 3.4; use -l N%.",
114
+ apis: ["LibTmux::Server#run"],
115
+ commands: [Command.new(command: "split-window", flags: ["-p"])]),
116
+ Workaround.new(id: "name-separators-rejected", name: "Colon and dot in names", releases: ["3.7"],
117
+ handling: :refuse,
118
+ summary: "tmux 3.7 rejects ':' and '.' in session and window names; the library refuses them before dispatch, typed or raw.",
119
+ apis: ["LibTmux::Server#new_session", "LibTmux::Session#new_window", "LibTmux::Session#rename",
120
+ "LibTmux::Window#rename", "LibTmux::Server#run"],
121
+ commands: [Command.new(command: "new-session", flags: ["-s"]), Command.new(command: "new-window", flags: ["-n"]),
122
+ Command.new(command: "rename-session"), Command.new(command: "rename-window")])
123
+ ].freeze
124
+
125
+ module_function
126
+
127
+ # The record gating +id+; raises KeyError for an id the registry lacks.
128
+ def capability(id) = CAPABILITIES.find { |entry| entry.id == id } || raise(KeyError, "no capability #{id}")
129
+
130
+ def workaround(id) = WORKAROUNDS.find { |entry| entry.id == id } || raise(KeyError, "no workaround #{id}")
131
+
132
+ # The manifest the docs site reads. The floor is the supported minimum.
133
+ def manifest
134
+ {"schema" => SCHEMA, "port" => PORT, "floor" => {"minimum" => Internal::TmuxVersion::MINIMUM},
135
+ "capabilities" => CAPABILITIES.map(&:to_h), "workarounds" => WORKAROUNDS.map(&:to_h)}
136
+ end
137
+
138
+ # The exact text of capabilities.json: deterministic, with a final newline.
139
+ def manifest_json = "#{JSON.pretty_generate(manifest)}\n"
140
+ end
141
+ end
142
+ end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "snapshot"
4
4
  require_relative "metadata"
5
+ require_relative "tmux_version"
5
6
 
6
7
  module LibTmux
7
8
  module Internal
@@ -47,25 +48,32 @@ module LibTmux
47
48
  private
48
49
 
49
50
  def acquire_server_info(budget)
50
- rows = read(budget, :server, 1, ["display-message", "-p", framing(%w[version pid start_time])], 3)
51
- unless rows.length == 1
52
- raise ProtocolError.new("invalid capture server metadata", delivery: :observed, phase: :capture)
53
- end
54
- version, pid, start_time = rows.first
51
+ version, pid, start_time = read_identity(budget)
55
52
  version = version.dup.force_encoding(Encoding::UTF_8)
56
- match = /\A(?:next-)?([0-9]+)\.([0-9]+)([a-z]?)(?:-[A-Za-z0-9.-]+)?\z/.match(version) if version.valid_encoding?
57
- unless match
58
- raise UnsupportedFeatureError.new("unrecognized tmux version for capture", delivery: :observed, phase: :capture)
59
- end
60
- supported = ([match[1].to_i, match[2].to_i, match[3]] <=> [3, 2, "a"]) >= 0
61
- unless supported
62
- raise UnsupportedFeatureError.new("captured metadata requires tmux 3.2a or newer", delivery: :observed, phase: :capture)
53
+ begin
54
+ TmuxVersion.require_supported(version)
55
+ rescue UnsupportedFeatureError => error
56
+ raise UnsupportedFeatureError.new(error.message, delivery: :observed, phase: :capture)
63
57
  end
64
58
  unless pid.match?(/\A[1-9][0-9]{0,9}\z/n) && start_time.match?(/\A-?[0-9]{1,19}\z/n)
65
59
  raise ProtocolError.new("invalid capture server identity metadata", delivery: :observed, phase: :capture)
66
60
  end
67
61
  {version: version, pid: Integer(pid, 10), start_time: Integer(start_time, 10),
68
- capabilities: {metadata: :byte_counted, field_baseline: "3.2a"}}
62
+ capabilities: {metadata: :byte_counted, field_baseline: TmuxVersion::MINIMUM}}
63
+ end
64
+
65
+ # The identity travels in the probe's error text so that no user hook fires.
66
+ def read_identity(budget)
67
+ started_at = monotonic
68
+ remaining = check_budget(budget)
69
+ argv, marker = TmuxVersion.identity_argv(%w[version pid start_time])
70
+ result = @server.run(argv, timeout: remaining, cancel: budget.fetch(:cancel))
71
+ values = TmuxVersion.parse_identity(result, marker, 3)
72
+ budget[:bytes] += result.stderr.bytesize
73
+ budget[:rows] += 1
74
+ budget.fetch(:reads) << {source: :server, attempt: 1, started_at: started_at, finished_at: monotonic,
75
+ bytes: result.stderr.bytesize, rows: 1}
76
+ values
69
77
  end
70
78
 
71
79
  def acquire_rows(budget, attempt, clients)
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "tmux_version"
4
+
3
5
  module LibTmux
4
6
  module Internal
5
7
  # Names and bounds are explicit: wire spelling is not Ruby case conversion.
@@ -23,7 +25,7 @@ module LibTmux
23
25
  Field.new(id: id, name: name, wire_name: wire_name, format: format, type: type,
24
26
  nullable: nullable, empty_is_null: empty_is_null, min: min, max: max,
25
27
  operators: operators || {text: TEXT, integer: INTEGER, boolean: EQUALITY}.fetch(type),
26
- min_version: "3.2a", scope: scope, capture_requirements: [scope].freeze)
28
+ min_version: Internal::TmuxVersion::MINIMUM, scope: scope, capture_requirements: [scope].freeze)
27
29
  end
28
30
  private_class_method :field
29
31
 
@@ -40,7 +42,7 @@ module LibTmux
40
42
  end
41
43
  private_class_method :schema
42
44
 
43
- # 3.2a is the supported capture baseline, not a claim about first introduction.
45
+ # The minimum supported tmux is the capture baseline, not a claim about first introduction.
44
46
  SCHEMAS = {
45
47
  session: schema(:session, "session", [
46
48
  field(:session, "session.id", :id, "id", "session_id", :text, operators: EQUALITY),
@@ -70,7 +72,7 @@ module LibTmux
70
72
  field(:pane, "pane.id", :id, "id", "pane_id", :text, operators: EQUALITY),
71
73
  field(:pane, "pane.window_id", :window_id, "windowId", "window_id", :text, operators: EQUALITY),
72
74
  field(:pane, "pane.index", :index, "index", "pane_index", :integer, min: 0, max: UINT_MAX),
73
- field(:pane, "pane.pid", :pid, "pid", "pane_pid", :integer, min: 0, max: INT_MAX),
75
+ field(:pane, "pane.pid", :pid, "pid", "pane_pid", :integer, nullable: true, empty_is_null: true, min: 0, max: INT_MAX),
74
76
  field(:pane, "pane.current_command", :current_command, "currentCommand", "pane_current_command", :text),
75
77
  field(:pane, "pane.current_path", :current_path, "currentPath", "pane_current_path", :text, nullable: true, empty_is_null: true),
76
78
  field(:pane, "pane.title", :title, "title", "pane_title", :text),
@@ -2,6 +2,9 @@
2
2
 
3
3
  require "libtmux/endpoint"
4
4
  require "libtmux/child"
5
+ require "libtmux/process"
6
+ require "libtmux/tmux_version"
7
+ require "libtmux/native_guard"
5
8
 
6
9
  module LibTmux
7
10
  class GuardedBlock
@@ -386,6 +389,11 @@ module LibTmux
386
389
  begin
387
390
  process_wait = Internal::ProcessWait.new
388
391
  prefix = binding.command_prefix
392
+ release = binding.release || Internal::TmuxVersion.connected(prefix) do |probe|
393
+ Internal::ProcessExecutor.new.run(probe, timeout: 5.0)
394
+ end
395
+ Internal::TmuxVersion.require_control(release)
396
+ @release = release
389
397
  @pin = Internal::SocketIdentity.new(Endpoint.new(socket_path: prefix.last, executable: prefix.first))
390
398
  @wake_reader, @wake_writer = pipe
391
399
  input_reader, @input = pipe
@@ -435,6 +443,9 @@ module LibTmux
435
443
  end
436
444
  raise CapacityError.new("control command exceeds its byte limit", phase: :admission) if line.bytesize > @max_command
437
445
 
446
+ # A raw line reaches the daemon like Server#run's argv, so it gets the
447
+ # same literal-command refusals.
448
+ Internal::NativeGuard.check_line(line, @release)
438
449
  deadline, request, result, error = clock + timeout, nil, nil, nil
439
450
  begin
440
451
  Thread.handle_interrupt(Exception => :never) do
@@ -76,7 +76,7 @@ module LibTmux
76
76
  module Internal
77
77
  # Keep the socket inode alive and route through it; PID timestamps can repeat.
78
78
  class SocketIdentity
79
- attr_reader :key
79
+ attr_reader :key, :release
80
80
 
81
81
  def initialize(endpoint)
82
82
  @endpoint = endpoint
@@ -119,6 +119,11 @@ module LibTmux
119
119
  end
120
120
  end
121
121
 
122
+ # Records the daemon release that the server version check read through this route.
123
+ def record_release(release)
124
+ @release = release
125
+ end
126
+
122
127
  def command_prefix
123
128
  @mutex.synchronize do
124
129
  if @closed || Process.pid != @owner_pid
@@ -135,19 +135,28 @@ module LibTmux
135
135
  value = layout.is_a?(Symbol) ? layout.to_s.tr("_", "-") : layout
136
136
  budget = server.__send__(:operation_budget, timeout, cancel)
137
137
  # Older tmux can access an invalid pointer for a malformed layout header.
138
- unless LAYOUTS.include?(value) || /\A[0-9a-fA-F]{4},/.match?(value)
138
+ # tmux 3.8-rc2 and later report and accept saved layouts as JSON.
139
+ saved = /\A[0-9a-fA-F]{4},/.match?(value)
140
+ if !saved && value.start_with?("{")
141
+ json = Internal::Capabilities.capability("layout-json")
142
+ release = server.__send__(:tmux_release)
143
+ unless json.available?(release)
144
+ raise UnsupportedFeatureError.new("JSON layouts require tmux #{json.since} or newer " \
145
+ "(connected version #{release.text})", phase: :admission, delivery: :not_sent)
146
+ end
147
+ saved = true
148
+ end
149
+ unless LAYOUTS.include?(value) || saved
139
150
  choices = (LAYOUTS + MIRRORED_LAYOUTS).select { |name| name.start_with?(value) }
140
151
  raise ArgumentError, "layout must be a native name or a saved layout" if value.empty? || choices.empty?
141
152
 
142
153
  if choices.any? { |name| MIRRORED_LAYOUTS.include?(name) }
143
- version = server.display('#{version}', **budget.options).text
144
- release = /\A(\d+)\.(\d+)/.match(version)
145
- raise ProtocolError.new("tmux version is invalid", phase: :decode, delivery: :observed) unless release
146
-
147
- if ([release[1].to_i, release[2].to_i] <=> [3, 5]).negative?
154
+ release = server.__send__(:tmux_release)
155
+ mirrored = Internal::Capabilities.capability("layout-mirrored-main")
156
+ unless mirrored.available?(release)
148
157
  choices &= LAYOUTS
149
158
  if choices.empty?
150
- raise UnsupportedFeatureError.new("mirrored layouts require tmux 3.5+", phase: :admission)
159
+ raise UnsupportedFeatureError.new("mirrored layouts require tmux #{mirrored.since}+", phase: :admission)
151
160
  end
152
161
  end
153
162
  end
@@ -16,7 +16,7 @@ module LibTmux
16
16
  def unquote(value)
17
17
  escapes = {"a" => "\a", "b" => "\b", "t" => "\t", "n" => "\n", "v" => "\v",
18
18
  "f" => "\f", "r" => "\r", "s" => " ", "E" => "\e"}
19
- value.b.gsub(/\\([0-7]{3}|.)/n) do
19
+ value.b.gsub(/\\([0-7]{3}|.)/nm) do
20
20
  escaped = Regexp.last_match(1)
21
21
  if escaped.match?(/\A[0-7]{3}\z/)
22
22
  byte = escaped.to_i(8)