diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index d99ab355..582abef2 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -143,6 +143,10 @@ availability is separate. The full support matrix, benchmarks and implementation type coverage remain separate required gates; one local outer pass does not establish them. +The [option catalog generator](../scripts/option-catalog/README.md) reads a +pinned local tmux source release. Its `--check` compares the shipped option +and hook metadata with that source. + README commands are tested too. The unit suite lints the READMEs in `test/unit/readme_blocks_test.rb`: one command per console block, `gem install` names, flags against each executable's `--help`, linked MCP tool names, gem diff --git a/docs/reference/api.md b/docs/reference/api.md index 9557a663..0d879c0f 100644 --- a/docs/reference/api.md +++ b/docs/reference/api.md @@ -510,11 +510,11 @@ type checking. See [signature proof](behavior.md#signature-proof). | Method | Declared return | Contract | | --- | --- | --- | -| [#get](../../gems/libtmux/lib/libtmux/options.rb#L192) | `::LibTmux::OptionValue` | [Options and hooks](behavior.md#options-and-hooks) | -| [#list](../../gems/libtmux/lib/libtmux/options.rb#L65) | `::Array[::LibTmux::OptionValue]` | [Options and hooks](behavior.md#options-and-hooks) | -| [#run](../../gems/libtmux/lib/libtmux/options.rb#L202) | `::LibTmux::CommandResult` | [Options and hooks](behavior.md#options-and-hooks) | -| [#set](../../gems/libtmux/lib/libtmux/options.rb#L196) | `::LibTmux::CommandResult` | [Options and hooks](behavior.md#options-and-hooks) | -| [#unset](../../gems/libtmux/lib/libtmux/options.rb#L131) | `::LibTmux::CommandResult` | [Options and hooks](behavior.md#options-and-hooks) | +| [#get](../../gems/libtmux/lib/libtmux/options.rb#L194) | `::LibTmux::OptionValue` | [Options and hooks](behavior.md#options-and-hooks) | +| [#list](../../gems/libtmux/lib/libtmux/options.rb#L66) | `::Array[::LibTmux::OptionValue]` | [Options and hooks](behavior.md#options-and-hooks) | +| [#run](../../gems/libtmux/lib/libtmux/options.rb#L204) | `::LibTmux::CommandResult` | [Options and hooks](behavior.md#options-and-hooks) | +| [#set](../../gems/libtmux/lib/libtmux/options.rb#L198) | `::LibTmux::CommandResult` | [Options and hooks](behavior.md#options-and-hooks) | +| [#unset](../../gems/libtmux/lib/libtmux/options.rb#L132) | `::LibTmux::CommandResult` | [Options and hooks](behavior.md#options-and-hooks) | ## LibTmux::IncompleteSnapshotError @@ -618,6 +618,32 @@ type checking. See [signature proof](behavior.md#signature-proof). | [#text](../../gems/libtmux/lib/libtmux/errors.rb#L5) | `::String?` | [Errors](behavior.md#errors) | | [.new](../../gems/libtmux/lib/libtmux/errors.rb#L7) | `::LibTmux::NoMatchError` | [Errors](behavior.md#errors) | +## LibTmux::OptionCatalog + +| Method | Declared return | Contract | +| --- | --- | --- | +| [.hooks](../../gems/libtmux/lib/libtmux/option_catalog.rb#L52) | `::Hash[::String, ::LibTmux::OptionCatalog::Hook]` | [Option catalog](behavior.md#option-catalog) | +| [.options](../../gems/libtmux/lib/libtmux/option_catalog.rb#L49) | `::Hash[::String, ::LibTmux::OptionCatalog::Option]` | [Option catalog](behavior.md#option-catalog) | +| [.tmux_version](../../gems/libtmux/lib/libtmux/option_catalog.rb#L46) | `::String` | [Option catalog](behavior.md#option-catalog) | + +## LibTmux::OptionCatalog::Hook + +| Method | Declared return | Contract | +| --- | --- | --- | +| [#name](../../gems/libtmux/lib/libtmux/option_catalog.rb#L29) | `::String` | [Option catalog](behavior.md#option-catalog) | +| [#scopes](../../gems/libtmux/lib/libtmux/option_catalog.rb#L29) | `::Array[::LibTmux::OptionCatalog::scope]` | [Option catalog](behavior.md#option-catalog) | + +## LibTmux::OptionCatalog::Option + +| Method | Declared return | Contract | +| --- | --- | --- | +| [#array?](../../gems/libtmux/lib/libtmux/option_catalog.rb#L24) | `bool` | [Option catalog](behavior.md#option-catalog) | +| [#choices](../../gems/libtmux/lib/libtmux/option_catalog.rb#L10) | `::Array[::String]` | [Option catalog](behavior.md#option-catalog) | +| [#name](../../gems/libtmux/lib/libtmux/option_catalog.rb#L10) | `::String` | [Option catalog](behavior.md#option-catalog) | +| [#range](../../gems/libtmux/lib/libtmux/option_catalog.rb#L10) | `::Range[::Integer]?` | [Option catalog](behavior.md#option-catalog) | +| [#scopes](../../gems/libtmux/lib/libtmux/option_catalog.rb#L10) | `::Array[::LibTmux::OptionCatalog::scope]` | [Option catalog](behavior.md#option-catalog) | +| [#type](../../gems/libtmux/lib/libtmux/option_catalog.rb#L10) | `::LibTmux::OptionCatalog::option_type` | [Option catalog](behavior.md#option-catalog) | + ## LibTmux::OptionValue | Method | Declared return | Contract | @@ -634,10 +660,10 @@ type checking. See [signature proof](behavior.md#signature-proof). | Method | Declared return | Contract | | --- | --- | --- | -| [#get](../../gems/libtmux/lib/libtmux/options.rb#L102) | `::LibTmux::OptionValue` | [Options and hooks](behavior.md#options-and-hooks) | -| [#list](../../gems/libtmux/lib/libtmux/options.rb#L65) | `::Array[::LibTmux::OptionValue]` | [Options and hooks](behavior.md#options-and-hooks) | -| [#set](../../gems/libtmux/lib/libtmux/options.rb#L120) | `::LibTmux::CommandResult` | [Options and hooks](behavior.md#options-and-hooks) | -| [#unset](../../gems/libtmux/lib/libtmux/options.rb#L131) | `::LibTmux::CommandResult` | [Options and hooks](behavior.md#options-and-hooks) | +| [#get](../../gems/libtmux/lib/libtmux/options.rb#L103) | `::LibTmux::OptionValue` | [Options and hooks](behavior.md#options-and-hooks) | +| [#list](../../gems/libtmux/lib/libtmux/options.rb#L66) | `::Array[::LibTmux::OptionValue]` | [Options and hooks](behavior.md#options-and-hooks) | +| [#set](../../gems/libtmux/lib/libtmux/options.rb#L121) | `::LibTmux::CommandResult` | [Options and hooks](behavior.md#options-and-hooks) | +| [#unset](../../gems/libtmux/lib/libtmux/options.rb#L132) | `::LibTmux::CommandResult` | [Options and hooks](behavior.md#options-and-hooks) | ## LibTmux::OutcomeUnknown diff --git a/docs/reference/behavior.md b/docs/reference/behavior.md index afcca543..f9e7d173 100644 --- a/docs/reference/behavior.md +++ b/docs/reference/behavior.md @@ -631,6 +631,37 @@ remains present. Append follows tmux's lowest-free-index rule, and indexed hooks execute in index order. Empty arrays remain distinct from empty String values. +## Option catalog + +`LibTmux::OptionCatalog.options` and `.hooks` return frozen hashes keyed by +canonical tmux name. Each hash is sorted by name. Look up a name with `[]` +to get its definition or `nil`. The catalog describes built-in definitions +from `OptionCatalog.tmux_version`, currently tmux 3.8, and works without a daemon. +Use `Options` and `Hooks` to read or change values on a connected daemon, +which checks names and values against its own release. + +An option's `type` corresponds to its tmux table type. `scopes` lists every +scope declared in the table. `range` gives inclusive numeric limits or `nil`. +`choices` gives the choice values in tmux order and is empty for other types. +`array?` reports the upstream array flag. A hook gives its name and scopes. + + +```ruby +require "libtmux" + +catalog = LibTmux::OptionCatalog +history = catalog.options.fetch("history-limit") +position = catalog.options.fetch("status-position") +created = catalog.hooks.fetch("pane-created") + +puts "tmux source: #{catalog.tmux_version}" +puts "history-limit: #{history.type}, #{history.scopes.join(', ')}, " \ + "#{history.range}" +puts "status-position: #{position.choices.join(', ')}" +puts "pane-created: #{created.scopes.join(', ')}" +``` + + ## Option values Option-value accessors are local. `raw` preserves bytes, and `as` explicitly diff --git a/docs/reference/contracts.json b/docs/reference/contracts.json index 15e508fd..2a9d0c48 100644 --- a/docs/reference/contracts.json +++ b/docs/reference/contracts.json @@ -537,6 +537,23 @@ "LibTmux::MCP::StdioTransport.new" ] }, + { + "id": "option-catalog", + "title": "Option catalog", + "methods": [ + "LibTmux::OptionCatalog.hooks", + "LibTmux::OptionCatalog.options", + "LibTmux::OptionCatalog.tmux_version", + "LibTmux::OptionCatalog::Hook#name", + "LibTmux::OptionCatalog::Hook#scopes", + "LibTmux::OptionCatalog::Option#array?", + "LibTmux::OptionCatalog::Option#choices", + "LibTmux::OptionCatalog::Option#name", + "LibTmux::OptionCatalog::Option#range", + "LibTmux::OptionCatalog::Option#scopes", + "LibTmux::OptionCatalog::Option#type" + ] + }, { "id": "option-values", "title": "Option values", diff --git a/examples/api/option_catalog.rb b/examples/api/option_catalog.rb new file mode 100644 index 00000000..83362144 --- /dev/null +++ b/examples/api/option_catalog.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +# docs:begin main +require "libtmux" + +catalog = LibTmux::OptionCatalog +history = catalog.options.fetch("history-limit") +position = catalog.options.fetch("status-position") +created = catalog.hooks.fetch("pane-created") + +puts "tmux source: #{catalog.tmux_version}" +puts "history-limit: #{history.type}, #{history.scopes.join(', ')}, " \ + "#{history.range}" +puts "status-position: #{position.choices.join(', ')}" +puts "pane-created: #{created.scopes.join(', ')}" +# docs:end main diff --git a/examples/manifest.json b/examples/manifest.json index 5abaccda..2de6d58c 100644 --- a/examples/manifest.json +++ b/examples/manifest.json @@ -257,6 +257,20 @@ "output": "Reply from the fixture\n" } }, + { + "id": "api_option_catalog", + "path": "examples/api/option_catalog.rb", + "gem": "libtmux", + "api": { + "symbols": [ + "LibTmux::OptionCatalog.tmux_version", + "LibTmux::OptionCatalog.options", + "LibTmux::OptionCatalog.hooks" + ], + "description": "Look up an option's type, scope and allowed values and a hook's scopes using the catalog shipped with the gem.", + "output": "tmux source: 3.8\nhistory-limit: number, session, 0..2147483647\nstatus-position: top, bottom\npane-created: window, pane\n" + } + }, { "id": "api_cancellation", "path": "examples/api/cancellation.rb", @@ -618,6 +632,12 @@ "region": "main", "language": "ruby" }, + { + "id": "api_option_catalog/main", + "source": "examples/api/option_catalog.rb", + "region": "main", + "language": "ruby" + }, { "id": "api_cancellation/main", "source": "examples/api/cancellation.rb", diff --git a/gems/libtmux/CHANGELOG.md b/gems/libtmux/CHANGELOG.md index 65c680c7..bc1ddbb7 100644 --- a/gems/libtmux/CHANGELOG.md +++ b/gems/libtmux/CHANGELOG.md @@ -18,6 +18,8 @@ in the [main changelog](../../CHANGELOG.md). (#181) - Option reads preserve literal asterisks and spaces in user option names while reporting whether the value is inherited. (#182) +- OptionCatalog exposes the built-in option and hook definitions from tmux + 3.8, including option types, ranges, choices and scopes. (#191) - `Server#write_buffer` raises `UnsupportedFeatureError` for an empty payload before sending it to tmux. Nonempty writes keep their existing behavior. (#172) diff --git a/gems/libtmux/README.md b/gems/libtmux/README.md index 022d220b..70b2e3e8 100644 --- a/gems/libtmux/README.md +++ b/gems/libtmux/README.md @@ -649,6 +649,8 @@ hook: display-message created The contracts are in [Options and hooks][behavior-options] and [Option values][behavior-option-values]. +Look up built-in types, choices and hook scopes in the +[option catalog][behavior-option-catalog]. ## Watch tmux change @@ -953,6 +955,7 @@ MIT. See [LICENSE](LICENSE). [behavior-live-handles]: ../../docs/reference/behavior.md#live-handles [behavior-option-values]: ../../docs/reference/behavior.md#option-values [behavior-options]: ../../docs/reference/behavior.md#options-and-hooks +[behavior-option-catalog]: ../../docs/reference/behavior.md#option-catalog [behavior-records]: ../../docs/reference/behavior.md#captured-records [behavior-signature]: ../../docs/reference/behavior.md#signature-proof [behavior-window-links]: ../../docs/reference/behavior.md#window-links diff --git a/gems/libtmux/lib/libtmux.rb b/gems/libtmux/lib/libtmux.rb index 8f287f99..6e1569e4 100644 --- a/gems/libtmux/lib/libtmux.rb +++ b/gems/libtmux/lib/libtmux.rb @@ -19,3 +19,7 @@ require "libtmux/terminal" require "libtmux/text_wait" require "libtmux/program" + +module LibTmux + autoload :OptionCatalog, "libtmux/option_catalog" +end diff --git a/gems/libtmux/lib/libtmux/option_catalog.rb b/gems/libtmux/lib/libtmux/option_catalog.rb new file mode 100644 index 00000000..5abfdce5 --- /dev/null +++ b/gems/libtmux/lib/libtmux/option_catalog.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require "json" + +module LibTmux + # Built-in option and hook definitions from the pinned tmux source release. + module OptionCatalog + # Metadata for a built-in tmux option. + class Option + attr_reader :name, :type, :scopes, :range, :choices + + def initialize(name, definition) + @name = name + @type = definition.fetch("type").to_sym + @scopes = definition.fetch("scopes").map(&:to_sym).freeze + @range = Range.new(*definition.fetch("range")) if definition.key?("range") + @choices = definition.fetch("choices", [].freeze) + @array = definition.fetch("array", false) + freeze + end + private_class_method :new + + # Whether tmux defines the option as an array. + def array? = @array + end + + # A built-in hook and the scopes declared by tmux. + class Hook + attr_reader :name, :scopes + + def initialize(name, scopes) + @name = name + @scopes = scopes.map(&:to_sym).freeze + freeze + end + private_class_method :new + end + + data = JSON.parse(File.binread(File.expand_path("../../schema/option-catalog.json", __dir__)), freeze: true) + TMUX_VERSION = data.fetch("tmux_version") + OPTIONS = data.fetch("options").to_h { |name, definition| [name, Option.__send__(:new, name, definition)] }.freeze + HOOKS = data.fetch("hooks").to_h { |name, scopes| [name, Hook.__send__(:new, name, scopes)] }.freeze + private_constant :TMUX_VERSION, :OPTIONS, :HOOKS + + # The source release for both catalogs, independent of any connected daemon. + def self.tmux_version = TMUX_VERSION + + # A frozen hash keyed by canonical option name, sorted by name. + def self.options = OPTIONS + + # A frozen hash keyed by canonical hook name, sorted by name. + def self.hooks = HOOKS + end +end diff --git a/gems/libtmux/lib/libtmux/options.rb b/gems/libtmux/lib/libtmux/options.rb index 7265f11e..300f09dd 100644 --- a/gems/libtmux/lib/libtmux/options.rb +++ b/gems/libtmux/lib/libtmux/options.rb @@ -46,7 +46,8 @@ def as(type) end end - # Explicit acquisition and mutation at one option scope. + # Explicit acquisition and mutation at one option scope. For built-in + # option types, scopes and allowed values, see {OptionCatalog.options}. class Options # tmux prints the user option name as-is and its value as one args_escape token. USER_OPTION_RECORD = / @@ -187,7 +188,8 @@ def decode_escaped(value) end end - # Hook values are executable tmux command strings, never literal data. + # Hook values are executable tmux command strings. See {OptionCatalog.hooks} + # for the built-in names and their scopes. class Hooks < Options def get(name, index: nil, inherited: false, timeout: 5.0, cancel: nil) super diff --git a/gems/libtmux/libtmux.gemspec b/gems/libtmux/libtmux.gemspec index 8edab0c4..af46e349 100644 --- a/gems/libtmux/libtmux.gemspec +++ b/gems/libtmux/libtmux.gemspec @@ -35,6 +35,7 @@ Gem::Specification.new do |spec| lib/libtmux/server.rb lib/libtmux/operations.rb lib/libtmux/options.rb + lib/libtmux/option_catalog.rb lib/libtmux/catalog.rb lib/libtmux/snapshot.rb lib/libtmux/capture.rb @@ -53,6 +54,7 @@ Gem::Specification.new do |spec| sig/libtmux.rbs sig/criteria.rbs sig/operations.rbs + sig/option_catalog.rbs sig/snapshot.rbs sig/control.rbs sig/group.rbs @@ -61,6 +63,7 @@ Gem::Specification.new do |spec| sig/program.rbs sig/fields.rbs schema/where-v1.json + schema/option-catalog.json ] spec.add_dependency "json", ">= 3.0.2", "< 4" spec.add_dependency "tmpdir", "~> 0.3" diff --git a/gems/libtmux/schema/option-catalog.json b/gems/libtmux/schema/option-catalog.json new file mode 100644 index 00000000..65729f25 --- /dev/null +++ b/gems/libtmux/schema/option-catalog.json @@ -0,0 +1,1723 @@ +{ + "tmux_version": "3.8", + "source_revision": "7f2a35ad3321f9ba57a1062ca73b1f3ff26aca53", + "source_sha256": { + "options-table.c": "cbf3b034785b30aeee3d67032f99e9d9b173a321e11b19be1a11c9e9914fc866", + "tmux.h": "88f17595490be65d26c060ba4e39548d6fdd552847ef95889c9b7fa8767c1e7a" + }, + "options": { + "activity-action": { + "type": "choice", + "scopes": [ + "session" + ], + "choices": [ + "none", + "any", + "current", + "other" + ] + }, + "aggressive-resize": { + "type": "flag", + "scopes": [ + "window" + ] + }, + "allow-passthrough": { + "type": "choice", + "scopes": [ + "window", + "pane" + ], + "choices": [ + "off", + "on", + "all" + ] + }, + "allow-rename": { + "type": "flag", + "scopes": [ + "window", + "pane" + ] + }, + "allow-set-title": { + "type": "flag", + "scopes": [ + "window", + "pane" + ] + }, + "alternate-screen": { + "type": "flag", + "scopes": [ + "window", + "pane" + ] + }, + "assume-paste-time": { + "type": "number", + "scopes": [ + "session" + ], + "range": [ + 0, + 2147483647 + ] + }, + "automatic-rename": { + "type": "flag", + "scopes": [ + "window" + ] + }, + "automatic-rename-format": { + "type": "string", + "scopes": [ + "window" + ] + }, + "backspace": { + "type": "key", + "scopes": [ + "server" + ] + }, + "base-index": { + "type": "number", + "scopes": [ + "session" + ], + "range": [ + 0, + 2147483647 + ] + }, + "bell-action": { + "type": "choice", + "scopes": [ + "session" + ], + "choices": [ + "none", + "any", + "current", + "other" + ] + }, + "buffer-limit": { + "type": "number", + "scopes": [ + "server" + ], + "range": [ + 1, + 2147483647 + ] + }, + "clear-on-attach": { + "type": "flag", + "scopes": [ + "server" + ] + }, + "clock-mode-colour": { + "type": "string", + "scopes": [ + "window" + ] + }, + "clock-mode-style": { + "type": "choice", + "scopes": [ + "window" + ], + "choices": [ + "12", + "24", + "12-with-seconds", + "24-with-seconds" + ] + }, + "codepoint-widths": { + "type": "string", + "scopes": [ + "server" + ], + "array": true + }, + "command-alias": { + "type": "string", + "scopes": [ + "server" + ], + "array": true + }, + "copy-command": { + "type": "string", + "scopes": [ + "server" + ] + }, + "copy-mode-current-line-number-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "copy-mode-current-line-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "copy-mode-current-match-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "copy-mode-line-number-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "copy-mode-line-numbers": { + "type": "choice", + "scopes": [ + "window" + ], + "choices": [ + "off", + "default", + "absolute", + "relative", + "hybrid" + ] + }, + "copy-mode-mark-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "copy-mode-match-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "copy-mode-position-format": { + "type": "string", + "scopes": [ + "window", + "pane" + ] + }, + "copy-mode-position-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "copy-mode-selection-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "cursor-colour": { + "type": "string", + "scopes": [ + "window", + "pane" + ] + }, + "cursor-style": { + "type": "choice", + "scopes": [ + "window", + "pane" + ], + "choices": [ + "default", + "blinking-block", + "block", + "blinking-underline", + "underline", + "blinking-bar", + "bar" + ] + }, + "dark-theme-black": { + "type": "string", + "scopes": [ + "server" + ] + }, + "dark-theme-blue": { + "type": "string", + "scopes": [ + "server" + ] + }, + "dark-theme-cyan": { + "type": "string", + "scopes": [ + "server" + ] + }, + "dark-theme-dark-grey": { + "type": "string", + "scopes": [ + "server" + ] + }, + "dark-theme-green": { + "type": "string", + "scopes": [ + "server" + ] + }, + "dark-theme-light-grey": { + "type": "string", + "scopes": [ + "server" + ] + }, + "dark-theme-magenta": { + "type": "string", + "scopes": [ + "server" + ] + }, + "dark-theme-red": { + "type": "string", + "scopes": [ + "server" + ] + }, + "dark-theme-white": { + "type": "string", + "scopes": [ + "server" + ] + }, + "dark-theme-yellow": { + "type": "string", + "scopes": [ + "server" + ] + }, + "default-client-command": { + "type": "command", + "scopes": [ + "server" + ] + }, + "default-command": { + "type": "string", + "scopes": [ + "session" + ] + }, + "default-shell": { + "type": "string", + "scopes": [ + "session" + ] + }, + "default-size": { + "type": "string", + "scopes": [ + "session" + ] + }, + "default-terminal": { + "type": "string", + "scopes": [ + "server" + ] + }, + "destroy-unattached": { + "type": "choice", + "scopes": [ + "session" + ], + "choices": [ + "off", + "on", + "keep-last", + "keep-group" + ] + }, + "detach-on-destroy": { + "type": "choice", + "scopes": [ + "session" + ], + "choices": [ + "off", + "on", + "no-detached", + "previous", + "next" + ] + }, + "display-panes-active-colour": { + "type": "string", + "scopes": [ + "window" + ] + }, + "display-panes-border-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "display-panes-colour": { + "type": "string", + "scopes": [ + "window" + ] + }, + "display-panes-format": { + "type": "string", + "scopes": [ + "window" + ] + }, + "display-panes-time": { + "type": "number", + "scopes": [ + "window" + ], + "range": [ + 1, + 2147483647 + ] + }, + "display-time": { + "type": "number", + "scopes": [ + "session" + ], + "range": [ + 0, + 2147483647 + ] + }, + "editor": { + "type": "string", + "scopes": [ + "server" + ] + }, + "escape-time": { + "type": "number", + "scopes": [ + "server" + ], + "range": [ + 0, + 2147483647 + ] + }, + "exit-empty": { + "type": "flag", + "scopes": [ + "server" + ] + }, + "exit-unattached": { + "type": "flag", + "scopes": [ + "server" + ] + }, + "extended-keys": { + "type": "choice", + "scopes": [ + "server" + ], + "choices": [ + "off", + "on", + "always" + ] + }, + "extended-keys-format": { + "type": "choice", + "scopes": [ + "server" + ], + "choices": [ + "csi-u", + "xterm" + ] + }, + "fill-character": { + "type": "string", + "scopes": [ + "window" + ] + }, + "focus-events": { + "type": "flag", + "scopes": [ + "server" + ] + }, + "focus-follows-mouse": { + "type": "flag", + "scopes": [ + "session" + ] + }, + "get-clipboard": { + "type": "choice", + "scopes": [ + "server" + ], + "choices": [ + "off", + "buffer", + "request", + "both" + ] + }, + "history-file": { + "type": "string", + "scopes": [ + "server" + ] + }, + "history-limit": { + "type": "number", + "scopes": [ + "session" + ], + "range": [ + 0, + 2147483647 + ] + }, + "initial-repeat-time": { + "type": "number", + "scopes": [ + "session" + ], + "range": [ + 0, + 2000000 + ] + }, + "input-buffer-size": { + "type": "number", + "scopes": [ + "server" + ], + "range": [ + 1048576, + 4294967295 + ] + }, + "key-table": { + "type": "string", + "scopes": [ + "session" + ] + }, + "light-theme-black": { + "type": "string", + "scopes": [ + "server" + ] + }, + "light-theme-blue": { + "type": "string", + "scopes": [ + "server" + ] + }, + "light-theme-cyan": { + "type": "string", + "scopes": [ + "server" + ] + }, + "light-theme-dark-grey": { + "type": "string", + "scopes": [ + "server" + ] + }, + "light-theme-green": { + "type": "string", + "scopes": [ + "server" + ] + }, + "light-theme-light-grey": { + "type": "string", + "scopes": [ + "server" + ] + }, + "light-theme-magenta": { + "type": "string", + "scopes": [ + "server" + ] + }, + "light-theme-red": { + "type": "string", + "scopes": [ + "server" + ] + }, + "light-theme-white": { + "type": "string", + "scopes": [ + "server" + ] + }, + "light-theme-yellow": { + "type": "string", + "scopes": [ + "server" + ] + }, + "lock-after-time": { + "type": "number", + "scopes": [ + "session" + ], + "range": [ + 0, + 2147483647 + ] + }, + "lock-command": { + "type": "string", + "scopes": [ + "session" + ] + }, + "main-pane-height": { + "type": "string", + "scopes": [ + "window" + ] + }, + "main-pane-width": { + "type": "string", + "scopes": [ + "window" + ] + }, + "menu-border-lines": { + "type": "choice", + "scopes": [ + "window" + ], + "choices": [ + "single", + "double", + "heavy", + "simple", + "rounded", + "padded", + "none" + ] + }, + "menu-border-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "menu-selected-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "menu-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "message-command-style": { + "type": "string", + "scopes": [ + "session" + ] + }, + "message-format": { + "type": "string", + "scopes": [ + "session" + ] + }, + "message-limit": { + "type": "number", + "scopes": [ + "server" + ], + "range": [ + 0, + 2147483647 + ] + }, + "message-line": { + "type": "choice", + "scopes": [ + "session" + ], + "choices": [ + "0", + "1", + "2", + "3", + "4" + ] + }, + "message-style": { + "type": "string", + "scopes": [ + "session" + ] + }, + "mode-keys": { + "type": "choice", + "scopes": [ + "window" + ], + "choices": [ + "emacs", + "vi" + ] + }, + "mode-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "monitor-activity": { + "type": "flag", + "scopes": [ + "window" + ] + }, + "monitor-bell": { + "type": "flag", + "scopes": [ + "window" + ] + }, + "monitor-silence": { + "type": "number", + "scopes": [ + "window" + ], + "range": [ + 0, + 2147483647 + ] + }, + "mouse": { + "type": "flag", + "scopes": [ + "session" + ] + }, + "other-pane-height": { + "type": "string", + "scopes": [ + "window" + ] + }, + "other-pane-width": { + "type": "string", + "scopes": [ + "window" + ] + }, + "pane-active-border-style": { + "type": "string", + "scopes": [ + "window", + "pane" + ] + }, + "pane-base-index": { + "type": "number", + "scopes": [ + "window" + ], + "range": [ + 0, + 65535 + ] + }, + "pane-border-format": { + "type": "string", + "scopes": [ + "window", + "pane" + ] + }, + "pane-border-indicators": { + "type": "choice", + "scopes": [ + "window" + ], + "choices": [ + "off", + "colour", + "arrows", + "both" + ] + }, + "pane-border-lines": { + "type": "choice", + "scopes": [ + "window", + "pane" + ], + "choices": [ + "single", + "double", + "heavy", + "simple", + "number", + "spaces", + "none" + ] + }, + "pane-border-status": { + "type": "choice", + "scopes": [ + "window", + "pane" + ], + "choices": [ + "off", + "top", + "bottom", + "top-floating", + "bottom-floating" + ] + }, + "pane-border-style": { + "type": "string", + "scopes": [ + "window", + "pane" + ] + }, + "pane-colours": { + "type": "colour", + "scopes": [ + "window", + "pane" + ], + "array": true + }, + "pane-scrollbars": { + "type": "choice", + "scopes": [ + "window" + ], + "choices": [ + "off", + "modal", + "on", + "auto-hide" + ] + }, + "pane-scrollbars-position": { + "type": "choice", + "scopes": [ + "window" + ], + "choices": [ + "right", + "left" + ] + }, + "pane-scrollbars-style": { + "type": "string", + "scopes": [ + "window", + "pane" + ] + }, + "pane-scrollbars-timeout": { + "type": "number", + "scopes": [ + "window" + ], + "range": [ + 0, + 2147483647 + ] + }, + "pane-status-current-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "pane-status-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "popup-border-lines": { + "type": "choice", + "scopes": [ + "window" + ], + "choices": [ + "single", + "double", + "heavy", + "simple", + "rounded", + "padded", + "none" + ] + }, + "popup-border-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "popup-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "prefix": { + "type": "key", + "scopes": [ + "session" + ] + }, + "prefix-timeout": { + "type": "number", + "scopes": [ + "server" + ], + "range": [ + 0, + 2147483647 + ] + }, + "prefix2": { + "type": "key", + "scopes": [ + "session" + ] + }, + "prompt-command-cursor-colour": { + "type": "string", + "scopes": [ + "session" + ] + }, + "prompt-command-cursor-style": { + "type": "choice", + "scopes": [ + "session" + ], + "choices": [ + "default", + "blinking-block", + "block", + "blinking-underline", + "underline", + "blinking-bar", + "bar" + ] + }, + "prompt-cursor-colour": { + "type": "string", + "scopes": [ + "session" + ] + }, + "prompt-cursor-style": { + "type": "choice", + "scopes": [ + "session" + ], + "choices": [ + "default", + "blinking-block", + "block", + "blinking-underline", + "underline", + "blinking-bar", + "bar" + ] + }, + "prompt-history-limit": { + "type": "number", + "scopes": [ + "server" + ], + "range": [ + 0, + 2147483647 + ] + }, + "remain-on-exit": { + "type": "choice", + "scopes": [ + "window", + "pane" + ], + "choices": [ + "off", + "on", + "failed", + "key", + "failed-key" + ] + }, + "remain-on-exit-format": { + "type": "string", + "scopes": [ + "window", + "pane" + ] + }, + "renumber-windows": { + "type": "flag", + "scopes": [ + "session" + ] + }, + "repeat-time": { + "type": "number", + "scopes": [ + "session" + ], + "range": [ + 0, + 2000000 + ] + }, + "scroll-on-clear": { + "type": "flag", + "scopes": [ + "window", + "pane" + ] + }, + "session-status-current-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "session-status-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "set-clipboard": { + "type": "choice", + "scopes": [ + "server" + ], + "choices": [ + "off", + "external", + "on" + ] + }, + "set-titles": { + "type": "flag", + "scopes": [ + "session" + ] + }, + "set-titles-string": { + "type": "string", + "scopes": [ + "session" + ] + }, + "silence-action": { + "type": "choice", + "scopes": [ + "session" + ], + "choices": [ + "none", + "any", + "current", + "other" + ] + }, + "status": { + "type": "choice", + "scopes": [ + "session" + ], + "choices": [ + "off", + "on", + "2", + "3", + "4", + "5" + ] + }, + "status-bg": { + "type": "colour", + "scopes": [ + "session" + ] + }, + "status-fg": { + "type": "colour", + "scopes": [ + "session" + ] + }, + "status-format": { + "type": "string", + "scopes": [ + "session" + ], + "array": true + }, + "status-interval": { + "type": "number", + "scopes": [ + "session" + ], + "range": [ + 0, + 2147483647 + ] + }, + "status-justify": { + "type": "choice", + "scopes": [ + "session" + ], + "choices": [ + "left", + "centre", + "right", + "absolute-centre" + ] + }, + "status-keys": { + "type": "choice", + "scopes": [ + "session" + ], + "choices": [ + "emacs", + "vi" + ] + }, + "status-left": { + "type": "string", + "scopes": [ + "session" + ] + }, + "status-left-length": { + "type": "number", + "scopes": [ + "session" + ], + "range": [ + 0, + 32767 + ] + }, + "status-left-style": { + "type": "string", + "scopes": [ + "session" + ] + }, + "status-position": { + "type": "choice", + "scopes": [ + "session" + ], + "choices": [ + "top", + "bottom" + ] + }, + "status-right": { + "type": "string", + "scopes": [ + "session" + ] + }, + "status-right-length": { + "type": "number", + "scopes": [ + "session" + ], + "range": [ + 0, + 32767 + ] + }, + "status-right-style": { + "type": "string", + "scopes": [ + "session" + ] + }, + "status-style": { + "type": "string", + "scopes": [ + "session" + ] + }, + "switch-mode-match-style": { + "type": "string", + "scopes": [ + "window", + "pane" + ] + }, + "synchronize-panes": { + "type": "flag", + "scopes": [ + "window", + "pane" + ] + }, + "terminal-features": { + "type": "string", + "scopes": [ + "server" + ], + "array": true + }, + "terminal-overrides": { + "type": "string", + "scopes": [ + "server" + ], + "array": true + }, + "theme": { + "type": "choice", + "scopes": [ + "server" + ], + "choices": [ + "detect", + "terminal", + "light", + "dark" + ] + }, + "tiled-layout-max-columns": { + "type": "number", + "scopes": [ + "window" + ], + "range": [ + 0, + 65535 + ] + }, + "tree-mode-border-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "tree-mode-preview-format": { + "type": "string", + "scopes": [ + "window", + "pane" + ] + }, + "tree-mode-preview-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "tree-mode-selection-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "update-environment": { + "type": "string", + "scopes": [ + "session" + ], + "array": true + }, + "user-keys": { + "type": "string", + "scopes": [ + "server" + ], + "array": true + }, + "variation-selector-always-wide": { + "type": "flag", + "scopes": [ + "server" + ] + }, + "visual-activity": { + "type": "choice", + "scopes": [ + "session" + ], + "choices": [ + "off", + "on", + "both" + ] + }, + "visual-bell": { + "type": "choice", + "scopes": [ + "session" + ], + "choices": [ + "off", + "on", + "both" + ] + }, + "visual-silence": { + "type": "choice", + "scopes": [ + "session" + ], + "choices": [ + "off", + "on", + "both" + ] + }, + "window-active-style": { + "type": "string", + "scopes": [ + "window", + "pane" + ] + }, + "window-pane-current-status-format": { + "type": "string", + "scopes": [ + "window" + ] + }, + "window-pane-status-format": { + "type": "string", + "scopes": [ + "window" + ] + }, + "window-size": { + "type": "choice", + "scopes": [ + "window" + ], + "choices": [ + "largest", + "smallest", + "manual", + "latest" + ] + }, + "window-status-activity-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "window-status-bell-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "window-status-current-format": { + "type": "string", + "scopes": [ + "window" + ] + }, + "window-status-current-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "window-status-format": { + "type": "string", + "scopes": [ + "window" + ] + }, + "window-status-last-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "window-status-separator": { + "type": "string", + "scopes": [ + "window" + ] + }, + "window-status-style": { + "type": "string", + "scopes": [ + "window" + ] + }, + "window-style": { + "type": "string", + "scopes": [ + "window", + "pane" + ] + }, + "word-separators": { + "type": "string", + "scopes": [ + "session" + ] + }, + "wrap-search": { + "type": "flag", + "scopes": [ + "window" + ] + }, + "xterm-keys": { + "type": "flag", + "scopes": [ + "window" + ] + } + }, + "hooks": { + "after-bind-key": [ + "session" + ], + "after-capture-pane": [ + "session" + ], + "after-copy-mode": [ + "session" + ], + "after-display-message": [ + "session" + ], + "after-display-panes": [ + "session" + ], + "after-kill-pane": [ + "session" + ], + "after-list-buffers": [ + "session" + ], + "after-list-clients": [ + "session" + ], + "after-list-keys": [ + "session" + ], + "after-list-panes": [ + "session" + ], + "after-list-sessions": [ + "session" + ], + "after-list-windows": [ + "session" + ], + "after-load-buffer": [ + "session" + ], + "after-lock-server": [ + "session" + ], + "after-new-session": [ + "session" + ], + "after-new-window": [ + "session" + ], + "after-paste-buffer": [ + "session" + ], + "after-pipe-pane": [ + "session" + ], + "after-refresh-client": [ + "session" + ], + "after-rename-session": [ + "session" + ], + "after-rename-window": [ + "session" + ], + "after-resize-pane": [ + "session" + ], + "after-resize-window": [ + "session" + ], + "after-save-buffer": [ + "session" + ], + "after-select-layout": [ + "session" + ], + "after-select-pane": [ + "session" + ], + "after-select-window": [ + "session" + ], + "after-send-keys": [ + "session" + ], + "after-set-buffer": [ + "session" + ], + "after-set-environment": [ + "session" + ], + "after-set-hook": [ + "session" + ], + "after-set-option": [ + "session" + ], + "after-show-environment": [ + "session" + ], + "after-show-messages": [ + "session" + ], + "after-show-options": [ + "session" + ], + "after-split-window": [ + "session" + ], + "after-swap-window": [ + "session" + ], + "after-unbind-key": [ + "session" + ], + "alert-activity": [ + "session" + ], + "alert-bell": [ + "session" + ], + "alert-silence": [ + "session" + ], + "client-active": [ + "session" + ], + "client-attached": [ + "session" + ], + "client-closed": [ + "session" + ], + "client-created": [ + "session" + ], + "client-dark-theme": [ + "session" + ], + "client-detached": [ + "session" + ], + "client-focus-in": [ + "session" + ], + "client-focus-out": [ + "session" + ], + "client-light-theme": [ + "session" + ], + "client-resized": [ + "session" + ], + "client-session-changed": [ + "session" + ], + "command-error": [ + "session" + ], + "marked-pane-changed": [ + "session" + ], + "pane-activity": [ + "window", + "pane" + ], + "pane-bell": [ + "window", + "pane" + ], + "pane-command-finished": [ + "window", + "pane" + ], + "pane-command-started": [ + "window", + "pane" + ], + "pane-created": [ + "window", + "pane" + ], + "pane-died": [ + "window", + "pane" + ], + "pane-exited": [ + "window", + "pane" + ], + "pane-focus-in": [ + "window", + "pane" + ], + "pane-focus-out": [ + "window", + "pane" + ], + "pane-mode-changed": [ + "window", + "pane" + ], + "pane-mode-entered": [ + "window", + "pane" + ], + "pane-mode-exited": [ + "window", + "pane" + ], + "pane-moved": [ + "window", + "pane" + ], + "pane-prompt-closed": [ + "window", + "pane" + ], + "pane-prompt-opened": [ + "window", + "pane" + ], + "pane-resized": [ + "window", + "pane" + ], + "pane-set-clipboard": [ + "window", + "pane" + ], + "pane-shell-prompt": [ + "window", + "pane" + ], + "pane-title-changed": [ + "window", + "pane" + ], + "session-added-to-group": [ + "session" + ], + "session-closed": [ + "session" + ], + "session-created": [ + "session" + ], + "session-removed-from-group": [ + "session" + ], + "session-renamed": [ + "session" + ], + "session-window-changed": [ + "session" + ], + "window-closed": [ + "window" + ], + "window-created": [ + "window" + ], + "window-layout-changed": [ + "window" + ], + "window-linked": [ + "session" + ], + "window-pane-changed": [ + "window" + ], + "window-renamed": [ + "window" + ], + "window-resized": [ + "window" + ], + "window-unlinked": [ + "session" + ], + "window-unzoomed": [ + "window" + ], + "window-zoomed": [ + "window" + ] + } +} diff --git a/gems/libtmux/sig/option_catalog.rbs b/gems/libtmux/sig/option_catalog.rbs new file mode 100644 index 00000000..97109b61 --- /dev/null +++ b/gems/libtmux/sig/option_catalog.rbs @@ -0,0 +1,26 @@ +module LibTmux + module OptionCatalog + type scope = :server | :session | :window | :pane + type option_type = :string | :number | :key | :colour | :flag | :choice | :command + + class Option + private def self.new: (*untyped) -> instance + attr_reader name: String + attr_reader type: option_type + attr_reader scopes: Array[scope] + attr_reader range: Range[Integer]? + attr_reader choices: Array[String] + def array?: () -> bool + end + + class Hook + private def self.new: (*untyped) -> instance + attr_reader name: String + attr_reader scopes: Array[scope] + end + + def self.tmux_version: () -> String + def self.options: () -> Hash[String, Option] + def self.hooks: () -> Hash[String, Hook] + end +end diff --git a/scripts/export-docs/tests/packaging/docs_export_test.rb b/scripts/export-docs/tests/packaging/docs_export_test.rb index 5f0b2c2f..afbbcfa5 100644 --- a/scripts/export-docs/tests/packaging/docs_export_test.rb +++ b/scripts/export-docs/tests/packaging/docs_export_test.rb @@ -33,8 +33,11 @@ def test_export_covers_the_public_inventory_without_machine_paths assert symbols.all? { |symbol| symbol.dig("source", "path").start_with?("gems/") } assert symbols.all? { |symbol| symbol.fetch("visibility") == "public" } assert_includes packages.find { |entry| entry.fetch("name") == "libtmux-async" }.fetch("rbs"), "scope_diagnostics" - assert_equal ["LibTmux::Async::scope_diagnostics"], aliases.map { |entry| entry.fetch("id") } - assert_equal "libtmux-async", aliases.first.fetch("package") + assert_equal [ + ["LibTmux::Async::scope_diagnostics", "libtmux-async"], + ["LibTmux::OptionCatalog::option_type", "libtmux"], + ["LibTmux::OptionCatalog::scope", "libtmux"] + ], aliases.map { |entry| entry.values_at("id", "package") } refute_match(%r{/(?:home|Users)/}, JSON.generate(payload)) end diff --git a/scripts/option-catalog/README.md b/scripts/option-catalog/README.md new file mode 100644 index 00000000..27f1b4ef --- /dev/null +++ b/scripts/option-catalog/README.md @@ -0,0 +1,23 @@ +# Option catalog generator + +This script reads the tmux 3.8 `options_table[]` initializers and hook macros. +It uses `tmux.h` for numeric limits named by the table. The output is +[`option-catalog.json`](../../gems/libtmux/schema/option-catalog.json), shipped +in the core gem and read by `LibTmux::OptionCatalog`. + +Use the source at tmux commit `7f2a35ad3321f9ba57a1062ca73b1f3ff26aca53`. +The script checks the SHA-256 of both input files. Pass the local directory +containing these files to regenerate the catalog. + +```console +$ bundle exec ruby scripts/option-catalog/generate.rb /path/to/tmux-3.8 +``` + +Verify the generated catalog against the same pin. + +```console +$ bundle exec ruby scripts/option-catalog/generate.rb --check /path/to/tmux-3.8 +``` + +When updating the pin, change the release, revision and file hashes together. +Run the generator tests and the native catalog comparison on the new release. diff --git a/scripts/option-catalog/generate.rb b/scripts/option-catalog/generate.rb new file mode 100644 index 00000000..dee03bf9 --- /dev/null +++ b/scripts/option-catalog/generate.rb @@ -0,0 +1,197 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require "digest" +require "json" + +class TmuxOptionCatalog + Error = Class.new(StandardError) + VERSION = "3.8" + REVISION = "7f2a35ad3321f9ba57a1062ca73b1f3ff26aca53" + SOURCE_FILES = { + "options-table.c" => "cbf3b034785b30aeee3d67032f99e9d9b173a321e11b19be1a11c9e9914fc866", + "tmux.h" => "88f17595490be65d26c060ba4e39548d6fdd552847ef95889c9b7fa8767c1e7a" + }.freeze + C_LIMITS = {"SHRT_MAX" => 32_767, "USHRT_MAX" => 65_535, + "INT_MAX" => 2_147_483_647, "UINT_MAX" => 4_294_967_295}.freeze + SCOPES = %w[server session window pane].freeze + TYPES = %w[string number key colour flag choice command].freeze + TOKEN = %r{/\*.*?\*/|//[^\n]*|"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[A-Za-z_]\w*|-?\d+|\S}m + + def self.read(directory) + files = SOURCE_FILES.map do |name, sha256| + contents = File.binread(File.join(directory, name)) + unless Digest::SHA256.hexdigest(contents) == sha256 + raise Error, "#{name} does not match tmux #{VERSION} at #{REVISION}" + end + + contents + end + new(*files).parse + end + + def initialize(source, header) + # Keep quoted strings intact so C comments and delimiters inside them are literal. + @source = source.gsub(/\\\r?\n/, "").gsub(TOKEN) do |token| + token.start_with?("/*", "//") ? " #{"\n" * token.count("\n")}" : token + end + @tokens = @source.scan(TOKEN) + @bounds = header.scan(/^#define[ \t]+(\w+)[ \t]+(-?\d+)[ \t]*$/).to_h + .transform_values { |value| Integer(value, 10) }.merge(C_LIMITS) + end + + def parse + entries = initializer("const struct options_table_entry options_table") + raise Error, "missing options table sentinel" unless entries.pop == ["{", ".", "name", "=", "NULL", "}"] + + options, hooks = {}, {} + macros = hook_macros + entries.each do |entry| + if entry.first == "{" + fields = designators(entry) + name = literal(fields.fetch("name").join) + catalog = options + definition = option(fields) + else + macro, open, name_token = entry + raise Error, "expected a hook invocation" unless open == "(" && entry.last == ")" + + prefix, definition = macros.fetch(macro) + name = prefix + literal(name_token) + catalog = hooks + end + raise Error, "duplicate entry #{name}" if options.key?(name) || hooks.key?(name) + + catalog[name] = definition + end + {"tmux_version" => VERSION, "source_revision" => REVISION, "source_sha256" => SOURCE_FILES, + "options" => options.sort.to_h, "hooks" => hooks.sort.to_h} + rescue KeyError => error + raise Error, "unrecognized options table entry or field #{error.key.inspect}" + end + + private + + def option(fields) + type = table_name(fields.fetch("type").join, TYPES) + result = {"type" => type, "scopes" => scopes(fields)} + result["array"] = true if fields.fetch("flags", []).include?("OPTIONS_TABLE_IS_ARRAY") + case type + when "number" + result["range"] = %w[minimum maximum].map do |field| + value = fields.fetch(field, ["0"]).join + @bounds.fetch(value) { Integer(value, 10) } + rescue ArgumentError + raise Error, "unknown numeric bound #{value.inspect}" + end + when "choice" + name = fields.fetch("choices").join + items = initializer("static const char * #{name}") + raise Error, "missing sentinel in #{name}" unless items.pop == ["NULL"] + + result["choices"] = items.map { |item| literal(item.join) } + end + result + end + + def hook_macros + definitions = @source.scan(/^#define[ \t]+(OPTIONS_TABLE_\w*HOOK)\([^)]*\)[ \t]+([^\n]+)$/).to_h + after = definitions.delete("OPTIONS_TABLE_AFTER_HOOK") + macros = definitions.transform_values { |definition| ["", scopes(designators(definition.scan(TOKEN)))] } + if after + macro, open, prefix = after.scan(TOKEN) + raise Error, "expected OPTIONS_TABLE_AFTER_HOOK to delegate to a hook macro" unless open == "(" + + macros["OPTIONS_TABLE_AFTER_HOOK"] = [literal(prefix), macros.fetch(macro).last] + end + macros + end + + def scopes(fields) + fields.fetch("scope").join.split("|").map { |word| table_name(word, SCOPES) } + end + + def table_name(word, names) + name = word.delete_prefix("OPTIONS_TABLE_").downcase + return name if word.start_with?("OPTIONS_TABLE_") && names.include?(name) + + raise Error, "unknown table constant #{word.inspect}" + end + + def literal(token) + match = /\A"([^"\\]*)"\z/.match(token) + raise Error, "expected a plain C string in the option table" unless match + + match[1] + end + + def designators(tokens) + unless tokens.first == "{" && tokens.last == "}" + raise Error, "expected a struct initializer in the option table" + end + + entries(tokens[1...-1]).to_h do |dot, name, equals, *value| + raise Error, "expected a designated field in the option table" unless dot == "." && equals == "=" + + [name, value] + end + end + + def initializer(declaration) + beginning = "#{declaration} [ ] = {".split + start = @tokens.each_cons(beginning.length).with_index.find { |part, _| part == beginning }&.last + raise Error, "missing #{declaration}" unless start + + start += beginning.length + depth = 1 + body = @tokens.drop(start).take_while do |token| + depth += 1 if token == "{" + depth -= 1 if token == "}" + depth.positive? + end + raise Error, "unterminated #{declaration}" unless @tokens[start + body.length, 2] == ["}", ";"] + + entries(body) + end + + def entries(tokens) + rows = [] + first = 0 + depth = 0 + tokens.each_with_index do |token, index| + case token + when "{", "(", "[" then depth += 1 + when "}", ")", "]" then depth -= 1 + when "," + if depth.zero? + rows << tokens[first...index] + first = index + 1 + end + end + raise Error, "unbalanced option table initializer" if depth.negative? + end + raise Error, "unbalanced option table initializer" unless depth.zero? + + rows << tokens[first..] if first < tokens.length + rows + end +end + +if $PROGRAM_NAME == __FILE__ + check = ARGV.delete("--check") + abort "usage: #{__FILE__} [--check] TMUX_SOURCE_DIRECTORY" unless ARGV.length == 1 + + begin + catalog = TmuxOptionCatalog.read(ARGV.first) + output = JSON.pretty_generate(catalog) + "\n" + path = File.expand_path("../../gems/libtmux/schema/option-catalog.json", __dir__) + if check + abort "option catalog is stale; regenerate it from the pinned tmux source" unless File.file?(path) && File.binread(path) == output + else + File.binwrite(path, output) + end + puts "tmux #{catalog.fetch("tmux_version")} catalog: #{catalog.fetch("options").length} options and #{catalog.fetch("hooks").length} hooks" + rescue SystemCallError, TmuxOptionCatalog::Error => error + abort error.message + end +end diff --git a/scripts/option-catalog/tests/generate_test.rb b/scripts/option-catalog/tests/generate_test.rb new file mode 100644 index 00000000..0776cccf --- /dev/null +++ b/scripts/option-catalog/tests/generate_test.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require_relative "../../../test/test_helper" +require_relative "../generate" +require "tmpdir" + +class GenerateOptionCatalogTest < Minitest::Test + TABLE = <<~'C' + static const char *positions[] = { "top", "bottom", NULL }; + #define OPTIONS_TABLE_HOOK(name) { .name = name, .scope = OPTIONS_TABLE_SESSION } + #define OPTIONS_TABLE_PANE_HOOK(name) \ + { .name = name, .scope = OPTIONS_TABLE_WINDOW | OPTIONS_TABLE_PANE } + #define OPTIONS_TABLE_WINDOW_HOOK(name) { .name = name, .scope = OPTIONS_TABLE_WINDOW } + #define OPTIONS_TABLE_AFTER_HOOK(name) OPTIONS_TABLE_HOOK("after-" name) + const struct options_table_entry options_table[] = { + /* { .name = "removed-option", .type = OPTIONS_TABLE_STRING }, */ + { .name = "history-limit", .type = OPTIONS_TABLE_NUMBER, .scope = OPTIONS_TABLE_SESSION, + .minimum = EXAMPLE_MIN, .maximum = INT_MAX, + .text = "The string can contain }, commas, /* text */ and \"escaped quotes\"." }, + { .name = "status-position", .type = OPTIONS_TABLE_CHOICE, .scope = OPTIONS_TABLE_SESSION, + .choices = positions }, + { .name = "command-alias", .type = OPTIONS_TABLE_STRING, .scope = OPTIONS_TABLE_SERVER, + .flags = OPTIONS_TABLE_IS_ARRAY }, + OPTIONS_TABLE_HOOK("client-created"), + OPTIONS_TABLE_WINDOW_HOOK("window-created"), + OPTIONS_TABLE_PANE_HOOK("pane-created"), + OPTIONS_TABLE_AFTER_HOOK("new-window"), + { .name = NULL } + }; + C + + def test_reads_the_option_initializers_and_all_hook_forms + catalog = TmuxOptionCatalog.new(TABLE, "#define EXAMPLE_MIN -2\n").parse + + assert_equal({ + "command-alias" => {"type" => "string", "scopes" => ["server"], "array" => true}, + "history-limit" => {"type" => "number", "scopes" => ["session"], "range" => [-2, 2_147_483_647]}, + "status-position" => {"type" => "choice", "scopes" => ["session"], "choices" => %w[top bottom]} + }, catalog.fetch("options")) + assert_equal({ + "after-new-window" => ["session"], "client-created" => ["session"], + "pane-created" => %w[window pane], "window-created" => ["window"] + }, catalog.fetch("hooks")) + end + + def test_refuses_unknown_option_types + source = TABLE.sub("OPTIONS_TABLE_CHOICE", "OPTIONS_TABLE_OTHER") + error = assert_raises(TmuxOptionCatalog::Error) do + TmuxOptionCatalog.new(source, "#define EXAMPLE_MIN -2\n").parse + end + assert_includes error.message, "OPTIONS_TABLE_OTHER" + end + + def test_refuses_source_bytes_outside_the_pin + Dir.mktmpdir("libtmux-ruby-option-catalog-") do |directory| + File.write(File.join(directory, "options-table.c"), TABLE) + error = assert_raises(TmuxOptionCatalog::Error) { TmuxOptionCatalog.read(directory) } + assert_includes error.message, "options-table.c does not match tmux 3.8" + end + end +end diff --git a/test/integration/operations_test.rb b/test/integration/operations_test.rb index c3072214..039e859b 100644 --- a/test/integration/operations_test.rb +++ b/test/integration/operations_test.rb @@ -184,6 +184,28 @@ def test_write_punctuated_user_options_read_by_tmux end end + def test_catalog_lists_the_options_and_hooks_declared_by_its_tmux_release + with_server do |server, fixture| + catalog = LibTmux::OptionCatalog + option = catalog.options.fetch("status-position") + assert_includes option.choices, server.options(scope: :session).get(option.name).raw + hook = catalog.hooks.fetch("after-new-window") + session = server.list_sessions.fetch(0) + session.hooks.set(hook.name, command: "display-message catalog", index: 4) + assert_equal [4], session.hooks.list(name: hook.name).map(&:index) + + # Only the source release has exactly the catalog's set of built-in names. + next unless server.tmux_version == catalog.tmux_version + + [[:server, "-s"], [:session, "-g"], [:window, "-gw"], [:pane, "-Ap"]].each do |scope, flags| + assert_catalog_scope(fixture, "show-options", flags, scope, catalog.options) + end + [[:session, "-g"], [:window, "-gw"]].each do |scope, flags| + assert_catalog_scope(fixture, "show-hooks", flags, scope, catalog.hooks) + end + end + end + def test_buffer_bytes_capture_flags_and_copy_commands with_server do |server, _fixture| pane = server.list_panes.fetch(0) @@ -460,6 +482,13 @@ def test_source_file_and_wait_channels_preserve_literal_arguments_and_final_outc private + def assert_catalog_scope(fixture, command, flags, scope, entries) + output, error, status = fixture.tmux(command, flags, "-F", '#{option_name}') + assert status.success?, error + expected = entries.values.select { |entry| entry.scopes.include?(scope) }.map(&:name) + assert_equal expected, output.lines(chomp: true).uniq.sort, "#{command} #{flags}" + end + # tmux 3.8 dumps layouts as JSON and marks the active or last pane in them. def without_focus(layout) layout.gsub(/,"(?:a":true|l":\d+)/, "") diff --git a/test/types/consumer.rb b/test/types/consumer.rb index 4854f51c..9c22cdd3 100644 --- a/test/types/consumer.rb +++ b/test/types/consumer.rb @@ -122,6 +122,14 @@ def verify_rejects_wrong_return(value = Object.new) consumer.call("::LibTmux::Options", options, :set, "@types", true, expected: "::LibTmux::CommandResult") option = consumer.call("::LibTmux::Options", options, :get, "@types", expected: "::LibTmux::OptionValue") consumer.call("::LibTmux::OptionValue", option, :as, :boolean, expected: "bool") + catalog = LibTmux::OptionCatalog + consumer.call("singleton(::LibTmux::OptionCatalog)", catalog, :tmux_version, expected: "String") + definitions = consumer.call("singleton(::LibTmux::OptionCatalog)", catalog, :options, + expected: "Hash[String, ::LibTmux::OptionCatalog::Option]") + consumer.call("::LibTmux::OptionCatalog::Option", definitions.fetch("history-limit"), :range, expected: "Range[Integer]") + hooks = consumer.call("singleton(::LibTmux::OptionCatalog)", catalog, :hooks, + expected: "Hash[String, ::LibTmux::OptionCatalog::Hook]") + consumer.call("::LibTmux::OptionCatalog::Hook", hooks.fetch("pane-created"), :scopes, expected: "Array[Symbol]") result = consumer.call("::LibTmux::Pane", pane, :capture, expected: "::LibTmux::CommandResult") consumer.call("::LibTmux::CommandResult", result, :stdout, expected: "String") consumer.call("::LibTmux::CommandResult", result, :success?, expected: "bool") diff --git a/test/unit/option_catalog_test.rb b/test/unit/option_catalog_test.rb new file mode 100644 index 00000000..b6e595a5 --- /dev/null +++ b/test/unit/option_catalog_test.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require_relative "../test_helper" +require "libtmux" + +class OptionCatalogTest < Minitest::Test + def setup + assert LibTmux.const_defined?(:OptionCatalog, false), "libtmux exposes the tmux option and hook catalog" + @catalog = LibTmux::OptionCatalog + end + + def test_options_report_types_scopes_and_allowed_values + assert_equal "3.8", @catalog.tmux_version + history = @catalog.options.fetch("history-limit") + assert_equal "history-limit", history.name + assert_equal :number, history.type + assert_equal [:session], history.scopes + assert_equal 0..2_147_483_647, history.range + assert_empty history.choices + refute history.array? + + position = @catalog.options.fetch("status-position") + assert_equal :choice, position.type + assert_equal ["top", "bottom"], position.choices + assert_nil position.range + assert @catalog.options.fetch("command-alias").array? + assert_equal [:window, :pane], @catalog.options.fetch("remain-on-exit").scopes + assert_equal 1_048_576, @catalog.options.fetch("input-buffer-size").range.begin + assert_nil @catalog.options["@custom"] + end + + def test_hooks_report_the_scope_of_each_upstream_macro + scopes_by_name = { + "after-new-window" => [:session], + "client-created" => [:session], + "pane-created" => [:window, :pane], + "window-created" => [:window], + "window-zoomed" => [:window] + } + scopes_by_name.each do |name, scopes| + hook = @catalog.hooks.fetch(name) + assert_equal name, hook.name + assert_equal scopes, hook.scopes + assert_nil @catalog.options[name] + end + assert_nil @catalog.hooks["history-limit"] + end + + def test_catalog_is_immutable_and_sorted_by_name + assert_equal @catalog.options.keys.sort, @catalog.options.keys + assert_equal @catalog.hooks.keys.sort, @catalog.hooks.keys + assert_predicate @catalog.options, :frozen? + assert_predicate @catalog.hooks, :frozen? + + position = @catalog.options.fetch("status-position") + assert_predicate position, :frozen? + assert_predicate position.name, :frozen? + assert_predicate position.scopes, :frozen? + assert_predicate position.choices, :frozen? + assert position.choices.all?(&:frozen?) + end +end