oops-cli 0.4.0 → 0.5.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 +4 -4
- data/CHANGELOG.ja.md +13 -0
- data/CHANGELOG.md +13 -0
- data/README.ja.md +87 -16
- data/README.md +87 -16
- data/lib/oops/cli.rb +24 -9
- data/lib/oops/formatter.rb +36 -26
- data/lib/oops/recorder.rb +10 -5
- data/lib/oops/session_log.rb +95 -15
- data/lib/oops/shell/oops.bash +30 -2
- data/lib/oops/stream_parser.rb +25 -2
- data/lib/oops/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6e78b65166ce70966491bfbda8f353c458cdcd3bdf21a082e1aef7abaaee872e
|
|
4
|
+
data.tar.gz: 043bcf8229c508f00e5b331f00396dbcd170e899afacc3e6e0b41ca43b1b22d6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b4ccbb6e8210f8a0427befc7618a04e0c061ec8e59130473ba2be87d72d2a739c5197121a23de1f52e9b43e15a51d9569f91accc2c224ab49429bbe0519fdd9d
|
|
7
|
+
data.tar.gz: a61a54bb2f9911212a6d632ef3c7f65f632a4042830f428d643842c9b3379d256e2ad76014f2a9a693a56a981958fe4755f43b4821a9d882494d21814defe045
|
data/CHANGELOG.ja.md
CHANGED
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
[English](CHANGELOG.md) | 日本語
|
|
4
4
|
|
|
5
|
+
## 0.5.0 - 2026-11-07
|
|
6
|
+
|
|
7
|
+
- 追加: `--json` で、選んだコマンドを JSON の配列(コマンド・ディレクトリ・終了コード・開始と終了の時刻・出力)として標準出力へ書き出せるようにしました。クリップボードと同じように整えてマスクします。スクリプトやエージェント向けです([#43](https://github.com/ysb3996-blip/oops/issues/43))。
|
|
8
|
+
- 削除: `--tail N` と `--head N` オプションを消しました。[#30](https://github.com/ysb3996-blip/oops/issues/30) で残してほしいという声はありませんでした。長い出力を短くするときは macOS なら `oops -p | tail -n 50 | pbcopy` のようにパイプでつないでください。
|
|
9
|
+
- 追加: このリポジトリに Claude Code のプラグインを置きました。`/plugin install oops --marketplace ysb3996-blip/oops` で入れると `/oops:err` が使え、そのタブで最後に失敗したコマンドを会話に入れます。直前に失敗したコマンドについて聞くと、Claude Code が自分から使います。README の「AI コーディングエージェントと使う」を参照してください。
|
|
10
|
+
|
|
11
|
+
## 0.4.1 - 2026-11-04
|
|
12
|
+
|
|
13
|
+
- 修正: 閉じたタブのプロセス番号が別のプロセスに使い回されると、そのログがいつまでも消えなかった問題を直しました。ログに最後に書き込まれた後に起動したプロセスは、そのログを書いたタブではないと判断します([#58](https://github.com/ysb3996-blip/oops/issues/58))。
|
|
14
|
+
- 変更: 開いたままのタブも、`retention_days` より古いコマンドを自分のログから落とすようにしました。確かめるのはコマンドが終わったとき(1 時間に 1 回まで)です([#59](https://github.com/ysb3996-blip/oops/issues/59))。
|
|
15
|
+
- Ghostty 上の bash: Ghostty が外側のシェルにシェル統合を入れていた場合、記録される側のシェルでもう一度読み込むようにしました。zsh と fish はすでにそうしていました。README で bash の人に頼んでいた 1 行は、この場合は要りません([#50](https://github.com/ysb3996-blip/oops/issues/50))。
|
|
16
|
+
- 変更: 全画面アプリの出力の代わりに入る 1 行に、コマンド名を入れるようにしました。1 行に 1 コマンドのときだけで、たとえばページャーを通した `git log` なら `[git: full-screen output omitted]` です([#45](https://github.com/ysb3996-blip/oops/issues/45))。
|
|
17
|
+
|
|
5
18
|
## 0.4.0 - 2026-11-03
|
|
6
19
|
|
|
7
20
|
- `--print` / `-p` を追加。クリップボードの代わりに整形したテキストを標準出力へ書き出し、SSH 先・コンテナ・スクリプトからパイプできるようにします([#40](https://github.com/ysb3996-blip/oops/issues/40))。[@ShahadatTest](https://github.com/ShahadatTest) さんによる貢献([#52](https://github.com/ysb3996-blip/oops/pull/52))。
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
English | [日本語](CHANGELOG.ja.md)
|
|
4
4
|
|
|
5
|
+
## 0.5.0 - 2026-11-07
|
|
6
|
+
|
|
7
|
+
- Added: `--json` writes the selected commands to stdout as a JSON array (command, directory, exit code, start and end times, output), cleaned and masked as on the clipboard, for scripts and agents ([#43](https://github.com/ysb3996-blip/oops/issues/43)).
|
|
8
|
+
- Removed: the `--tail N` and `--head N` options. Nobody asked to keep them in [#30](https://github.com/ysb3996-blip/oops/issues/30). To shorten a long output, pipe it instead, e.g. `oops -p | tail -n 50 | pbcopy` on macOS.
|
|
9
|
+
- Added: a Claude Code plugin in this repository. `/plugin install oops --marketplace ysb3996-blip/oops` adds `/oops:err`, which puts the tab's last failed command into the conversation; Claude Code also uses it when you ask about a command that just failed. See "With AI coding agents" in the README.
|
|
10
|
+
|
|
11
|
+
## 0.4.1 - 2026-11-04
|
|
12
|
+
|
|
13
|
+
- Fixed: a closed tab's log is no longer kept forever when its process id is reused by another process. A process that started after the log was last written is not the tab that wrote it ([#58](https://github.com/ysb3996-blip/oops/issues/58)).
|
|
14
|
+
- Changed: a tab that stays open now drops commands older than `retention_days` from its own log when a command finishes, at most once an hour ([#59](https://github.com/ysb3996-blip/oops/issues/59)).
|
|
15
|
+
- bash in Ghostty: when Ghostty injected its shell integration into the outer shell, the recorded shell now loads it again, as zsh and fish already did. The line the README asked bash users to add is no longer needed there ([#50](https://github.com/ysb3996-blip/oops/issues/50)).
|
|
16
|
+
- Changed: the line that stands in for a full-screen app's output names the command when the line holds one command, e.g. `[git: full-screen output omitted]` for `git log` through a pager ([#45](https://github.com/ysb3996-blip/oops/issues/45)).
|
|
17
|
+
|
|
5
18
|
## 0.4.0 - 2026-11-03
|
|
6
19
|
|
|
7
20
|
- `--print` / `-p`: write the formatted text to stdout instead of the clipboard, so SSH sessions, containers and scripts can pipe it ([#40](https://github.com/ysb3996-blip/oops/issues/40)). Contributed by [@ShahadatTest](https://github.com/ShahadatTest) in [#52](https://github.com/ysb3996-blip/oops/pull/52).
|
data/README.ja.md
CHANGED
|
@@ -4,10 +4,12 @@
|
|
|
4
4
|
|
|
5
5
|
[](https://rubygems.org/gems/oops-cli) [](https://github.com/ysb3996-blip/oops/actions/workflows/ci.yml) [](LICENSE) [](https://codecov.io/gh/ysb3996-blip/oops) [](#貢献してくれた人たち)
|
|
6
6
|
|
|
7
|
-
直前のシェルコマンドとその出力を、貼り付けられる形でクリップボードにコピーします。
|
|
7
|
+
直前のシェルコマンドとその出力を、貼り付けられる形でクリップボードにコピーします。Claude Code に「なんで失敗したの?」と聞けば、同じ記録を Claude が自分で読みます。
|
|
8
8
|
|
|
9
9
|
![デモ: テストが失敗し、oops がコマンドと出力をコピーし、コメント行と pbpaste でクリップボードの中身(コードブロック)を見せる。続いて cat .env、oops で API キーが [secret] になる](docs/demo.gif)
|
|
10
10
|
|
|
11
|
+
> **Claude Code を使っているなら:** `/plugin install oops --marketplace ysb3996-blip/oops` でプラグインを入れて、「なんで失敗したの?」と聞くだけです。失敗したコマンドと出力を、秘密をマスクした状態で Claude が自分で読むので、貼り付けも、Claude にコマンドを実行し直させることも要りません。[AI コーディングエージェントと使う](#ai-コーディングエージェントと使う)を参照してください。
|
|
12
|
+
|
|
11
13
|
コマンドを実行して失敗した。その結果をチャットのアシスタントや Slack の同僚、GitHub の issue に見せたい。普通は、出力をドラッグで選択してコピーし、貼り付けて、コードフェンスを自分で付ける、という手間がかかります。oops なら 2 ステップです。
|
|
12
14
|
|
|
13
15
|
1. `oops`(直前の失敗したコマンドなら `oops err`)と打つ。
|
|
@@ -59,6 +61,14 @@ source ~/.oops/oops.plugin.zsh
|
|
|
59
61
|
- `PATH` の Ruby が古いと、`oops: ... init zsh failed (oops needs Ruby 3.3 or later); recording disabled` と出て、記録なしで普通に起動します。
|
|
60
62
|
- プラグインマネージャーは `main` ブランチから更新するので、最新のリリースより先の内容になることがあります。
|
|
61
63
|
|
|
64
|
+
### Claude Code で使う場合
|
|
65
|
+
|
|
66
|
+
Claude Code を使っているなら、Claude Code の中で oops プラグインも入れてください。タブで最後に失敗したコマンドを、貼り付けなしで Claude が読めるようになります([Claude Code のプラグイン](#claude-code-のプラグイン)を参照)。
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
/plugin install oops --marketplace ysb3996-blip/oops
|
|
70
|
+
```
|
|
71
|
+
|
|
62
72
|
## シェルの設定
|
|
63
73
|
|
|
64
74
|
rc ファイルに 1 行足します(zsh は `~/.zshrc`、bash は `~/.bashrc`。macOS で bash の設定を `~/.bash_profile` に置いているなら、そちらに書きます)。
|
|
@@ -110,18 +120,17 @@ oops init fish | source
|
|
|
110
120
|
|---|---|
|
|
111
121
|
| `-n N` | 直前の N コマンドをコピーする(`err` と併用すると直近の失敗した N コマンド) |
|
|
112
122
|
| `--github` | GitHub の issue / PR コメント向けの書式にする |
|
|
113
|
-
| `--tail N` | 出力の最後の N 行だけ残す |
|
|
114
|
-
| `--head N` | 出力の最初の N 行だけ残す(`--tail` と併用すると両端を残す) |
|
|
115
123
|
| `--time` | 各コマンドの開始時刻を `# 2026-10-31 19:30:09 +09:00` の行として付ける |
|
|
116
124
|
| `--output-only` | `$ コマンド` の行を省き、出力だけをコピーする |
|
|
117
125
|
| `-p`, `--print` | クリップボードの代わりに標準出力へ書き出す(`copied:` 行は出さない) |
|
|
126
|
+
| `--json` | コマンドを JSON で標準出力へ書き出す(スクリプトやエージェント向け) |
|
|
118
127
|
| `--show-secrets` | 秘密情報をマスクせず、置換テーブルも適用しない |
|
|
119
128
|
|
|
120
129
|
`N` は正の整数で指定します。件数は位置引数の `N` または `-n N` のどちらかで 1 回だけ指定でき、併用や `-n` の繰り返しは使い方の誤りになります。対象のコマンドが指定件数より少なければ、見つかった分をコピーします。`-n` は上記の書式オプションと併用できます。
|
|
121
130
|
|
|
122
131
|
`oops` 自身はコピー対象になりません。直前のコマンドが `oops` だった場合は、その前のコマンドがコピーされます。
|
|
123
132
|
|
|
124
|
-
終了コード: `0` コピーした(または `--print` で出力した)、`1` コピーしなかった(記録していない、まだコマンドがない、失敗したコマンドがない、一時停止中、クリップボードのコマンドが失敗)、`2` 使い方の誤り。
|
|
133
|
+
終了コード: `0` コピーした(または `--print`・`--json` で出力した)、`1` コピーしなかった(記録していない、まだコマンドがない、失敗したコマンドがない、一時停止中、クリップボードのコマンドが失敗)、`2` 使い方の誤り。
|
|
125
134
|
|
|
126
135
|
### 標準の出力
|
|
127
136
|
|
|
@@ -168,10 +177,6 @@ $ bundle exec rspec
|
|
|
168
177
|
</details>
|
|
169
178
|
````
|
|
170
179
|
|
|
171
|
-
### `--tail` と `--head`
|
|
172
|
-
|
|
173
|
-
`oops --tail 50` は出力の最後の 50 行を残し、その前に `... 2,950 lines omitted ...` を入れます。`--head 20` は最初の 20 行を残し、その後ろに同じ印を入れます。両方指定すると、中間が印 1 行に置き換わります。`oops 3` では、行数の制限は各コマンドごとに適用されます。
|
|
174
|
-
|
|
175
180
|
### `--time` と `--output-only`
|
|
176
181
|
|
|
177
182
|
`oops --time` は、各 `$ コマンド` の前にコマンドを始めたローカル時刻を `#` の行として入れ、`--github` の要約にも時刻を足します。`oops --output-only` は `$ コマンド` の行を省いて出力と `(exit N)` だけを残します。画面に出る `copied: $ ...` の行と `--github` の要約には、コマンドの代わりに終了コードと行数だけが出ます。どちらも他のオプションと併用できます。
|
|
@@ -184,10 +189,76 @@ $ bundle exec rspec
|
|
|
184
189
|
```
|
|
185
190
|
````
|
|
186
191
|
|
|
192
|
+
### `--json`
|
|
193
|
+
|
|
194
|
+
`oops --json` は、選んだコマンドを Markdown のブロックとしてコピーする代わりに、古い順の JSON 配列として標準出力へ書き出します。`N`・`err`・`-n`・`--show-secrets` と併用できます。`--github`・`--time`・`--output-only` は Markdown の形を変えるオプションなので、併用すると使い方の誤りになります。コマンド・ディレクトリ・出力は、クリップボードと同じように整えてマスクします。`output` の末尾に改行は付かず、`exit` は終了コード(成功なら `0`)、時刻はローカル時刻の ISO 8601 です(分からないときは `null`)。
|
|
195
|
+
|
|
196
|
+
```json
|
|
197
|
+
[
|
|
198
|
+
{
|
|
199
|
+
"command": "ls /nothing-here",
|
|
200
|
+
"cwd": "~/app",
|
|
201
|
+
"exit": 1,
|
|
202
|
+
"started_at": "2026-11-07T10:19:05+09:00",
|
|
203
|
+
"ended_at": "2026-11-07T10:19:05+09:00",
|
|
204
|
+
"output": "ls: /nothing-here: No such file or directory"
|
|
205
|
+
}
|
|
206
|
+
]
|
|
207
|
+
```
|
|
208
|
+
|
|
187
209
|
### `oops status`
|
|
188
210
|
|
|
189
211
|
`oops status` は `recording`・`paused`・`off`(このシェルが記録されていない、またはログが削除された)のどれかを出力し、記録中のときだけ終了コード `0` を返します。エラーは出さないので、プロンプトやスクリプトから呼べます(例: [starship のカスタムモジュール](https://starship.rs/config/#custom-commands))。呼ぶたびに Ruby が起動するため 0.1〜0.2 秒ほどかかります。`off` のときの詳しい理由は `oops doctor` で分かります。
|
|
190
212
|
|
|
213
|
+
## AI コーディングエージェントと使う
|
|
214
|
+
|
|
215
|
+

|
|
216
|
+
|
|
217
|
+
Claude Code のようにターミナルで動くコーディングエージェントは、起動したタブの環境変数を引き継いでシェルコマンドを実行します。そのため記録中のタブで起動すると、エージェント自身が `oops -p` を実行して、起動する前にそのタブで実行したコマンドと出力を、クリップボードと同じようにマスクした状態で読めます。貼り付けは要らず、エージェントがコマンドを実行し直す必要もありません。
|
|
218
|
+
|
|
219
|
+
```console
|
|
220
|
+
$ bundle exec rake test # 失敗する
|
|
221
|
+
$ claude
|
|
222
|
+
> `oops -p err` を実行して、失敗したテストを直して。
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
Claude Code では `! oops -p err` と打って、自分で出力を会話に入れることもできます。直近 3 つのコマンドなら `-n 3` を付けます。スクリプトやエージェントが終了コードと出力を別々に扱いたいときは [`--json`](#--json) を使えます。ほかのターミナルのエージェントも、シェルコマンドに環境変数を引き継ぐものなら同じように使えます。
|
|
226
|
+
|
|
227
|
+
特に役立つのは、コマンドを実行し直したくないときです。
|
|
228
|
+
|
|
229
|
+
- 時間がかかる(テスト全体、Docker のビルド)、またはたまにしか失敗しない。
|
|
230
|
+
- 何かを変える(`terraform apply`、マイグレーション、デプロイ)。
|
|
231
|
+
- エージェントに渡していない権限が要る(本番の `kubectl`、MFA のセッション、`sudo`)。
|
|
232
|
+
- 出力にトークンや鍵が含まれる。エージェントが見る前にマスクされます(マスクはパターンによるもので、範囲は[設定](#設定)を参照)。
|
|
233
|
+
|
|
234
|
+
エージェントが読めるのは、起動したタブで、起動する前に終わったコマンドだけです。Claude Code で新しく何かを見せたいときは、いったん終了してコマンドを実行し、`claude --continue` で戻ります。
|
|
235
|
+
|
|
236
|
+
### Claude Code のプラグイン
|
|
237
|
+
|
|
238
|
+
Claude Code 用の oops プラグインを入れると `/oops:err` が使えます。そのタブで最後に失敗したコマンドを会話に入れ、後ろに頼みごとを続けられます(`/oops:err 直して`)。打つ機会はあまりありません。直前に失敗したコマンドについて聞くと(「さっきのなんで失敗したの?」)、Claude Code が自分から使います。Claude Code の中で次のように入れます。
|
|
239
|
+
|
|
240
|
+
```
|
|
241
|
+
/plugin install oops --marketplace ysb3996-blip/oops
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
Claude Code 2.1.275 より前では、`/plugin marketplace add ysb3996-blip/oops` の後に `/plugin install oops@oops` を実行します。プラグインを使うには `oops` コマンド自体が要ります([インストール](#インストール)を参照)。
|
|
245
|
+
|
|
246
|
+
プラグインを使わない場合は、次の内容を `~/.claude/commands/oops.md` に保存して `/oops` と打ちます。こちらも同じように Claude Code が自分から使います。
|
|
247
|
+
|
|
248
|
+
```markdown
|
|
249
|
+
---
|
|
250
|
+
description: Read the last failed command in this terminal tab (recorded by oops)
|
|
251
|
+
allowed-tools: Bash(oops:*)
|
|
252
|
+
---
|
|
253
|
+
The last command that failed in this terminal tab, recorded by oops (secrets masked):
|
|
254
|
+
|
|
255
|
+
!`oops -p err`
|
|
256
|
+
|
|
257
|
+
$ARGUMENTS
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
まだ何も失敗していないときや、タブが記録されていないときは、代わりに oops のメッセージが表示されます。
|
|
261
|
+
|
|
191
262
|
## 設定
|
|
192
263
|
|
|
193
264
|
任意の設定ファイルは `${XDG_CONFIG_HOME:-~/.config}/oops/config.yml` です。
|
|
@@ -199,14 +270,14 @@ replace: # コピー時に適用する単純な文字列置換
|
|
|
199
270
|
secrets: # マスクする追加の正規表現
|
|
200
271
|
- "sk_live_[A-Za-z0-9]+"
|
|
201
272
|
clipboard: auto # auto | pbcopy | wl-copy | xclip | xsel | clip.exe | osc52 | 任意のコマンド(標準入力から読む)
|
|
202
|
-
retention_days: 7 #
|
|
273
|
+
retention_days: 7 # この日数より古いコマンドを削除する。false なら残す
|
|
203
274
|
```
|
|
204
275
|
|
|
205
276
|
- 置換とマスクはコピーする瞬間に適用されます。記録済みのログは変更されないため、設定を変えると以前のコマンドにも反映されます。
|
|
206
277
|
- `replace` のキーは長いものから順に、大文字小文字を区別して適用されます。
|
|
207
278
|
- 未知のキーは警告されて無視されます。YAML が壊れている場合も警告が出て、設定なしで続行します。
|
|
208
279
|
- `clipboard: auto` のとき、oops は macOS では `pbcopy`、WSL では `clip.exe`、Wayland では `wl-copy`、X11 では `xclip` か `xsel` を試し、だめなら OSC 52 端末エスケープシーケンスにフォールバックします(SSH 越しでもこれが使われます)。どれを選んだかは `oops doctor` で確認できます。
|
|
209
|
-
- `retention_days`(標準は 7
|
|
280
|
+
- `retention_days`(標準は 7)は、閉じたタブがログに最後に書き込んだ時点から数えます。古いログは、タブが記録を始めるときに削除されます。開いたままのタブも、コマンドが終わったときに、自分のログからこの日数より古いコマンドを落とします(1 時間に 1 回まで)。何も実行していないタブでは、次のコマンドまで残ります。`false` にすると、ログは 50 MB の上限で削除されるまで残ります。
|
|
210
281
|
- 自分で指定した `clipboard:` のコマンドは、独自のプロセスグループで 5 秒のタイムアウト付きで実行されます。そのため端末の読み取りや設定(パスワード入力、`stty` など)はできません。コピー中の Ctrl-C もそのコマンドには届きません。
|
|
211
282
|
|
|
212
283
|
組み込みのマスク対象: AWS アクセスキー、GitHub / GitLab / Slack / npm / OpenAI・Anthropic(`sk-...`)/ Stripe / Google の API トークン、Slack の webhook URL、JWT、`Authorization` ヘッダーの値、秘密鍵ブロック(END 行がなくても)、`key`・`token`・`secret`・`password`・`passwd`・`pwd` を含む名前に代入された値、`--password <値>`・`curl -u user:pass`・`mysql -p<値>`・`docker login -p <値>` で渡されたパスワード、メールアドレス、URL 中の認証情報、公開 IPv4 アドレス(ループバックとプライベート範囲は対象外)。出力だけでなくコマンドラインにも適用されます。
|
|
@@ -220,12 +291,12 @@ retention_days: 7 # 閉じたタブのログをこの日数で削除する。f
|
|
|
220
291
|
1. `oops init` は、rc ファイルから `eval` する(fish では `| source` する)スニペットを出力します。まだ記録されていない端末の対話シェルでは、シェルを `oops record` の下で再起動します。
|
|
221
292
|
2. `oops record` は、Ruby 製の小さな pty 中継です。キー入力とシェルの出力をそのまま通し、ウィンドウのリサイズをシェルに伝えます。
|
|
222
293
|
3. シェルのフック(zsh は `preexec`/`precmd`、bash は `DEBUG` トラップと `PROMPT_COMMAND`、fish は `fish_preexec`/`fish_postexec` イベント)が、各コマンドの前後に目に見えない APC マーカーを出力します。中継がそれを取り除くので、端末には見えません。
|
|
223
|
-
4. コマンドが終わるたびに、中継は 1 行の JSON(コマンド、ディレクトリ、時刻、終了コード、生の出力)をタブごとのログに追記します。置き場所は `${XDG_STATE_HOME:-~/.local/state}/oops/sessions/` です。出力は 1 コマンドあたり 1 MB まで(末尾を残す)。各タブのログは 50 MB までで、超えると新しいコマンドだけを残して書き直されます(約 37 MiB まで縮む)。タブが記録を始めるとき、閉じたタブのログのうち `retention_days`(標準は 7 日)を過ぎたものが削除され、続いて残りが古い順に、全ログの合計が 50 MB
|
|
294
|
+
4. コマンドが終わるたびに、中継は 1 行の JSON(コマンド、ディレクトリ、時刻、終了コード、生の出力)をタブごとのログに追記します。置き場所は `${XDG_STATE_HOME:-~/.local/state}/oops/sessions/` です。出力は 1 コマンドあたり 1 MB まで(末尾を残す)。各タブのログは 50 MB までで、超えると新しいコマンドだけを残して書き直されます(約 37 MiB まで縮む)。タブが記録を始めるとき、閉じたタブのログのうち `retention_days`(標準は 7 日)を過ぎたものが削除され、続いて残りが古い順に、全ログの合計が 50 MB 以下になるまで削除されます。開いているタブのログは削除されないので、忙しいタブが複数あると合計がそれを超えることがあります。ログ名のプロセスが動いていて、しかもログに最後に書き込まれる前に起動していれば、そのタブは開いているとみなします。開いているタブも、コマンドが終わったときに、`retention_days` より古いコマンドを自分のログから落とします(1 時間に 1 回まで)。
|
|
224
295
|
5. `oops` は現在のタブのログの末尾を読み、テキストを整えてマスクし、コードブロックを組み立ててクリップボードに渡します。コマンドを再実行することはありません。
|
|
225
296
|
|
|
226
297
|
## プライバシーとセキュリティ
|
|
227
298
|
|
|
228
|
-
- セッションログには、記録されたすべてのコマンドの生の出力が**マスクされないまま**入っています。マスクと置換テーブルはコピーするときにだけ適用されます。ログは `${XDG_STATE_HOME:-~/.local/state}/oops/sessions/` にあり、あなただけが読めます(ファイルは 0600、ディレクトリは 0700
|
|
299
|
+
- セッションログには、記録されたすべてのコマンドの生の出力が**マスクされないまま**入っています。マスクと置換テーブルはコピーするときにだけ適用されます。ログは `${XDG_STATE_HOME:-~/.local/state}/oops/sessions/` にあり、あなただけが読めます(ファイルは 0600、ディレクトリは 0700)。`retention_days` が標準の 7 なら、閉じたタブのログは最後のコマンドから 7 日たつと(別のタブが記録を始めるときに)削除され、開いているタブはコマンドが終わったときに 7 日より古いコマンドを落とします。閉じたタブのログは `oops clear` ですぐに削除でき、`oops clear --all` なら今のタブのログも空にします。開いている他のタブのログも含めてすべて削除するには `rm -rf "${XDG_STATE_HOME:-$HOME/.local/state}/oops"` を実行します(標準では `rm -rf ~/.local/state/oops`)。ディスクに残したくない秘密情報を表示する前には `oops pause` を使ってください。
|
|
229
300
|
- セキュリティ上の問題を非公開で報告する方法は [SECURITY.ja.md](SECURITY.ja.md) を見てください。公開の issue には書かないでください。
|
|
230
301
|
- 記録される出力は信頼できません。実行したプログラムは、oops のマーカーに見せかけたテキストを出力できます。そのため oops は、`oops pause` / `oops resume` が作るフラグファイルと一致する一時停止・再開マーカーにだけ従います。出力によって黙って記録を止められることはありません。ただし、プログラムがログ上でコマンドの開始と終了の位置を偽装することはできるので、貼り付ける内容は確認してください。
|
|
231
302
|
|
|
@@ -236,10 +307,10 @@ retention_days: 7 # 閉じたタブのログをこの日数で削除する。f
|
|
|
236
307
|
- 使えるのは現在のタブだけです。閉じたタブや以前のタブのものは取り出せません。
|
|
237
308
|
- OSC 52 経由(SSH やクリップボードツールがない場合)では、端末が一度に送れる量を制限します。oops は約 100 KB に切り詰めて警告します。OSC 52 は一方通行のエスケープシーケンスなので、端末がコピーを受け付けたかどうかは分かりません。無視する端末や確認を求める端末もあり、tmux では `set -g set-clipboard on` が必要です。
|
|
238
309
|
- 8192 文字を超えるコマンドは、記録されるコマンドラインが切り詰められます。
|
|
239
|
-
- vim・less・tmux
|
|
310
|
+
- vim・less・tmux などの全画面アプリの出力は保存されず、1 行に置き換わります。コマンドラインが 1 つのコマンドだけで、その名前が英数字と `. _ + - /` だけなら、その行にコマンド名が入ります(ページャーを通した `git log` なら `[git: full-screen output omitted]`)。そうでなければ `[full-screen app output omitted]` です。全画面になる前と後に出た行は残ります。
|
|
240
311
|
- スペースで始まるコマンドラインは、zsh では記録されますが bash では記録されません(bash はそれを履歴に保存せず、oops はコマンドを履歴から読むため)。
|
|
241
312
|
- [bash-preexec](https://github.com/rcaloras/bash-preexec) を使う bash(atuin や Ghostty の bash 統合の背後にあるものも含む): oops は自前の `DEBUG` トラップを使わず、bash-preexec にフックを登録します。rc ファイルでどちらが先でも同じです。bash が履歴に保存しなかった行(`HISTCONTROL=ignorespace`、`HISTIGNORE`)は、この場合 `(not saved in history)` と記録されます。行のテキストを履歴から読むためです。bash-preexec がない場合、oops はそのような行の最初のコマンドを記録します。
|
|
242
|
-
- Ghostty
|
|
313
|
+
- Ghostty のシェル統合(プロンプトマーク、同じディレクトリでの新しいタブ、タイトル)は、外側のシェルにだけ入ります。入っていた場合は、zsh・fish・bash のどれでも、スニペットが記録される側のシェルにもう一度読み込みます。bash 3.2(macOS の `/bin/bash`)には Ghostty が統合を入れられないので、読み込まれません。使いたい場合は、`~/.bashrc` の oops の行の下に次の行を置きます: `[ -n "$GHOSTTY_RESOURCES_DIR" ] && source "$GHOSTTY_RESOURCES_DIR/shell-integration/bash/ghostty.bash"`。
|
|
243
314
|
- bash: パイプラインの次の行では、`PIPESTATUS` に最後の終了ステータスしか入りません(`false | true` の後に `echo ${PIPESTATUS[*]}` を実行すると、`1 0` ではなく `0` と表示されます)。oops の `PROMPT_COMMAND` の項目が先に実行されるためです。`$?` と `$_` は保たれます。パイプラインの各ステータスを表示するプロンプト(starship の `pipestatus`)は、bash では表示が少なくなります。zsh は `pipestatus` を保ちます。
|
|
244
315
|
- macOS: `pbcopy` は文字エンコーディングを `LANG` / `LC_*` から決めます。日本語などの非 ASCII 文字が文字化けして届くときは、UTF-8 のロケール(例: `LANG=en_US.UTF-8`)が設定されているか確認してください。SSH 越しで起動したシェルでは設定されていないことがあります。
|
|
245
316
|
- ネイティブ Windows にはまだ対応していません。
|
|
@@ -282,11 +353,11 @@ bundle exec exe/oops doctor
|
|
|
282
353
|
|
|
283
354
|
```sh
|
|
284
355
|
gem build oops-cli.gemspec
|
|
285
|
-
gem install ./oops-cli-0.
|
|
356
|
+
gem install ./oops-cli-0.5.0.gem # ファイル名は lib/oops/version.rb に合わせる
|
|
286
357
|
rbenv rehash # rbenv を使う場合のみ
|
|
287
358
|
```
|
|
288
359
|
|
|
289
|
-
- gem は Ruby のバージョンごとに入ります。新しいタブが、インストールした Ruby とは別の Ruby を使っていると(たとえばリポジトリは 4.0.9 を指定しているのに、標準の Ruby が 3.3.5 の場合)、シェルの起動時に oops が見つからなくなります(bash では `bash: oops: command not found`、zsh では `command not found: oops` と出ます)。その Ruby でも `gem install` を実行してください(例: `RBENV_VERSION=3.3.5 gem install ./oops-cli-0.
|
|
360
|
+
- gem は Ruby のバージョンごとに入ります。新しいタブが、インストールした Ruby とは別の Ruby を使っていると(たとえばリポジトリは 4.0.9 を指定しているのに、標準の Ruby が 3.3.5 の場合)、シェルの起動時に oops が見つからなくなります(bash では `bash: oops: command not found`、zsh では `command not found: oops` と出ます)。その Ruby でも `gem install` を実行してください(例: `RBENV_VERSION=3.3.5 gem install ./oops-cli-0.5.0.gem`)。
|
|
290
361
|
- 変更のたびに入れ直してください。インストールされた gem はコピーで、チェックアウトへのリンクではありません。
|
|
291
362
|
- 削除するには `gem uninstall oops-cli` です。
|
|
292
363
|
- `eval "$(oops init zsh)"` は、プロンプトで打つだけでなく rc ファイルに書いてください。記録されるシェルは rc ファイルを読み直すため、行がないとフックが入りません(`oops doctor` に `hooks: missing` と出ます)。
|
data/README.md
CHANGED
|
@@ -4,10 +4,12 @@ English | [日本語](README.ja.md)
|
|
|
4
4
|
|
|
5
5
|
[](https://rubygems.org/gems/oops-cli) [](https://github.com/ysb3996-blip/oops/actions/workflows/ci.yml) [](LICENSE) [](https://codecov.io/gh/ysb3996-blip/oops) [](#contributors)
|
|
6
6
|
|
|
7
|
-
Copy the last shell command and its output to the clipboard, ready to paste.
|
|
7
|
+
Copy the last shell command and its output to the clipboard, ready to paste. Or ask Claude Code why it failed: it reads the same record itself.
|
|
8
8
|
|
|
9
9
|
![Demo: a test fails, oops copies the command and its output, a comment line and pbpaste show what is on the clipboard; then cat .env, oops, and the API key is shown as [secret]](docs/demo.gif)
|
|
10
10
|
|
|
11
|
+
> **Using Claude Code?** Install the plugin with `/plugin install oops --marketplace ysb3996-blip/oops`, then ask "why did that fail?". Claude reads the failed command and its output itself, with secrets masked, so you neither paste it nor have Claude run it again. See [With AI coding agents](#with-ai-coding-agents).
|
|
12
|
+
|
|
11
13
|
You ran a command and it failed. You want to show it to a chat assistant, a teammate on Slack, or a GitHub issue. Normally that means dragging to select the output, copying, pasting, and adding the code fences yourself. With oops it is two steps:
|
|
12
14
|
|
|
13
15
|
1. Type `oops` (or `oops err` for the last failed command).
|
|
@@ -59,6 +61,14 @@ source ~/.oops/oops.plugin.zsh
|
|
|
59
61
|
- If the Ruby on `PATH` is too old, the shell prints `oops: ... init zsh failed (oops needs Ruby 3.3 or later); recording disabled` and starts as usual, without recording.
|
|
60
62
|
- Plugin managers update from the `main` branch, which can be ahead of the latest release.
|
|
61
63
|
|
|
64
|
+
### For Claude Code
|
|
65
|
+
|
|
66
|
+
If you use Claude Code, also install the oops plugin inside it, so Claude can read the last command that failed in your tab without you pasting it (see [Claude Code plugin](#claude-code-plugin)):
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
/plugin install oops --marketplace ysb3996-blip/oops
|
|
70
|
+
```
|
|
71
|
+
|
|
62
72
|
## Set up your shell
|
|
63
73
|
|
|
64
74
|
Add one line to your rc file (`~/.zshrc` for zsh, `~/.bashrc` for bash; if you keep your bash setup in `~/.bash_profile` on macOS, put it there):
|
|
@@ -110,18 +120,17 @@ Options for `oops`, `oops N` and `oops err`:
|
|
|
110
120
|
|---|---|
|
|
111
121
|
| `-n N` | Copy the last N commands (with `err`, the last N failed commands) |
|
|
112
122
|
| `--github` | Format for a GitHub issue or PR comment |
|
|
113
|
-
| `--tail N` | Keep only the last N output lines |
|
|
114
|
-
| `--head N` | Keep only the first N output lines (with `--tail`, keeps both ends) |
|
|
115
123
|
| `--time` | Add the time each command started as a `# 2026-10-31 19:30:09 +09:00` line |
|
|
116
124
|
| `--output-only` | Leave out the `$ command` line and copy only the output |
|
|
117
125
|
| `-p`, `--print` | Write the text to stdout instead of the clipboard (no `copied:` line) |
|
|
126
|
+
| `--json` | Write the commands as JSON to stdout, for scripts and agents |
|
|
118
127
|
| `--show-secrets` | Do not mask secrets and do not apply the replace table |
|
|
119
128
|
|
|
120
129
|
`N` must be a positive integer. Specify the command count once, either as a positional `N` or with `-n N`; combining them or repeating `-n` is a usage error. If fewer matching commands are available, oops copies those it finds. `-n` can be combined with the formatting options above.
|
|
121
130
|
|
|
122
131
|
`oops` never copies itself: if the previous command was `oops`, you get the command before it.
|
|
123
132
|
|
|
124
|
-
Exit codes: `0` copied (or printed with `--print`), `1` nothing copied (not recording, no command yet, no failed command, paused, or the clipboard command failed), `2` usage error.
|
|
133
|
+
Exit codes: `0` copied (or printed with `--print` or `--json`), `1` nothing copied (not recording, no command yet, no failed command, paused, or the clipboard command failed), `2` usage error.
|
|
125
134
|
|
|
126
135
|
### Default output
|
|
127
136
|
|
|
@@ -168,10 +177,6 @@ $ bundle exec rspec
|
|
|
168
177
|
</details>
|
|
169
178
|
````
|
|
170
179
|
|
|
171
|
-
### `--tail` and `--head`
|
|
172
|
-
|
|
173
|
-
`oops --tail 50` keeps the last 50 lines of output and puts `... 2,950 lines omitted ...` before them. `--head 20` keeps the first 20 and puts the marker after. With both, the middle is replaced by one marker line. With `oops 3`, the limits apply to each command.
|
|
174
|
-
|
|
175
180
|
### `--time` and `--output-only`
|
|
176
181
|
|
|
177
182
|
`oops --time` puts the local time the command started on a `#` line before each `$ command`, and adds it to the `--github` summary. `oops --output-only` leaves out the `$ command` line and keeps the output and `(exit N)`; the `copied: $ ...` line on screen and the `--github` summary then show the exit code and line count without the command. Both can be combined with the other options:
|
|
@@ -184,10 +189,76 @@ $ bundle exec rspec
|
|
|
184
189
|
```
|
|
185
190
|
````
|
|
186
191
|
|
|
192
|
+
### `--json`
|
|
193
|
+
|
|
194
|
+
`oops --json` writes the selected commands to stdout as a JSON array, oldest first, instead of copying a Markdown block. It works with `N`, `err`, `-n` and `--show-secrets`; `--github`, `--time` and `--output-only` shape the Markdown and are a usage error with it. Command, directory and output are cleaned and masked as on the clipboard. `output` has no trailing newline, `exit` is the exit code (`0` on success), and the times are ISO 8601 in local time (`null` when unknown):
|
|
195
|
+
|
|
196
|
+
```json
|
|
197
|
+
[
|
|
198
|
+
{
|
|
199
|
+
"command": "ls /nothing-here",
|
|
200
|
+
"cwd": "~/app",
|
|
201
|
+
"exit": 1,
|
|
202
|
+
"started_at": "2026-11-07T10:19:05+09:00",
|
|
203
|
+
"ended_at": "2026-11-07T10:19:05+09:00",
|
|
204
|
+
"output": "ls: /nothing-here: No such file or directory"
|
|
205
|
+
}
|
|
206
|
+
]
|
|
207
|
+
```
|
|
208
|
+
|
|
187
209
|
### `oops status`
|
|
188
210
|
|
|
189
211
|
`oops status` prints `recording`, `paused` or `off` (this shell is not recorded, or its log was deleted) and exits `0` only when recording. It prints no error, so a prompt or script can call it, for example with a [starship custom module](https://starship.rs/config/#custom-commands). Each call starts Ruby, which takes roughly 0.1 to 0.2 seconds; `oops doctor` tells more when the answer is `off`.
|
|
190
212
|
|
|
213
|
+
## With AI coding agents
|
|
214
|
+
|
|
215
|
+

|
|
216
|
+
|
|
217
|
+
A coding agent that runs in your terminal, such as Claude Code, starts its shell commands with the environment of the tab you started it in. In a recorded tab it can therefore run `oops -p` itself and read the commands you ran there before starting it, with their output, masked as on the clipboard. You do not paste anything, and the agent does not run the command again.
|
|
218
|
+
|
|
219
|
+
```console
|
|
220
|
+
$ bundle exec rake test # fails
|
|
221
|
+
$ claude
|
|
222
|
+
> Run `oops -p err` and fix the failing test.
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
In Claude Code you can also type `! oops -p err` to put the output into the conversation yourself. Add `-n 3` for the last 3 commands, or use [`--json`](#--json) when a script or agent wants the exit code and output as separate fields. Other terminal agents work the same way when they pass your environment to their shell commands.
|
|
226
|
+
|
|
227
|
+
This helps most when running the command again is a bad idea:
|
|
228
|
+
|
|
229
|
+
- It is slow (a full test suite, a Docker build) or fails only sometimes.
|
|
230
|
+
- It changes things (`terraform apply`, a migration, a deploy).
|
|
231
|
+
- It needs access you do not give the agent (production `kubectl`, an MFA session, `sudo`).
|
|
232
|
+
- Its output holds tokens or keys: they are masked before the agent sees them (masking is pattern based, see [Configuration](#configuration) for what it covers).
|
|
233
|
+
|
|
234
|
+
The agent reads only the tab it was started in, and only what finished before it started. To show it something new in Claude Code, quit, run the command, and come back with `claude --continue`.
|
|
235
|
+
|
|
236
|
+
### Claude Code plugin
|
|
237
|
+
|
|
238
|
+
The oops plugin for Claude Code adds `/oops:err`, which puts the last failed command of the tab into the conversation, optionally followed by a request (`/oops:err fix it`). You rarely need to type it: when you ask about a command that just failed ("why did that fail?"), Claude Code uses it on its own. Install it inside Claude Code:
|
|
239
|
+
|
|
240
|
+
```
|
|
241
|
+
/plugin install oops --marketplace ysb3996-blip/oops
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
On Claude Code before 2.1.275, run `/plugin marketplace add ysb3996-blip/oops` and then `/plugin install oops@oops`. The plugin needs the `oops` command itself (see [Install](#install)).
|
|
245
|
+
|
|
246
|
+
Without the plugin, save this as `~/.claude/commands/oops.md` and type `/oops` instead; Claude Code uses it on its own in the same way:
|
|
247
|
+
|
|
248
|
+
```markdown
|
|
249
|
+
---
|
|
250
|
+
description: Read the last failed command in this terminal tab (recorded by oops)
|
|
251
|
+
allowed-tools: Bash(oops:*)
|
|
252
|
+
---
|
|
253
|
+
The last command that failed in this terminal tab, recorded by oops (secrets masked):
|
|
254
|
+
|
|
255
|
+
!`oops -p err`
|
|
256
|
+
|
|
257
|
+
$ARGUMENTS
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
When nothing has failed yet, or the tab is not recorded, Claude Code shows oops's message instead.
|
|
261
|
+
|
|
191
262
|
## Configuration
|
|
192
263
|
|
|
193
264
|
Optional file at `${XDG_CONFIG_HOME:-~/.config}/oops/config.yml`:
|
|
@@ -199,14 +270,14 @@ replace: # plain string replacements applied when copying
|
|
|
199
270
|
secrets: # extra regular expressions to mask
|
|
200
271
|
- "sk_live_[A-Za-z0-9]+"
|
|
201
272
|
clipboard: auto # auto | pbcopy | wl-copy | xclip | xsel | clip.exe | osc52 | any command (reads stdin)
|
|
202
|
-
retention_days: 7 # delete
|
|
273
|
+
retention_days: 7 # delete commands older than this many days; false keeps them
|
|
203
274
|
```
|
|
204
275
|
|
|
205
276
|
- Replacements and masking are applied at the moment you copy; the recorded log stays untouched, so a config change also affects earlier commands.
|
|
206
277
|
- `replace` keys are applied longest first and are case-sensitive.
|
|
207
278
|
- Unknown keys produce a warning and are ignored. A broken YAML file produces a warning and oops carries on without a config.
|
|
208
279
|
- With `clipboard: auto`, oops tries `pbcopy` on macOS, `clip.exe` on WSL, `wl-copy` on Wayland, `xclip` or `xsel` on X11, and falls back to the OSC 52 terminal escape sequence (also used over SSH). `oops doctor` shows which one it picked.
|
|
209
|
-
- `retention_days` (default 7) counts from the last time a closed tab wrote its log. Old logs are deleted when a tab starts recording. With `false`,
|
|
280
|
+
- `retention_days` (default 7) counts from the last time a closed tab wrote its log. Old logs are deleted when a tab starts recording. A tab that stays open also drops commands older than that from its own log when a command finishes (at most once an hour), so a tab that runs nothing keeps them until its next command. With `false`, logs stay until the 50 MB limit removes them.
|
|
210
281
|
- A custom `clipboard:` command runs in its own process group with a 5 second timeout, so it must not read from or configure the terminal (no password prompts, no `stty`); Ctrl-C during a copy does not reach it.
|
|
211
282
|
|
|
212
283
|
Built-in masking covers AWS access keys, GitHub, GitLab, Slack, npm, OpenAI/Anthropic (`sk-...`), Stripe and Google API tokens, Slack webhook URLs, JWTs, `Authorization` header values, private key blocks (also when the END line is missing), values assigned to names containing `key`, `token`, `secret`, `password`, `passwd` or `pwd`, passwords given as `--password <value>`, `curl -u user:pass`, `mysql -p<value>` or `docker login -p <value>`, email addresses, credentials inside URLs, and public IPv4 addresses (loopback and private ranges are left alone). It is applied to the command line as well as the output.
|
|
@@ -220,12 +291,12 @@ Masking is pattern based, so it is a safety net, not a guarantee: a secret in a
|
|
|
220
291
|
1. `oops init` prints a snippet that you `eval` from your rc file (`| source` in fish). In an interactive shell on a terminal that is not recorded yet, it restarts the shell under `oops record`.
|
|
221
292
|
2. `oops record` is a small Ruby pty relay: it passes your keystrokes and the shell's output through untouched and forwards window resizes to the shell.
|
|
222
293
|
3. Hooks in the shell (zsh `preexec`/`precmd`, bash `DEBUG` trap and `PROMPT_COMMAND`, fish `fish_preexec`/`fish_postexec` events) emit invisible APC markers around each command. The relay strips them, so your terminal never sees them.
|
|
223
|
-
4. For each finished command the relay appends one JSON line (command, directory, times, exit code, raw output) to a per-tab log under `${XDG_STATE_HOME:-~/.local/state}/oops/sessions/`. Output is capped at 1 MB per command (the tail is kept). Each tab's log is capped at 50 MB: when it passes that, it is rewritten to keep only its newest commands (down to about 37 MiB). When a tab starts recording, logs of closed tabs older than `retention_days` (7 by default) are deleted, and then the oldest of the rest until all logs together take at most 50 MB; logs of open tabs are never deleted, so several busy tabs can together use more.
|
|
294
|
+
4. For each finished command the relay appends one JSON line (command, directory, times, exit code, raw output) to a per-tab log under `${XDG_STATE_HOME:-~/.local/state}/oops/sessions/`. Output is capped at 1 MB per command (the tail is kept). Each tab's log is capped at 50 MB: when it passes that, it is rewritten to keep only its newest commands (down to about 37 MiB). When a tab starts recording, logs of closed tabs older than `retention_days` (7 by default) are deleted, and then the oldest of the rest until all logs together take at most 50 MB; logs of open tabs are never deleted, so several busy tabs can together use more. A tab counts as open while the process in its log name is running and started before the log was last written. An open tab also drops commands older than `retention_days` from its own log when a command finishes, at most once an hour.
|
|
224
295
|
5. `oops` reads the end of the current tab's log, cleans and masks the text, builds the code block and hands it to the clipboard. Nothing is ever re-run.
|
|
225
296
|
|
|
226
297
|
## Privacy and security
|
|
227
298
|
|
|
228
|
-
- The session logs hold the raw, **unmasked** output of every recorded command. Masking and the replace table are applied only when you copy. The logs live under `${XDG_STATE_HOME:-~/.local/state}/oops/sessions/`, readable only by you (mode 0600, directory 0700).
|
|
299
|
+
- The session logs hold the raw, **unmasked** output of every recorded command. Masking and the replace table are applied only when you copy. The logs live under `${XDG_STATE_HOME:-~/.local/state}/oops/sessions/`, readable only by you (mode 0600, directory 0700). With the default `retention_days` of 7, a closed tab's log is deleted once 7 days have passed since its last command (when another tab starts recording), and an open tab drops commands older than 7 days when it finishes a command. `oops clear` deletes the logs of closed tabs now, and `oops clear --all` also empties the current tab's log. To delete everything, including the logs of other open tabs, run `rm -rf "${XDG_STATE_HOME:-$HOME/.local/state}/oops"` (by default `rm -rf ~/.local/state/oops`). Use `oops pause` before showing secrets you do not want on disk.
|
|
229
300
|
- To report a security problem privately, see [SECURITY.md](SECURITY.md). Please do not open a public issue for it.
|
|
230
301
|
- Recorded output is untrusted: any program you run can print text that looks like oops's markers. oops therefore obeys a pause or resume marker only when it agrees with the flag file that `oops pause` and `oops resume` set, so output cannot silently pause recording. A program can still fake where a command starts and ends in the log, so check what you paste.
|
|
231
302
|
|
|
@@ -236,10 +307,10 @@ Masking is pattern based, so it is a safety net, not a guarantee: a secret in a
|
|
|
236
307
|
- Only the current tab is available. A tab you closed, or an earlier tab, cannot be retrieved.
|
|
237
308
|
- Via OSC 52 (SSH, or no clipboard tool), terminals limit how much can be sent at once. oops truncates to roughly 100 KB and warns. OSC 52 is a one-way escape sequence, so oops cannot tell whether the terminal accepted the copy; some terminals ignore it or ask first, and tmux needs `set -g set-clipboard on`.
|
|
238
309
|
- Commands longer than 8192 characters are truncated in the recorded command line.
|
|
239
|
-
- Output of full-screen apps (vim, less, tmux and so on) is not kept; it is replaced by `[full-screen app output omitted]`.
|
|
310
|
+
- Output of full-screen apps (vim, less, tmux and so on) is not kept; it is replaced by one line. When the command line holds a single command with a plain name (letters, digits and `. _ + - /`), that line names it, such as `[git: full-screen output omitted]` for `git log` through a pager; otherwise it reads `[full-screen app output omitted]`. Lines printed before and after the full-screen part are kept.
|
|
240
311
|
- A command line that starts with a space is recorded in zsh but dropped in bash (bash does not save it to history, which is where oops reads the command).
|
|
241
312
|
- bash with [bash-preexec](https://github.com/rcaloras/bash-preexec) (also behind atuin and Ghostty's bash integration): oops registers its hooks with it instead of using its own `DEBUG` trap, whichever of the two comes first in your rc file. A line that bash did not save to history (`HISTCONTROL=ignorespace`, `HISTIGNORE`) is then recorded as `(not saved in history)`, because the text of the line is read from history there. Without bash-preexec, oops records the first command of such a line instead.
|
|
242
|
-
-
|
|
313
|
+
- Ghostty adds its shell integration (prompt marks, new tabs in the same directory, titles) only to the outer shell. When it did, the snippet loads it again in the recorded shell, for zsh, fish and bash. Ghostty cannot add it to bash 3.2 (macOS's `/bin/bash`), so there it is not loaded unless you load it yourself below the oops line in `~/.bashrc`: `[ -n "$GHOSTTY_RESOURCES_DIR" ] && source "$GHOSTTY_RESOURCES_DIR/shell-integration/bash/ghostty.bash"`.
|
|
243
314
|
- bash: the line after a pipeline sees `PIPESTATUS` with only the last status (`false | true`, then `echo ${PIPESTATUS[*]}` prints `0`, not `1 0`), because oops's `PROMPT_COMMAND` entry runs first. `$?` and `$_` are kept. A prompt that shows pipeline statuses (starship's `pipestatus`) shows less in bash. zsh keeps `pipestatus`.
|
|
244
315
|
- macOS: `pbcopy` chooses its text encoding from `LANG` / `LC_*`. If Japanese or other non-ASCII text arrives garbled, check that a UTF-8 locale is set (for example `LANG=en_US.UTF-8`), which can be missing in shells started over SSH.
|
|
245
316
|
- Native Windows is not supported yet.
|
|
@@ -282,11 +353,11 @@ To try it as your shell does, build and install the gem into the Ruby you are us
|
|
|
282
353
|
|
|
283
354
|
```sh
|
|
284
355
|
gem build oops-cli.gemspec
|
|
285
|
-
gem install ./oops-cli-0.
|
|
356
|
+
gem install ./oops-cli-0.5.0.gem # the file name follows lib/oops/version.rb
|
|
286
357
|
rbenv rehash # only with rbenv
|
|
287
358
|
```
|
|
288
359
|
|
|
289
|
-
- A gem is installed per Ruby version. If a new tab uses another Ruby than the one you installed into (for example the repository pins 4.0.9, but your global Ruby is 3.3.5), the shell cannot find oops when it starts (bash prints `bash: oops: command not found`, zsh prints `command not found: oops`). Run `gem install` again under that Ruby, e.g. `RBENV_VERSION=3.3.5 gem install ./oops-cli-0.
|
|
360
|
+
- A gem is installed per Ruby version. If a new tab uses another Ruby than the one you installed into (for example the repository pins 4.0.9, but your global Ruby is 3.3.5), the shell cannot find oops when it starts (bash prints `bash: oops: command not found`, zsh prints `command not found: oops`). Run `gem install` again under that Ruby, e.g. `RBENV_VERSION=3.3.5 gem install ./oops-cli-0.5.0.gem`.
|
|
290
361
|
- Reinstall after every change. The installed gem is a copy, not a link to your checkout.
|
|
291
362
|
- To remove it: `gem uninstall oops-cli`.
|
|
292
363
|
- Put `eval "$(oops init zsh)"` in your rc file, not just at the prompt: the recorded shell reads the rc file again, and without the line it has no hooks (`oops doctor` shows `hooks: missing`).
|
data/lib/oops/cli.rb
CHANGED
|
@@ -25,8 +25,8 @@ module Oops
|
|
|
25
25
|
when "clear" then return clear(argv.drop(1), env: env, stdout: stdout)
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
-
options = {
|
|
29
|
-
|
|
28
|
+
options = { github: false, show_secrets: false, time: false, output_only: false, print: false,
|
|
29
|
+
json: false }
|
|
30
30
|
count = nil
|
|
31
31
|
parser = OptionParser.new do |o|
|
|
32
32
|
o.banner = "Usage: oops [N | err] [-n N] [options]\n " \
|
|
@@ -37,12 +37,11 @@ module Oops
|
|
|
37
37
|
count = positive(v, "-n")
|
|
38
38
|
end
|
|
39
39
|
o.on("--github", "Format for a GitHub issue or PR comment") { options[:github] = true }
|
|
40
|
-
o.on("--tail N", "Keep only the last N output lines") { |v| options[:tail] = positive(v, "--tail") }
|
|
41
|
-
o.on("--head N", "Keep only the first N output lines") { |v| options[:head] = positive(v, "--head") }
|
|
42
40
|
o.on("--time", "Add the time each command started") { options[:time] = true }
|
|
43
41
|
o.on("--output-only", "Leave out the command line, keep the output") { options[:output_only] = true }
|
|
44
42
|
o.on("--show-secrets", "Do not mask secrets or apply the replace table") { options[:show_secrets] = true }
|
|
45
43
|
o.on("-p", "--print", "Write the text to stdout instead of the clipboard") { options[:print] = true }
|
|
44
|
+
o.on("--json", "Write the commands as JSON to stdout") { options[:json] = true }
|
|
46
45
|
o.on("-v", "--version", "Print version and exit") do
|
|
47
46
|
stdout.puts "oops #{VERSION}"
|
|
48
47
|
return 0
|
|
@@ -52,7 +51,9 @@ module Oops
|
|
|
52
51
|
return 0
|
|
53
52
|
end
|
|
54
53
|
end
|
|
55
|
-
|
|
54
|
+
copy_target = target(parser.parse(argv), count: count)
|
|
55
|
+
check_json(options)
|
|
56
|
+
copy(copy_target, options, env: env, stdout: stdout, stderr: stderr)
|
|
56
57
|
rescue OptionParser::ParseError, UsageError => e
|
|
57
58
|
stderr.puts "oops: #{e.message}"
|
|
58
59
|
2
|
|
@@ -67,6 +68,15 @@ module Oops
|
|
|
67
68
|
value.to_i
|
|
68
69
|
end
|
|
69
70
|
|
|
71
|
+
# --json has its own shape, so the Markdown formatting options make no
|
|
72
|
+
# sense with it.
|
|
73
|
+
def self.check_json(options)
|
|
74
|
+
return unless options[:json]
|
|
75
|
+
|
|
76
|
+
conflict = %i[github time output_only].find { |key| options[key] }
|
|
77
|
+
raise UsageError, "--json cannot be combined with --#{conflict.to_s.tr('_', '-')}" if conflict
|
|
78
|
+
end
|
|
79
|
+
|
|
70
80
|
# Count comes from either a positional N or -n N. "err" filters the
|
|
71
81
|
# selection to failed commands; without a count, one command is copied.
|
|
72
82
|
def self.target(args, count:)
|
|
@@ -91,8 +101,13 @@ module Oops
|
|
|
91
101
|
raise Failure, (target.failed_only ? "no failed command found." : "no command found yet.") if records.empty?
|
|
92
102
|
|
|
93
103
|
home = env["HOME"].to_s.empty? ? Dir.home : env["HOME"]
|
|
94
|
-
|
|
95
|
-
|
|
104
|
+
formatter = Formatter.new(config: config, home: home, **options.except(:print, :json))
|
|
105
|
+
if options[:json]
|
|
106
|
+
stdout.write formatter.json(records)
|
|
107
|
+
return 0
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
result = formatter.format(records)
|
|
96
111
|
if options[:print]
|
|
97
112
|
stdout.write result.text
|
|
98
113
|
return 0
|
|
@@ -244,7 +259,7 @@ module Oops
|
|
|
244
259
|
0
|
|
245
260
|
end
|
|
246
261
|
|
|
247
|
-
private_class_method :positive, :target, :copy, :report_command, :report_counts, :recording_log,
|
|
248
|
-
:write_marker, :status, :clear, :doctor
|
|
262
|
+
private_class_method :positive, :check_json, :target, :copy, :report_command, :report_counts, :recording_log,
|
|
263
|
+
:toggle, :write_marker, :status, :clear, :doctor
|
|
249
264
|
end
|
|
250
265
|
end
|
data/lib/oops/formatter.rb
CHANGED
|
@@ -1,29 +1,30 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
1
3
|
module Oops
|
|
2
|
-
# Turns captured records into the paste-ready clipboard text
|
|
4
|
+
# Turns captured records into the paste-ready clipboard text, or into JSON
|
|
5
|
+
# for scripts and agents (#json).
|
|
3
6
|
#
|
|
4
7
|
# Per field (command line and output) the pipeline is: strip ANSI, scrub
|
|
5
8
|
# invalid UTF-8, shorten the home directory to "~", mask secrets, apply the
|
|
6
|
-
# config replace table. Output is then
|
|
9
|
+
# config replace table. Output is then fenced.
|
|
7
10
|
# With show_secrets, the home/mask/replace steps are skipped. With time, a
|
|
8
11
|
# "# <local start time>" line precedes each command; with output_only, the
|
|
9
12
|
# "$ command" line is left out.
|
|
10
13
|
class Formatter
|
|
11
14
|
GITHUB_COLLAPSE_LINES = 30
|
|
12
15
|
|
|
13
|
-
# lines: output lines
|
|
14
|
-
# "... N lines omitted ..." marker is not counted.
|
|
16
|
+
# lines: output lines after formatting, summed over records.
|
|
15
17
|
# command_line: the formatted first command, without "$ " or indentation.
|
|
16
18
|
Result = Struct.new(:text, :lines, :masked_count, :command_line)
|
|
17
19
|
|
|
18
20
|
TIME_FORMAT = "%Y-%m-%d %H:%M:%S %:z".freeze
|
|
21
|
+
ISO_TIME_FORMAT = "%Y-%m-%dT%H:%M:%S%:z".freeze
|
|
19
22
|
|
|
20
|
-
def initialize(config:, home: Dir.home, show_secrets: false,
|
|
21
|
-
|
|
23
|
+
def initialize(config:, home: Dir.home, show_secrets: false, github: false, time: false,
|
|
24
|
+
output_only: false)
|
|
22
25
|
@config = config
|
|
23
26
|
@home = home.to_s
|
|
24
27
|
@show_secrets = show_secrets
|
|
25
|
-
@head = head
|
|
26
|
-
@tail = tail
|
|
27
28
|
@github = github
|
|
28
29
|
@time = time
|
|
29
30
|
@output_only = output_only
|
|
@@ -37,11 +38,21 @@ module Oops
|
|
|
37
38
|
Result.new(text, blocks.sum { |b| b[:count] }, @masked, blocks.first&.dig(:command))
|
|
38
39
|
end
|
|
39
40
|
|
|
41
|
+
# The records as a pretty-printed JSON array, oldest first. Command, cwd
|
|
42
|
+
# and output go through the same cleaning as the Markdown text; the
|
|
43
|
+
# formatting options (github, time, output_only) do not apply. Times are
|
|
44
|
+
# ISO 8601 in local time, or null when the record has none.
|
|
45
|
+
def json(records)
|
|
46
|
+
@masked = 0
|
|
47
|
+
items = records.map { |record| json_item(record) }
|
|
48
|
+
"#{JSON.pretty_generate(items)}\n"
|
|
49
|
+
end
|
|
50
|
+
|
|
40
51
|
private
|
|
41
52
|
|
|
42
53
|
def build_block(record)
|
|
43
54
|
command = clean(record.cmd.to_s).sub(/\s+\z/, "")
|
|
44
|
-
shown
|
|
55
|
+
shown = output_lines(clean(PromptSp.strip(record.output.to_s)))
|
|
45
56
|
exit_code = record.exit
|
|
46
57
|
exit_code = nil if exit_code.to_i.zero?
|
|
47
58
|
stamp = start_time(record)
|
|
@@ -50,7 +61,7 @@ module Oops
|
|
|
50
61
|
body << "$ #{command.gsub("\n", "\n ").gsub(/^ +$/, '')}" unless @output_only
|
|
51
62
|
body.concat(shown)
|
|
52
63
|
body << "(exit #{exit_code})" if exit_code
|
|
53
|
-
{ command: command, exit: exit_code, count:
|
|
64
|
+
{ command: command, exit: exit_code, count: shown.size, time: stamp, content: body.join("\n") }
|
|
54
65
|
end
|
|
55
66
|
|
|
56
67
|
# Local time the command started, or nil without --time or for a record
|
|
@@ -61,25 +72,24 @@ module Oops
|
|
|
61
72
|
Time.at(record.started_at.to_i).strftime(TIME_FORMAT)
|
|
62
73
|
end
|
|
63
74
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
75
|
+
def json_item(record)
|
|
76
|
+
{ command: clean(record.cmd.to_s).sub(/\s+\z/, ""),
|
|
77
|
+
cwd: record.cwd && clean(record.cwd.to_s),
|
|
78
|
+
exit: record.exit,
|
|
79
|
+
started_at: iso_time(record.started_at),
|
|
80
|
+
ended_at: iso_time(record.ended_at),
|
|
81
|
+
output: output_lines(clean(PromptSp.strip(record.output.to_s))).join("\n") }
|
|
71
82
|
end
|
|
72
83
|
|
|
73
|
-
def
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
head = @head.to_i
|
|
77
|
-
tail = @tail.to_i
|
|
78
|
-
omitted = lines.size - head - tail
|
|
79
|
-
return lines if omitted <= 0
|
|
84
|
+
def iso_time(seconds)
|
|
85
|
+
seconds && Time.at(seconds.to_i).strftime(ISO_TIME_FORMAT)
|
|
86
|
+
end
|
|
80
87
|
|
|
81
|
-
|
|
82
|
-
|
|
88
|
+
# Output lines without the trailing blank ones.
|
|
89
|
+
def output_lines(text)
|
|
90
|
+
lines = text.split("\n", -1)
|
|
91
|
+
lines.pop while lines.any? && lines.last.strip.empty?
|
|
92
|
+
lines
|
|
83
93
|
end
|
|
84
94
|
|
|
85
95
|
def clean(raw)
|
data/lib/oops/recorder.rb
CHANGED
|
@@ -148,10 +148,11 @@ module Oops
|
|
|
148
148
|
dir = Paths.sessions_dir(env: @env)
|
|
149
149
|
@session_path = SessionLog.new_path(dir)
|
|
150
150
|
Paths.ensure_sessions_dir!(env: @env)
|
|
151
|
-
|
|
151
|
+
max_age = retention_seconds
|
|
152
|
+
rotate_logs(dir, max_age)
|
|
152
153
|
File.open(@session_path, File::WRONLY | File::CREAT | File::APPEND, 0o600, &:itself)
|
|
153
154
|
write_status("ok")
|
|
154
|
-
SessionLog.new(@session_path)
|
|
155
|
+
SessionLog.new(@session_path, max_age: max_age)
|
|
155
156
|
rescue StandardError => e
|
|
156
157
|
@log_failed = true
|
|
157
158
|
warn_line "oops: warning: session log unavailable (#{e.message}); recording disabled"
|
|
@@ -170,10 +171,14 @@ module Oops
|
|
|
170
171
|
end
|
|
171
172
|
end
|
|
172
173
|
|
|
173
|
-
|
|
174
|
-
def rotate_logs(dir)
|
|
174
|
+
def retention_seconds
|
|
175
175
|
days = Config.load(env: @env).retention_days
|
|
176
|
-
|
|
176
|
+
days && (days * 86_400)
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
# Rotation only frees space; failing to rotate must not stop recording.
|
|
180
|
+
def rotate_logs(dir, max_age)
|
|
181
|
+
SessionLog.rotate!(dir, max_age: max_age)
|
|
177
182
|
rescue SystemCallError
|
|
178
183
|
nil
|
|
179
184
|
end
|
data/lib/oops/session_log.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
require "time"
|
|
2
|
+
|
|
1
3
|
module Oops
|
|
2
4
|
# Append-only JSONL log of captured commands for one shell session. A log
|
|
3
5
|
# that grows past its cap is rewritten to keep only its newest records.
|
|
@@ -8,6 +10,11 @@ module Oops
|
|
|
8
10
|
# tab is not rewritten on every command.
|
|
9
11
|
TRIM_TO = 0.75
|
|
10
12
|
LOG_NAME = /\A(\d{8}-\d{6})-(\d+)\.jsonl\z/
|
|
13
|
+
# `ps` rounds a start time down to the second and file systems may round
|
|
14
|
+
# mtimes; the slack keeps such rounding from closing a live tab.
|
|
15
|
+
START_SLACK = 5
|
|
16
|
+
# How often an open tab checks its log for records older than max_age.
|
|
17
|
+
AGE_CHECK_INTERVAL = 3600
|
|
11
18
|
|
|
12
19
|
attr_reader :path
|
|
13
20
|
|
|
@@ -21,19 +28,43 @@ module Oops
|
|
|
21
28
|
true
|
|
22
29
|
rescue Errno::EPERM
|
|
23
30
|
true
|
|
24
|
-
rescue SystemCallError
|
|
31
|
+
rescue SystemCallError, RangeError
|
|
25
32
|
false
|
|
26
33
|
end
|
|
27
34
|
|
|
35
|
+
# Whether the tab whose log was last written at written_at is still open.
|
|
36
|
+
# Process ids are reused, and the recorder starts before it ever writes its
|
|
37
|
+
# log, so a live process that started after the last write is someone
|
|
38
|
+
# else and the log is closed. The mtime is used rather than the time in
|
|
39
|
+
# the log name, which is local time without a zone. When the start time
|
|
40
|
+
# cannot be read, the log is kept.
|
|
41
|
+
def self.session_alive?(pid, written_at, alive: method(:process_alive?),
|
|
42
|
+
started_at: method(:process_started_at))
|
|
43
|
+
return false unless alive.call(pid)
|
|
44
|
+
|
|
45
|
+
started = started_at.call(pid)
|
|
46
|
+
started.nil? || started <= written_at + START_SLACK
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# `ps -o lstart` prints the same fixed format on macOS and Linux in the C
|
|
50
|
+
# locale, e.g. "Wed Nov 4 22:33:04 2026". Nil when it cannot be read.
|
|
51
|
+
def self.process_started_at(pid)
|
|
52
|
+
out = IO.popen({ "LC_ALL" => "C" }, ["ps", "-o", "lstart=", "-p", pid.to_s],
|
|
53
|
+
err: File::NULL, &:read)
|
|
54
|
+
Time.strptime(out.split.join(" "), "%a %b %d %H:%M:%S %Y")
|
|
55
|
+
rescue SystemCallError, ArgumentError
|
|
56
|
+
nil
|
|
57
|
+
end
|
|
58
|
+
|
|
28
59
|
# Deletes logs of finished sessions: with max_age, those last written more
|
|
29
60
|
# than max_age seconds ago; then the oldest, until the total size of all
|
|
30
61
|
# session logs is at most max_bytes. Logs of live sessions are kept.
|
|
31
62
|
def self.rotate!(dir, max_bytes: DEFAULT_MAX_BYTES, max_age: nil, now: Time.now,
|
|
32
|
-
alive: method(:
|
|
63
|
+
alive: method(:session_alive?))
|
|
33
64
|
logs = list(dir)
|
|
34
65
|
if max_age
|
|
35
66
|
logs = logs.reject do |log|
|
|
36
|
-
next false if log[:mtime] >= now - max_age || alive.call(log[:pid])
|
|
67
|
+
next false if log[:mtime] >= now - max_age || alive.call(log[:pid], log[:mtime])
|
|
37
68
|
|
|
38
69
|
remove_log(File.join(dir, log[:name]))
|
|
39
70
|
true
|
|
@@ -42,7 +73,7 @@ module Oops
|
|
|
42
73
|
total = logs.sum { |l| l[:size] }
|
|
43
74
|
logs.sort_by { |l| l[:name] }.each do |log|
|
|
44
75
|
break if total <= max_bytes
|
|
45
|
-
next if alive.call(log[:pid])
|
|
76
|
+
next if alive.call(log[:pid], log[:mtime])
|
|
46
77
|
|
|
47
78
|
remove_log(File.join(dir, log[:name]))
|
|
48
79
|
total -= log[:size]
|
|
@@ -52,8 +83,8 @@ module Oops
|
|
|
52
83
|
end
|
|
53
84
|
|
|
54
85
|
# Deletes the logs of all finished sessions. Returns how many it deleted.
|
|
55
|
-
def self.clear!(dir, alive: method(:
|
|
56
|
-
dead = list(dir).reject { |log| alive.call(log[:pid]) }
|
|
86
|
+
def self.clear!(dir, alive: method(:session_alive?))
|
|
87
|
+
dead = list(dir).reject { |log| alive.call(log[:pid], log[:mtime]) }
|
|
57
88
|
dead.each { |log| remove_log(File.join(dir, log[:name])) }
|
|
58
89
|
dead.size
|
|
59
90
|
rescue Errno::ENOENT
|
|
@@ -83,20 +114,28 @@ module Oops
|
|
|
83
114
|
end
|
|
84
115
|
private_class_method :list, :remove_log
|
|
85
116
|
|
|
86
|
-
|
|
117
|
+
# With max_age (seconds), records that ended longer ago than that are
|
|
118
|
+
# dropped from the log as new ones are appended.
|
|
119
|
+
def initialize(path, max_bytes: DEFAULT_MAX_BYTES, max_age: nil)
|
|
87
120
|
@path = path
|
|
88
121
|
@max_bytes = max_bytes
|
|
122
|
+
@max_age = max_age
|
|
123
|
+
@next_age_check = nil
|
|
89
124
|
end
|
|
90
125
|
|
|
91
|
-
# Raises if the record cannot be written. Trimming
|
|
92
|
-
# best effort and never raises.
|
|
93
|
-
def append(record)
|
|
126
|
+
# Raises if the record cannot be written. Trimming the log, by size or by
|
|
127
|
+
# age, is best effort and never raises.
|
|
128
|
+
def append(record, now: Time.now)
|
|
94
129
|
line = record.to_json_line
|
|
95
130
|
size = File.open(@path, File::APPEND | File::CREAT | File::WRONLY, 0o600) do |f|
|
|
96
131
|
f.syswrite(line)
|
|
97
132
|
f.size
|
|
98
133
|
end
|
|
99
|
-
|
|
134
|
+
if size > @max_bytes
|
|
135
|
+
trim
|
|
136
|
+
elsif age_check_due?(now)
|
|
137
|
+
expire((now - @max_age).to_i)
|
|
138
|
+
end
|
|
100
139
|
end
|
|
101
140
|
|
|
102
141
|
# Yields records newest first. Unparsable lines are skipped. The block can
|
|
@@ -161,11 +200,52 @@ module Oops
|
|
|
161
200
|
end
|
|
162
201
|
|
|
163
202
|
# Rewrites the log with the newest complete records that fit in TRIM_TO
|
|
164
|
-
# of the cap (always at least the newest one).
|
|
165
|
-
# next to the log and renamed over it, so a reader sees either the old or
|
|
166
|
-
# the new log, never a mix.
|
|
203
|
+
# of the cap (always at least the newest one).
|
|
167
204
|
def trim
|
|
168
|
-
|
|
205
|
+
rewrite(newest_lines((@max_bytes * TRIM_TO).to_i))
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def age_check_due?(now)
|
|
209
|
+
return false unless @max_age
|
|
210
|
+
return false if @next_age_check && now < @next_age_check
|
|
211
|
+
|
|
212
|
+
@next_age_check = now + AGE_CHECK_INTERVAL
|
|
213
|
+
true
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# Records are appended in the order they end, so the first line is the
|
|
217
|
+
# oldest; the log is rewritten only when that one is too old or unreadable.
|
|
218
|
+
def expire(cutoff)
|
|
219
|
+
first = File.open(@path, "rb", &:gets)
|
|
220
|
+
return if first.nil?
|
|
221
|
+
|
|
222
|
+
record = parse(first.chomp)
|
|
223
|
+
return if record && record.ended_at.to_i >= cutoff
|
|
224
|
+
|
|
225
|
+
rewrite(lines_ended_since(cutoff))
|
|
226
|
+
rescue StandardError
|
|
227
|
+
nil
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
# Newest lines first, back to the first record that ended before cutoff.
|
|
231
|
+
def lines_ended_since(cutoff)
|
|
232
|
+
lines = []
|
|
233
|
+
File.open(@path, "rb") do |io|
|
|
234
|
+
each_line_backward(io) do |line|
|
|
235
|
+
record = parse(line)
|
|
236
|
+
next unless record
|
|
237
|
+
break if record.ended_at.to_i < cutoff
|
|
238
|
+
|
|
239
|
+
lines << line
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
lines
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# Replaces the log with keep (newest first). The new file is written next
|
|
246
|
+
# to the log and renamed over it, so a reader sees either the old or the
|
|
247
|
+
# new log, never a mix.
|
|
248
|
+
def rewrite(keep)
|
|
169
249
|
tmp = "#{@path}.trim"
|
|
170
250
|
remove_file(tmp)
|
|
171
251
|
File.open(tmp, File::WRONLY | File::CREAT | File::EXCL, 0o600) do |f|
|
data/lib/oops/shell/oops.bash
CHANGED
|
@@ -68,6 +68,16 @@ if [[ $- == *i* ]] && [ -t 0 ] && [ -t 1 ] && [ -z "${OOPS_DISABLE:-}" ] &&
|
|
|
68
68
|
if __oops_runs_script; then
|
|
69
69
|
: # not a prompt to record; run the script as asked
|
|
70
70
|
elif command -v "$__oops_bin" >/dev/null 2>&1; then
|
|
71
|
+
# Ghostty injects its bash integration by starting bash in POSIX mode
|
|
72
|
+
# with ENV pointing at it; that file sources the rc files (this one)
|
|
73
|
+
# with __ghostty_bash_flags set, and loads the integration afterwards.
|
|
74
|
+
# The restarted shell is not injected, so note whether this one is;
|
|
75
|
+
# the restarted shell loads the integration again (see the end).
|
|
76
|
+
if [ -n "${__ghostty_bash_flags+x}" ]; then
|
|
77
|
+
export __OOPS_GHOSTTY=1
|
|
78
|
+
else
|
|
79
|
+
unset __OOPS_GHOSTTY
|
|
80
|
+
fi
|
|
71
81
|
# execfail: if the exec fails anyway, keep this shell running.
|
|
72
82
|
__oops_execfail=$(shopt -p execfail || :)
|
|
73
83
|
shopt -s execfail
|
|
@@ -128,9 +138,13 @@ if [[ $- == *i* ]] && [ -t 0 ] && [ -t 1 ] && [ -z "${OOPS_DISABLE:-}" ] &&
|
|
|
128
138
|
[ "${BASH_SUBSHELL:-0}" = 0 ]; then
|
|
129
139
|
case $BASH_COMMAND in
|
|
130
140
|
__oops_precmd*) ;; # empty line: the prompt is drawn again
|
|
141
|
+
# Ghostty's PS0 hook, run in this shell by bash 5.3 before the line.
|
|
142
|
+
__ghostty_preexec_hook) ;;
|
|
131
143
|
*)
|
|
132
|
-
|
|
133
|
-
|
|
144
|
+
# Entries after a rewrap, see __oops_wrap. Blanks are ignored:
|
|
145
|
+
# bash prints `a 2>/dev/null` back as `a 2> /dev/null`.
|
|
146
|
+
case ${__oops_tail//[[:space:]]/} in
|
|
147
|
+
*"${BASH_COMMAND//[[:space:]]/}"*) ;;
|
|
134
148
|
*)
|
|
135
149
|
__oops_armed=
|
|
136
150
|
__oops_tail=
|
|
@@ -331,6 +345,20 @@ if [[ $- == *i* ]] && [ -t 0 ] && [ -t 1 ] && [ -z "${OOPS_DISABLE:-}" ] &&
|
|
|
331
345
|
*) PROMPT_COMMAND="${PROMPT_COMMAND:-}"$'\n'"$__oops_e_install" ;;
|
|
332
346
|
esac
|
|
333
347
|
fi
|
|
348
|
+
|
|
349
|
+
# The shell this one replaced was injected with Ghostty's integration
|
|
350
|
+
# (see above); load it here, after our hooks, as Ghostty documents for a
|
|
351
|
+
# manual setup. Once per shell process.
|
|
352
|
+
if [ -n "${__OOPS_GHOSTTY:-}" ] && [ -z "${__oops_ghostty_loaded:-}" ] &&
|
|
353
|
+
[ -n "${GHOSTTY_RESOURCES_DIR:-}" ] &&
|
|
354
|
+
[ -r "$GHOSTTY_RESOURCES_DIR/shell-integration/bash/ghostty.bash" ]; then
|
|
355
|
+
__oops_ghostty_loaded=1
|
|
356
|
+
# It reads unset variables, which `set -u` earlier in ~/.bashrc rejects.
|
|
357
|
+
case $- in *u*) __oops_nounset=1 && set +u ;; *) __oops_nounset= ;; esac
|
|
358
|
+
builtin source "$GHOSTTY_RESOURCES_DIR/shell-integration/bash/ghostty.bash"
|
|
359
|
+
if [ -n "$__oops_nounset" ]; then set -u; fi
|
|
360
|
+
unset __oops_nounset
|
|
361
|
+
fi
|
|
334
362
|
fi
|
|
335
363
|
unset -f __oops_runs_script
|
|
336
364
|
fi
|
data/lib/oops/stream_parser.rb
CHANGED
|
@@ -19,7 +19,8 @@ module Oops
|
|
|
19
19
|
# 2. Recording. Content bytes seen between a start marker and the matching
|
|
20
20
|
# end marker are stored for that command, except inside alternate-screen
|
|
21
21
|
# spans ("ESC [ ? 1049 h", "ESC [ ? 1047 h", "ESC [ ? 47 h" up to any of
|
|
22
|
-
# the matching "l" forms), which are replaced by one
|
|
22
|
+
# the matching "l" forms), which are replaced by one line, e.g.
|
|
23
|
+
# "[git: full-screen output omitted]" (see omitted_line). The
|
|
23
24
|
# alternate-screen detector keeps its own small pending tail so that a
|
|
24
25
|
# sequence split across chunks is still recognised; this only delays
|
|
25
26
|
# storing, never forwarding.
|
|
@@ -32,6 +33,14 @@ module Oops
|
|
|
32
33
|
# and the marker is ignored when it returns false.
|
|
33
34
|
class StreamParser
|
|
34
35
|
OMITTED = "[full-screen app output omitted]\n".b.freeze
|
|
36
|
+
# A shell variable assignment before the command, as in LANG=C git log.
|
|
37
|
+
ASSIGNMENT = /\A[A-Za-z_][A-Za-z0-9_]*=/n
|
|
38
|
+
# Only a plain word is shown as the name. Anything else (quotes, a URL
|
|
39
|
+
# with a password, a NUL byte) is not a command name worth the risk.
|
|
40
|
+
PLAIN_WORD = %r{\A[A-Za-z0-9_.+/-]+\z}n
|
|
41
|
+
# In a line with several commands the first word may not be the program
|
|
42
|
+
# that went full screen (echo a; less b), so such lines get no name.
|
|
43
|
+
SEVERAL_COMMANDS = /[;&|()`{}\n]|\$\(/n
|
|
35
44
|
MAX_HELD_MARKER = 65_536
|
|
36
45
|
|
|
37
46
|
PREFIX = Marker::PREFIX
|
|
@@ -323,7 +332,7 @@ module Oops
|
|
|
323
332
|
return
|
|
324
333
|
end
|
|
325
334
|
cmd.buffer << data.byteslice(pos, match.begin(0) - pos)
|
|
326
|
-
cmd.buffer <<
|
|
335
|
+
cmd.buffer << omitted_line(cmd.cmd)
|
|
327
336
|
cmd.fullscreen = true
|
|
328
337
|
cmd.in_span = true
|
|
329
338
|
end
|
|
@@ -331,6 +340,20 @@ module Oops
|
|
|
331
340
|
end
|
|
332
341
|
end
|
|
333
342
|
|
|
343
|
+
# Names the command's first word (after any VAR=value), without its
|
|
344
|
+
# directory. The program that switched screens may be one it started, as
|
|
345
|
+
# git starts less, so this is the command the user typed, not the pager.
|
|
346
|
+
def omitted_line(command_line)
|
|
347
|
+
line = command_line.to_s.b
|
|
348
|
+
return OMITTED if line.match?(SEVERAL_COMMANDS)
|
|
349
|
+
|
|
350
|
+
word = line.split.find { |w| !w.match?(ASSIGNMENT) }
|
|
351
|
+
name = word[%r{[^/]+\z}n] if word&.match?(PLAIN_WORD)
|
|
352
|
+
return OMITTED unless name
|
|
353
|
+
|
|
354
|
+
"[#{name}: full-screen output omitted]\n".b
|
|
355
|
+
end
|
|
356
|
+
|
|
334
357
|
# Index of the earliest ESC at or after from whose tail is a proper prefix
|
|
335
358
|
# of one of seqs, or nil.
|
|
336
359
|
def partial_start(data, from, seqs)
|
data/lib/oops/version.rb
CHANGED