Skip to content

Commit 3abecf8

Browse files
committed
Make clear command work without explicit session argument
When run inside a session, `atch clear` now defaults to clearing the current session's log by reading the ATCH_SESSION environment variable. For nested sessions, it clears the innermost session.
1 parent 6f946b6 commit 3abecf8

3 files changed

Lines changed: 41 additions & 8 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ If no command is given, `$SHELL` is used.
8282
| `run <session> [cmd...]` | Like `start`, but atch stays in the foreground instead of daemonizing. |
8383
| `push <session>` | Copy stdin verbatim to the session. |
8484
| `kill [-f] <session>` | Gracefully stop a session (SIGTERM, then SIGKILL after 5 s if needed). With `-f` / `--force`, skip the grace period and send SIGKILL immediately. |
85-
| `clear <session>` | Truncate the on-disk session log. |
85+
| `clear [<session>]` | Truncate the on-disk session log. Defaults to the current session when run inside one. |
8686
| `list` | List all sessions. Shows `[attached]` when a client is connected, `[stale]` for leftover sockets with no running master. Prints `(no sessions)` when the list is empty. |
8787
| `current` | Print the current session name and exit 0 if inside a session; exit 1 silently if not. |
8888

@@ -241,7 +241,7 @@ Every byte written to the pty is appended to a log file on disk
241241
This is fundamentally different from `tmux`, `screen`, and `dtach`: they hold
242242
history only in memory. When the process exits or the machine restarts, the
243243
output is gone. With `atch` the raw byte stream is on disk until you
244-
explicitly clear it with `atch clear <session>`.
244+
explicitly clear it with `atch clear` (or `atch clear <session>`).
245245

246246
The log is capped at 1 MB by default; once it exceeds that, only the most
247247
recent 1 MB is kept. You can change the cap per session with `-C`:
@@ -286,7 +286,8 @@ The value must be a power of two.
286286
To wipe the on-disk log and start clean on the next attach:
287287

288288
```sh
289-
atch clear mysession
289+
atch clear # inside a session — clears the current session's log
290+
atch clear mysession # from outside — clear a named session's log
290291
```
291292

292293
## Backward compatibility

atch.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,22 @@ static int cmd_clear(int argc, char **argv)
507507
char log_path[600];
508508
int fd;
509509

510-
if (consume_session(&argc, &argv))
511-
return 1;
510+
if (argc > 0) {
511+
if (consume_session(&argc, &argv))
512+
return 1;
513+
} else {
514+
const char *chain = getenv(SESSION_ENVVAR);
515+
const char *last;
516+
517+
if (!chain || !*chain) {
518+
printf("%s: No session was specified.\n", progname);
519+
printf("Try '%s --help' for more information.\n",
520+
progname);
521+
return 1;
522+
}
523+
last = strrchr(chain, ':');
524+
sockname = (char *)(last ? last + 1 : chain);
525+
}
512526
if (argc > 0) {
513527
printf("%s: Invalid number of arguments.\n", progname);
514528
printf("Try '%s --help' for more information.\n", progname);
@@ -579,7 +593,7 @@ static void usage(void)
579593
" kill [-f] <session>"
580594
"\t\tStop session (SIGTERM then SIGKILL)\n"
581595
" -f, --force\t\t\tSkip grace period, send SIGKILL immediately\n"
582-
" clear <session>"
596+
" clear [<session>]"
583597
"\t\t\tTruncate the session log\n"
584598
" list\t\t\t\t\tList all sessions\n"
585599
" current\t\t\t\tPrint current session name\n"

tests/test.sh

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ assert_contains "kill -f: nonexistent → message" "does not exist" "$out"
250250
# ── 6. clear command ─────────────────────────────────────────────────────────
251251

252252
run "$ATCH" clear
253-
assert_exit "clear: no session → exit 1" 1 "$rc"
254-
assert_contains "clear: no session → message" "No session was specified" "$out"
253+
assert_exit "clear: no session, no env → exit 1" 1 "$rc"
254+
assert_contains "clear: no session, no env → message" "No session was specified" "$out"
255255

256256
# clear a nonexistent log is silent success
257257
run "$ATCH" clear s-noexist-clear
@@ -279,6 +279,24 @@ run "$ATCH" clear s-noexist-clear extra
279279
assert_exit "clear: extra arg → exit 1" 1 "$rc"
280280
assert_contains "clear: extra arg → message" "Invalid number of arguments" "$out"
281281

282+
# atch clear with ATCH_SESSION set uses current session
283+
"$ATCH" start s-clear-cur sleep 999
284+
wait_socket s-clear-cur
285+
run env ATCH_SESSION="$HOME/.cache/atch/s-clear-cur" "$ATCH" clear
286+
assert_exit "clear: current session → exit 0" 0 "$rc"
287+
assert_contains "clear: current session → message" "log cleared" "$out"
288+
289+
# atch clear with no arg uses innermost session from nested chain
290+
"$ATCH" start s-clear-inner sleep 999
291+
wait_socket s-clear-inner
292+
run env ATCH_SESSION="$HOME/.cache/atch/s-clear-cur:$HOME/.cache/atch/s-clear-inner" \
293+
"$ATCH" clear
294+
assert_exit "clear: nested env no arg → exit 0" 0 "$rc"
295+
assert_contains "clear: nested env no arg → message" "s-clear-inner" "$out"
296+
297+
tidy s-clear-cur
298+
tidy s-clear-inner
299+
282300
# ── 7. current command ───────────────────────────────────────────────────────
283301

284302
run "$ATCH" current

0 commit comments

Comments
 (0)