iterm2_ruby 0.2.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e7e477efa0c27e00e1cb34267975a48402332821b9ae336c55425ad3733e0bab
4
+ data.tar.gz: fbb8e0de6adce1dcf2fdca490c36bde11f1b652755d49d2520d18d33468b5be1
5
+ SHA512:
6
+ metadata.gz: 9ce8e36e0bc21000133f8d6fa0cc037aed5902812a298e5732e7865452a7f7b10fe1917882cf94d861bf55be6d83dc1bb39dce826d0bbf50d6f194b768fbe1f6
7
+ data.tar.gz: b2bbc939e3c51138ff675655f2a9ba421fdba58ae0e2250c64640d91f5096315c3ef0f9283f07b53269fe5c9d555f587e9b16203d26df469f620756cd792fbd2
data/CHANGELOG.md ADDED
@@ -0,0 +1,23 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented here. Format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
5
+
6
+ ## [0.2.0] - 2026-07-02
7
+
8
+ ### Added
9
+ - `ITerm2::Window`, `ITerm2::Tab`, `ITerm2::Session` — object wrappers over the
10
+ flat client API (`client.windows`, `client.tabs`, `client.sessions`)
11
+ - `client.reorder_tabs(assignments)` — move/reorder tabs across windows
12
+ - CLI: `tabs`, `move`, `send-text`, `read-screen`, `activate-session`, `list --triage`
13
+
14
+ ### Changed
15
+ - Dropped explicit `base64`/`ostruct` gemspec dependencies (stdlib on Ruby >= 3.1)
16
+
17
+ ## [0.1.0] - 2026-02-12
18
+
19
+ ### Added
20
+ - Initial release: `ITerm2::Connection` (auth, WebSocket handshake, binary framing)
21
+ - `ITerm2::Client`: session topology, send text, read screen, activate/raise,
22
+ create tab, split pane, close, profile properties, variables, notifications
23
+ - `iterm2ctl` CLI
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ group :development, :test do
8
+ gem "rspec", "~> 3.0"
9
+ gem "rake", "~> 13.0"
10
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Forrest Chang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,265 @@
1
+ # iterm2_ruby
2
+
3
+ [![Gem Version](https://img.shields.io/gem/v/iterm2_ruby.svg)](https://rubygems.org/gems/iterm2_ruby)
4
+ [![CI](https://github.com/fkchang/iterm2_ruby/actions/workflows/ci.yml/badge.svg)](https://github.com/fkchang/iterm2_ruby/actions/workflows/ci.yml)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
+
7
+ ![Same chaos, now findable](assets/hero.jpg)
8
+
9
+ Ruby gem + CLI for controlling iTerm2 via its native WebSocket + Protobuf API.
10
+
11
+ **Too many iTerms. Too many tabs. Too many agents "helping" you at once.**
12
+
13
+ GenAI made you think you can do 20 things at once. It was wrong, but you did it anyway and you're still doing it, and now you've got 20 terminal windows and no idea which one's stuck waiting on you. `osascript` is too slow to keep up, steals your focus every time it tries, and nags you for an Automation permission it already has. iterm2_ruby skips all of that: it talks to iTerm2 directly, so you can actually navigate the mess you made -- ~20x faster, no focus theft, plus real-time event notifications so you don't have to go looking in the first place.
14
+
15
+ ## Real-World Usage
16
+
17
+ Four things it's doing today, in real projects:
18
+
19
+ **Jump to any running session instantly, by project name.** No window-hunting:
20
+
21
+ ```ruby
22
+ ITerm2.connect { |c| c.raise_by_title(project_name) }
23
+ ```
24
+
25
+ No stolen focus, no AppleScript delay -- just the right window, front and center.
26
+
27
+ **A live dashboard across every open terminal.** One call returns every window, tab, and session, enriched with project name, status, cwd, and PID:
28
+
29
+ ```ruby
30
+ data = ITerm2.connect { |c| c.topology_for_aggregator }
31
+ ```
32
+
33
+ This feeds a real-time "mission control" view over a whole fleet of sessions.
34
+
35
+ **Split a pane and pop a live browser preview next to your code:**
36
+
37
+ ```ruby
38
+ ITerm2.connect do |c|
39
+ c.split_pane(guid, vertical: true, profile_name: "Web Browser",
40
+ profile_customizations: { "Initial URL" => url })
41
+ end
42
+ ```
43
+
44
+ **Spawn a background task in its own window, no permission dialogs:**
45
+
46
+ ```bash
47
+ iterm2ctl create window # => session E8F2...
48
+ iterm2ctl send "run-task.sh" --session E8F2...
49
+ ```
50
+
51
+ AppleScript automation triggers a macOS Automation prompt every time. This doesn't.
52
+
53
+ ## Installation
54
+
55
+ ```bash
56
+ gem install iterm2_ruby
57
+ ```
58
+
59
+ Or add to your Gemfile:
60
+
61
+ ```ruby
62
+ gem "iterm2_ruby"
63
+ ```
64
+
65
+ ## Prerequisites
66
+
67
+ - **macOS** with [iTerm2](https://iterm2.com/) installed
68
+ - **Ruby >= 3.1**
69
+ - iTerm2 API enabled: Preferences > General > Magic > Enable Python API
70
+
71
+ ## Quick Start (CLI)
72
+
73
+ ```bash
74
+ # List all windows/tabs/sessions
75
+ iterm2ctl list
76
+
77
+ # Send text to the active session
78
+ iterm2ctl send "echo hello"
79
+
80
+ # Read visible screen contents
81
+ iterm2ctl read
82
+
83
+ # Raise tab matching a title pattern
84
+ iterm2ctl raise "my-project"
85
+
86
+ # Show current focus state
87
+ iterm2ctl focus
88
+
89
+ # Watch for events in real-time
90
+ iterm2ctl watch
91
+ ```
92
+
93
+ ## Quick Start (Ruby API)
94
+
95
+ ### One-shot (auto-connects, runs command, disconnects)
96
+
97
+ ```ruby
98
+ require "iterm2"
99
+
100
+ # List all sessions
101
+ sessions = ITerm2.topology
102
+ sessions.each { |s| puts "#{s[:session_id]} - #{s[:title]}" }
103
+
104
+ # Send text to a session
105
+ ITerm2.send_text(session_id, "ls -la\n")
106
+
107
+ # Read screen contents
108
+ screen = ITerm2.read_screen(session_id)
109
+ puts screen[:lines].join("\n")
110
+
111
+ # Raise a tab by title
112
+ ITerm2.raise_by_title("my-project")
113
+
114
+ # Get current focus
115
+ focus = ITerm2.focus
116
+ puts focus[:active_session]
117
+ ```
118
+
119
+ ### Persistent client (reuses connection)
120
+
121
+ ```ruby
122
+ require "iterm2"
123
+
124
+ ITerm2.connect do |client|
125
+ # List sessions
126
+ sessions = client.topology
127
+ session_id = sessions.first[:session_id]
128
+
129
+ # Get detailed session info
130
+ info = client.session_info(session_id)
131
+ puts "CWD: #{info[:cwd]}, PID: #{info[:pid]}"
132
+
133
+ # Watch for focus changes
134
+ client.on_focus_change do |event|
135
+ puts "Focus changed: #{event}"
136
+ end
137
+
138
+ sleep # keep connection open for notifications
139
+ end
140
+ ```
141
+
142
+ ## CLI Command Reference
143
+
144
+ | Command | Description |
145
+ |---|---|
146
+ | `list` | List all windows/tabs/sessions |
147
+ | `list --with-cwd` | Include working directories |
148
+ | `list --with-pid` | Include PIDs |
149
+ | `list --triage` | Compact triage view (window/tab/session/cwd/job) |
150
+ | `tabs` | List tabs grouped by window |
151
+ | `send TEXT` | Send text to a session |
152
+ | `send-text ID TEXT` | Send text to a specific session by ID |
153
+ | `read` | Read visible screen contents |
154
+ | `read-screen ID` | Read screen for a specific session by ID |
155
+ | `raise PATTERN` | Raise tab matching title pattern |
156
+ | `raise --cwd PATH` | Raise tab by working directory |
157
+ | `activate-session ID` | Activate a session directly by ID |
158
+ | `create [window\|tab]` | Create a new window or tab |
159
+ | `split` | Split the current pane |
160
+ | `close` | Close a session |
161
+ | `move --tab ID --to-window ID` | Move a tab to another window |
162
+ | `var get NAME` | Get a variable |
163
+ | `var set NAME VALUE` | Set a variable (user.* only) |
164
+ | `var all` | Dump all variables |
165
+ | `info` | Show session tty, pid, cwd, job |
166
+ | `focus` | Show current focus state |
167
+ | `prompt` | Show prompt state |
168
+ | `watch [TYPE]` | Watch for events (focus/sessions/prompt/screen/layout) |
169
+ | `profile [KEYS]` | Get profile properties |
170
+ | `profiles` | List all profiles |
171
+ | `inject TEXT` | Inject data as if from process |
172
+ | `version` | Show version |
173
+ | `help` | Show help |
174
+
175
+ Most commands accept `--session ID`, `--tab ID`, or `--window ID` to target a specific object. Use `--json` for machine-readable output.
176
+
177
+ ## Ruby API Reference
178
+
179
+ ### Connection
180
+
181
+ | Method | Description |
182
+ |---|---|
183
+ | `ITerm2.connect { \|client\| ... }` | Open connection, yield client, auto-close |
184
+ | `ITerm2::Client.new` | Create persistent client |
185
+ | `client.close` | Close connection |
186
+
187
+ ### Topology
188
+
189
+ | Method | Description |
190
+ |---|---|
191
+ | `list_sessions` | Raw ListSessionsResponse from iTerm2 |
192
+ | `topology` | Flat array of `{window_id:, tab_id:, session_id:, title:}` |
193
+ | `topology_enriched` | Topology with cwd, pid, tty, job added |
194
+ | `session_info(session_id)` | Get tty, pid, cwd, name, job for a session |
195
+
196
+ ### Session Interaction
197
+
198
+ | Method | Description |
199
+ |---|---|
200
+ | `send_text(session_id, text)` | Send text to a session |
201
+ | `read_screen(session_id)` | Read visible screen (returns `{lines:, cursor:}`) |
202
+ | `inject(session_id, data)` | Inject data as if from process |
203
+
204
+ ### Window Management
205
+
206
+ | Method | Description |
207
+ |---|---|
208
+ | `activate_session(session_id)` | Raise and focus a session |
209
+ | `activate_tab(tab_id)` | Raise a tab |
210
+ | `activate_window(window_id)` | Raise a window |
211
+ | `raise_by_title(pattern)` | Raise first session matching title |
212
+ | `raise_by_cwd(pattern)` | Raise first session matching cwd |
213
+ | `create_tab(window_id:, profile_name:)` | Create a new tab |
214
+ | `split_pane(session_id, vertical:)` | Split a pane |
215
+ | `close_session(session_id)` | Close a session |
216
+ | `close_tab(tab_id)` | Close a tab |
217
+ | `reorder_tabs(window_id => [tab_ids])` | Move/reorder tabs across windows |
218
+
219
+ ### Profile & Properties
220
+
221
+ | Method | Description |
222
+ |---|---|
223
+ | `set_profile_property(session_id, key, value)` | Set a profile property |
224
+ | `get_profile_property(session_id, *keys)` | Get profile properties |
225
+ | `list_profiles(properties:, guids:)` | List all profiles |
226
+ | `get_property(name, session_id:)` | Get a session/window property |
227
+ | `get_variables(*names, session_id:)` | Get variables |
228
+ | `set_variables(vars, session_id:)` | Set user variables |
229
+ | `focus` | Get current focus state |
230
+ | `get_prompt(session_id)` | Get prompt state |
231
+
232
+ ### Notifications (Real-time Events)
233
+
234
+ | Method | Description |
235
+ |---|---|
236
+ | `on_focus_change { \|event\| }` | Focus changed |
237
+ | `on_new_session { \|event\| }` | New session created |
238
+ | `on_session_terminated { \|event\| }` | Session terminated |
239
+ | `on_prompt_change(session_id) { \|event\| }` | Prompt state changed |
240
+ | `on_screen_update(session_id) { \|event\| }` | Screen content changed |
241
+ | `on_layout_change { \|event\| }` | Layout changed |
242
+
243
+ ## How It Works
244
+
245
+ 1. **Authentication**: One-time osascript call gets a cookie+key from iTerm2
246
+ 2. **Connection**: WebSocket to iTerm2's local API (Unix socket or TCP port 1912)
247
+ 3. **Protocol**: Google Protocol Buffers for message encoding (binary, fast)
248
+ 4. **Notifications**: Background dispatch thread routes events to registered callbacks
249
+
250
+ No external WebSocket gems required -- framing is hand-rolled per RFC 6455.
251
+
252
+ ## Documentation
253
+
254
+ - [`docs/cli.md`](docs/cli.md) -- full `iterm2ctl` command reference
255
+ - [`docs/api.md`](docs/api.md) -- complete Ruby API reference
256
+ - [`docs/architecture.md`](docs/architecture.md) -- connection, auth, and dispatch design
257
+ - [`llms.txt`](llms.txt) -- condensed reference for LLM/agent use
258
+
259
+ ## Contributing
260
+
261
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fkchang/iterm2_ruby.
262
+
263
+ ## License
264
+
265
+ MIT License. See [LICENSE](LICENSE) for details.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec