Skip to content

Shell: scrollback + wheel inside the in-panel shell, and S to quit k10s into a real-terminal shell #12

Open

Description

@pierreneter

Summary

Two gaps in the pod shell (s), both about "I need to see / do more than the pane gives me":

  1. The in-panel shell cannot scroll. Output that leaves the screen is gone, the wheel over the pane silently moves the hidden table's selection, and full-screen programs in the pod (less, vim, htop) never see wheel events.
  2. There is no way to get the real terminal. When the emulator is not enough (long output, native select/copy, true colours, a long session), you have to quit k10s and type kubectl exec -it … -- sh by hand. S should do that: quit k10s and exec into the selected pod in the terminal k10s was started from.

Everything below was checked against main (v0.1.7, bubbletea v1.3.10, vt10x 2022-03-01).


Part 1 — scrolling in the in-panel shell

Current behaviour

  • Wheel over the shell pane corrupts the table selection. handleMouse → scrollPaneAt → scrollMain (model.go:1748). scrollMain handles modeLogs and modeText, then falls through to the table branch and mutates m.rowIdx / m.rowMem[kind] even though the pane is showing a shell. Detach after scrolling and the row you had selected has moved.
  • No scrollback. vt10x keeps only the visible screen: scrollUp clears the evicted rows and swaps the rest (vt10x state.go:477); there is no history buffer (the repo has a TODO: selection scroll and no commits since 2022-03). cat /etc/nginx/nginx.conf in a 20-row pane shows the last 20 lines, full stop.
  • pgup/pgdn do nothing useful. keyBytes forwards them to the pod as \x1b[5~ / \x1b[6~; a plain sh/bash prompt ignores them.
  • Mouse-aware programs never get the wheel. vt10x parses DECSET 9/1000/1002/1003/1006 and exposes the state via Terminal.Mode() (ModeMouseButton, ModeMouseMotion, ModeMouseSgr, …), but k10s never forwards a mouse report, so less --mouse, vim :set mouse=a, htop behave as if the terminal had no mouse.

Proposal

  1. Stop the fall-through. In scrollMain (or scrollPaneAt), modeShell gets its own branch; the table is never touched while a shell is in the pane.
  2. Add a scrollback buffer to the emulator. vt10x upstream has none and is unmaintained, so fork it into internal/vt (it is ~1.5k LOC, MIT) and add history the way st's scrollback patch does: in scrollUp, when orig == 0, the scroll region is the whole screen and ModeAltScreen is off, push the evicted []Glyph rows onto a ring of 2000 lines (const for now; a config knob can come later). Expose HistoryLen() and HistoryCell(i, x). Alt-screen output (vim/less) is never recorded, same as every real terminal.
    Alternative to evaluate before forking: swap to an emulator that already has scrollback (e.g. charmbracelet/x/vt). Only worth it if the render loop in shellBody (Cell, Cursor, CursorVisible, Size, Resize, Mode) maps over cheaply; otherwise fork.
  3. A view offset in the model. shellScroll int, 0 = live.
    • Wheel over the pane: ±2 lines (same step as the other panes).
    • Keys while in the shell: shift+up / shift+down one line, shift+home / shift+end top / live. (shift+pgup/shift+pgdn are not decodable in bubbletea v1 and most terminals eat them for their own scrollback anyway.) These are the only keys the shell does not forward, besides ctrl+].
    • Any key forwarded to the pod snaps back to live (xterm/kitty convention). New output while scrolled keeps the position.
    • shellBody renders history tail + screen shifted by the offset; the cursor is drawn only when live.
    • shellStatusLine shows ↑ 120 lines back · any key returns while scrolled, so a stale screen is never mistaken for a hung pod.
  4. Forward the wheel when the pod program wants it. When ModeAltScreen is on there is nothing to scroll locally, so instead:
    • Mode()&ModeMouseMask != 0 → write a mouse report to the session with pane-relative coordinates: SGR \x1b[<64;x;yM / \x1b[<65;x;yM when ModeMouseSgr, X10 \x1b[M + bytes otherwise.
    • no mouse mode → write 3× \x1b[A / \x1b[B (xterm "alternate scroll" behaviour; \x1bOA/\x1bOB when ModeAppCursor). This makes the wheel work in less, man, kubectl logs | less inside the pod with zero config.
    • Not on alt screen and mouse mode on (rare, e.g. a TUI that does not switch screens): forward the report; otherwise scroll locally.
  5. Resize: history lines keep the width they were written at; no reflow (out of scope).

Acceptance

  • Wheel over the shell pane never changes rowIdx / rowMem (regression test next to TestShellSizedToThePanel).
  • Write rows+50 lines into the fake session, wheel up → the first line is visible; one forwarded keystroke → back to live and the status line is the normal one.
  • History is capped (write 3000 lines → HistoryLen() == 2000) and alt-screen output is not recorded.
  • Alt screen + ?1000h/?1006h → one wheel tick yields the SGR sequence on the session's stdin; alt screen without mouse mode → three \x1b[A.
  • ctrl+], [ detach ] and a session that ends on its own still work from a scrolled position.
  • Docs: docs/ui.md "Shell" section, docs/keybindings.md, the in-app help in internal/ui/commands.go, and the s shell row in README.md.

Part 2 — S: quit k10s and open the shell in the parent terminal

Why

The in-panel shell is a small emulator with a theme-mapped 16-colour palette, no native selection (the mouse is captured; ctrl+s copy-mode only half helps), and whatever scrollback Part 1 gives it. Sometimes you want the real thing: the pod shell in the terminal you launched k10s from, with that terminal's scrollback, selection, fonts and colours, and k10s out of the way. Today that is "quit, remember the pod name, type kubectl exec -it … -n … -- sh". S should be that, with k10s resolving pod and container exactly as s does (workload → pod, bash if present else sh).

What already exists

  • domain.Source.Shell(kind, ns, name) (tea.ExecCommand, error) is still in the interface and implemented in k8s, mock and pendingSource. The k8s one (execCommand in internal/k8s/exec.go) does a raw-TTY in-process remotecommand stream with kterm.TTY{Raw: true} and MonitorSize — a kubectl exec -it equivalent with no kubectl binary. Nothing in the UI calls it since the in-panel shell replaced the old startShell (4c30892).
  • Precedent for "do a thing in the clean terminal after the TUI exits": /update sets m.relaunch, returns tea.Quit, and main.go runs update.Relaunch after p.Run() returns — alt screen torn down, mouse tracking off, nothing of k10s left on screen.

Proposal

  • New action domain.AShellOut (id "shellout", name open to bikeshedding): key S, label "Shell (terminal)", listed in Actions directly under s Shell. Offered wherever AShell is (Kind.Can), not allowed in read-only (ReadOnlyAllows + its test list), not Risky. S is free: nothing in handleKey or the action table uses it (D and T are the only shifted letters in use).

  • Flow: fireAction → cmd, err := m.src.Shell(kind, ns, name).

    • cmd == nil or error (demo, pending connection, not pod-backed) → toast no interactive shell here (offline demo, or not a pod), stay in k10s.
    • otherwise m.exitShell = cmd, m.backToTable(), return tea.Quit.
  • main.go, after p.Run() and before the relaunch branch:

    if c := m.ExitShell(); c != nil {
        c.SetStdin(os.Stdin); c.SetStdout(os.Stdout); c.SetStderr(os.Stderr)
        fmt.Fprintf(os.Stderr, "k10s: shell into %s (%s) — exit the shell to get your prompt back\n", name, ns)
        if err := c.Run(); err != nil { /* print, exit 1 */ }
        return
    }

    The process ends when the pod shell ends. The exit code should follow the remote shell's (remotecommand surfaces it as exec.CodeExitError).

  • Keep the store alive until then. The source lease only closes on context replace, and nothing on the tea.Quit path calls Close(); add a test so that stays true, because execCommand.Run builds its request lazily from store.c.

  • Align Shell() with ShellSession(). Store.Shell still rejects kind != "pods" and uses podContainer; switch it to logTarget so S works on Deployments/StatefulSets/Jobs the way s and l do.

  • Not tea.ExecProcess. That hands the terminal over and comes back into k10s afterwards; the point of S is to leave. (Coming back can be a follow-up — it is one line with the same execCommand — but it is a different feature.)

  • Inside the in-panel shell S is just a key and goes to the pod. Reaching the external shell from there is detach → S. A /shell slash command is optional sugar, not required.

  • Cross-platform: nothing here needs syscall.Exec; kterm raw mode is what kubectl uses on Windows too.

Acceptance

  • Table, pod selected, S → k10s exits the alt screen and the pod shell is running in the parent terminal; exiting it returns to the user's prompt with the shell's exit code.
  • S on a Deployment resolves to one of its pods (same pod l/s would pick).
  • S on a kind without a shell (Secret) is not offered; on the demo context it toasts and does not quit; in --readonly it is not offered.
  • Unit test: fireAction(AShellOut) with a source whose Shell() returns a command → ExitShell() != nil and the returned cmd is tea.Quit; with nil, nil → toast, no quit.
  • Actions pane shows S Shell (terminal); help text (commands.go), docs/keybindings.md, docs/ui.md Shell section and the README action table mention it, and say plainly that it quits k10s.

Out of scope

  • Reflowing scrollback on resize, searching the scrollback, select/copy inside the pane.
  • Returning to k10s after the external shell exits (would be tea.ExecProcess; separate issue if wanted).
  • Making S reachable from inside the running in-panel shell.

Pointers

  • internal/ui/shell.go — handleShellKey, keyBytes, shellBody, shellStatusLine
  • internal/ui/model.go — handleMouse (~2668), scrollPaneAt/scrollMain (~1727), handleKey shell guard (~1288), action dispatch (~1623), shellStartMsg/shellOutMsg (~1047)
  • internal/ui/actions.go — action table; internal/domain/domain.go, internal/domain/readonly.go
  • internal/k8s/exec.go — execCommand (tea.ExecCommand), Store.Shell; internal/k8s/shell.go — ShellSession, logTarget
  • internal/ui/update.go Relaunch() + main.go after p.Run() — the quit-then-act pattern to copy
  • internal/ui/shell_test.go — fake session harness for the new tests

Suggest two PRs (Part 1, Part 2); they do not touch the same lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issuehelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions