From ffa8a7cb50c9e05beb7b9b93388c77aedc9f91cd Mon Sep 17 00:00:00 2001 From: Erfan Yousefi Date: Sun, 8 Mar 2026 03:29:19 +0100 Subject: [PATCH] feat: establish krust v0.1 foundation with k9s-compatible UX and OSS release scaffolding This commit consolidates the first production-ready baseline for krust, including core runtime behavior, operator UX, safety rails, and project publication assets.\n\nCore runtime and architecture\n- Implement lazy, active-scope watch-plan reconciliation across contexts/kinds/namespaces\n- Keep warm-context retention bounded by TTL for faster context switching\n- Add bounded watch reconnect handling and terminal handling for RBAC-forbidden watchers\n- Keep invalidation-driven rendering with FPS control and bounded log/state paths\n\nOperator UX and workflows\n- Expand k9s-style navigation/command aliases for supported Kubernetes resources\n- Improve pane consistency for table/detail/log overlays and scrolling behavior\n- Add detail viewer search semantics (non-destructive search/highlight) with match navigation\n- Add YAML/JSON detail format toggle and syntax highlighting for both formats\n- Add secret decode pane improvements and format-aware edit/apply behavior\n- Add clipboard copy and dump flows with native backend + OSC52 fallback\n\nSafety and action model\n- Keep readonly enforcement for mutation paths\n- Maintain guarded action behavior and clearer error/status surfacing\n\nDocumentation and open-source readiness\n- Rewrite README for community-facing onboarding and operational clarity\n- Add architecture and performance guides under docs/\n- Add CONTRIBUTING guide and PR template\n- Add GitHub Actions CI pipeline (fmt/check/test/release build)\n- Add GitHub Actions release pipeline for tagged multi-platform artifacts\n- Add Cargo package metadata and repository links\n- Add .gitignore hygiene for macOS Finder artifacts\n\nQuality and verification\n- Bring strict clippy (-D warnings) to clean state for current codebase\n- Validate full required pipeline: fmt, check, test, and release build --- .github/pull_request_template.md | 25 + .github/workflows/ci.yml | 42 + .github/workflows/release.yml | 81 ++ .gitignore | 4 + AGENTS.md | 2 +- CONTRIBUTING.md | 125 +++ Cargo.lock | 3 +- Cargo.toml | 8 +- LICENSE | 21 + README.md | 320 ++++-- docs/architecture.md | 146 +++ docs/performance.md | 103 ++ src/cli.rs | 2 +- src/cluster/kube_provider.rs | 241 +++- src/cluster/mod.rs | 5 + src/config.rs | 19 +- src/lib.rs | 19 +- src/main.rs | 2 +- src/model.rs | 1 + src/ui/app.rs | 1757 +++++++++++++++++++++++++++--- 20 files changed, 2632 insertions(+), 294 deletions(-) create mode 100644 .github/pull_request_template.md create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/release.yml create mode 100644 CONTRIBUTING.md create mode 100644 LICENSE create mode 100644 docs/architecture.md create mode 100644 docs/performance.md diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..48acbbc --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,25 @@ +## Summary + +Describe what changed and why. + +## Validation + +- [ ] cargo fmt --all +- [ ] cargo check +- [ ] cargo test +- [ ] cargo build --release + +## Performance Impact + +If this touches watches/projection/render/logs, include impact notes. + +- Hot path changed: +- CPU/memory impact: +- API pressure impact: +- Failure-mode impact (RBAC/auth/network): + +## Compatibility and UX + +- [ ] keybinding behavior reviewed (k9s compatibility where applicable) +- [ ] command behavior reviewed +- [ ] docs updated for user-visible changes diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..165476d --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,42 @@ +name: CI + +on: + pull_request: + push: + branches: + - main + - master + +permissions: + contents: read + +concurrency: + group: ci-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + validate: + name: Validate (fmt/check/test/build) + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + + - name: Cache Rust artifacts + uses: Swatinem/rust-cache@v2 + + - name: cargo fmt --all -- --check + run: cargo fmt --all -- --check + + - name: cargo check --locked + run: cargo check --locked + + - name: cargo test --locked + run: cargo test --locked + + - name: cargo build --release --locked + run: cargo build --release --locked diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..7eadb6d --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,81 @@ +name: Release + +on: + push: + tags: + - 'v*' + +permissions: + contents: write + +concurrency: + group: release-${{ github.ref }} + cancel-in-progress: false + +jobs: + build: + name: Build ${{ matrix.target }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + include: + - os: ubuntu-latest + target: x86_64-unknown-linux-gnu + - os: macos-13 + target: x86_64-apple-darwin + - os: macos-14 + target: aarch64-apple-darwin + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + + - name: Add target + run: rustup target add ${{ matrix.target }} + + - name: Cache Rust artifacts + uses: Swatinem/rust-cache@v2 + + - name: Build release binary + run: cargo build --release --locked --target ${{ matrix.target }} + + - name: Package artifact + shell: bash + run: | + set -euo pipefail + TARGET='${{ matrix.target }}' + PKG="krust-${TARGET}" + mkdir -p "dist/${PKG}" + cp "target/${TARGET}/release/krust" "dist/${PKG}/krust" + cp README.md LICENSE "dist/${PKG}/" + tar -C dist -czf "dist/${PKG}.tar.gz" "${PKG}" + + - name: Upload build artifact + uses: actions/upload-artifact@v4 + with: + name: package-${{ matrix.target }} + path: dist/krust-${{ matrix.target }}.tar.gz + if-no-files-found: error + + publish: + name: Publish GitHub Release + needs: build + runs-on: ubuntu-latest + + steps: + - name: Download packaged artifacts + uses: actions/download-artifact@v4 + with: + pattern: package-* + path: dist + merge-multiple: true + + - name: Create release and upload artifacts + uses: softprops/action-gh-release@v2 + with: + files: dist/*.tar.gz + generate_release_notes: true diff --git a/.gitignore b/.gitignore index 0728338..3efb4df 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,7 @@ target # Added by cargo /target + +# macOS Finder artifacts +.DS_Store +**/.DS_Store diff --git a/AGENTS.md b/AGENTS.md index e710aa8..49dd782 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,7 +1,7 @@ # AGENTS.md ## Project Intent -`krs` is a latency-first, power-user Kubernetes TUI for very large environments: +`krust` is a latency-first, power-user Kubernetes TUI for very large environments: - many contexts/clusters - many resources per cluster - frequent churn diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..fd1afaa --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,125 @@ +# Contributing to krust + +Thanks for contributing to `krust`. + +This project is built for operators running large Kubernetes estates, so performance and operational clarity are non-negotiable. + +## Principles + +When proposing changes, optimize for these outcomes: + +- low input-to-render latency under churn +- controlled Kubernetes API pressure +- bounded memory growth +- predictable, keyboard-first UX +- clear behavior under RBAC/auth/network failures + +If a feature conflicts with these principles, prefer the simpler and faster path. + +## Development Setup + +### Prerequisites + +- Rust stable toolchain +- kubeconfig with at least one working context +- macOS or Linux environment + +### Clone and build + +```bash +git clone git@github.com:ErfanY/krust.git +cd krust +cargo build --release +``` + +Run locally: + +```bash +cargo run -- --context +``` + +## Required Validation Pipeline + +Run this sequence before opening or updating a PR: + +```bash +cargo fmt --all +cargo check +cargo test +cargo build --release +``` + +The CI pipeline enforces the same flow. + +## Code and Design Expectations + +## Performance-sensitive changes + +If your change touches watchers, projection, rendering, or logs, include in your PR description: + +- what hot path changed +- expected complexity/cost impact +- API watch/list pressure impact +- memory impact and bounds +- failure-mode behavior (RBAC/auth/disconnect) + +Use [`docs/performance.md`](docs/performance.md) as the baseline. + +## API/watch behavior + +- avoid eager all-context/all-kind watchers by default +- prefer active-scope/lazy activation +- keep reconnect logic bounded +- treat RBAC denials as expected operational state + +## UX behavior + +- preserve existing keybindings unless explicitly changing compatibility behavior +- avoid introducing mode ambiguities +- keep detail/log scrolling and search behavior predictable + +## Testing Guidance + +Add or update tests when changing behavior: + +- unit tests for parsing, formatting, and helper logic +- state/projection tests when list or sorting behavior changes +- UI behavior tests for keybinding and pane semantics + +Performance benchmark tests can be added as ignored tests. + +## Pull Request Checklist + +Before requesting review, verify: + +- [ ] `cargo fmt --all` +- [ ] `cargo check` +- [ ] `cargo test` +- [ ] `cargo build --release` +- [ ] docs updated (`README`/`docs/*`) for user-visible changes +- [ ] performance notes included for hot-path changes + +## Commit Style + +Use imperative commit titles and keep scope tight. + +Good examples: + +- `ui: clamp detail scroll bounds for wrapped views` +- `cluster: disable forbidden watchers per context/kind` +- `docs: add performance tuning guide` + +## Security and Sensitive Data + +- never commit kubeconfigs, tokens, certificates, or customer data +- redact cluster/account identifiers from logs/screenshots in PRs where possible +- for sensitive reports, contact maintainers privately + +## Need help? + +Open an issue with: + +- expected behavior +- current behavior +- reproducible steps +- environment (`krust` version, OS, Kubernetes version) diff --git a/Cargo.lock b/Cargo.lock index 1318063..4c644de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1042,7 +1042,7 @@ dependencies = [ ] [[package]] -name = "krs" +name = "krust" version = "0.1.0" dependencies = [ "anyhow", @@ -1059,6 +1059,7 @@ dependencies = [ "ratatui", "serde", "serde_json", + "serde_yaml", "thiserror 2.0.18", "tokio", "toml", diff --git a/Cargo.toml b/Cargo.toml index 67c4f84..db92696 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,12 @@ [package] -name = "krs" +name = "krust" version = "0.1.0" edition = "2024" +license = "MIT" +description = "Latency-first Kubernetes terminal navigator for multi-cluster operators" +readme = "README.md" +repository = "https://github.com/ErfanY/krust" +homepage = "https://github.com/ErfanY/krust" [dependencies] anyhow = "1.0.102" @@ -17,6 +22,7 @@ kube-runtime = "3.0.1" ratatui = { version = "0.30.0", default-features = false, features = ["crossterm"] } serde = { version = "1.0.228", features = ["derive"] } serde_json = "1.0.149" +serde_yaml = "0.9.34" thiserror = "2.0.18" tokio = { version = "1.50.0", features = ["macros", "rt-multi-thread", "signal", "sync", "time"] } toml = "1.0.6" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..be5ef86 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 Erfan Yousefi + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 294a955..a7ac646 100644 --- a/README.md +++ b/README.md @@ -1,44 +1,136 @@ -# krs +# krust -High-performance Kubernetes terminal navigator in Rust. +[![CI](https://github.com/ErfanY/krust/actions/workflows/ci.yml/badge.svg)](https://github.com/ErfanY/krust/actions/workflows/ci.yml) +[![Release](https://github.com/ErfanY/krust/actions/workflows/release.yml/badge.svg)](https://github.com/ErfanY/krust/actions/workflows/release.yml) +[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE) +[![Kubernetes 1.33+](https://img.shields.io/badge/Kubernetes-1.33%2B-326CE5?logo=kubernetes&logoColor=white)](https://kubernetes.io/) -Built against `k8s-openapi` `v1_33` (targeting Kubernetes `1.33+` clusters). +`krust` is a latency-first Kubernetes terminal navigator for operators who work across many clusters and large resource sets. -## Current Status +It is intentionally focused on fast core workflows (navigation, inspect, logs, safe edits/actions) with high k9s shortcut compatibility, while keeping API pressure and UI jitter under control. -`krs` now includes a working foundation for the plan: +Built with Rust (`tokio`, `kube`, `kube_runtime`, `ratatui`) and compiled against Kubernetes `1.33+` APIs (`k8s-openapi` `v1_33`). -- Layered architecture: `cluster IO` -> `state cache` -> `view projector` -> `TUI renderer` -- Multi-context tabs in one session -- Lazy watch activation with active-context watch plans (inactive context watchers are paused) -- Event-driven state deltas over bounded channels -- Invalidation-based rendering with FPS cap -- Kubernetes watch streams for a broad set of core resource kinds -- k9s-style keyboard navigation, filter/sort, table + describe/events/logs panes -- Safe mutation workflow with confirmation (`delete` implemented for Pods) -- `config.toml` and `keymap.toml` loading +## Why krust -## CLI +Most terminal Kubernetes tools become noisy or sluggish when context count and watch volume grow. `krust` is designed around a different default: + +- activate only the watch scope needed for what you are currently looking at +- keep render work invalidation-driven with a frame cap +- keep logs bounded and stream-focused +- treat RBAC denials and API turbulence as normal operational states + +This is also why plugins are out of scope for v1: predictable latency and operational clarity come first. + +## Scope and Non-Goals + +What `krust` is: + +- keyboard-first multi-context Kubernetes navigator +- read-heavy operations with guarded mutations +- high-signal terminal UI for SRE/platform workflows + +What `krust` is not: + +- a plugin/script execution platform +- a replacement for full GitOps pipelines or IDE workflows +- a "watch everything everywhere" cluster crawler at startup + +## Feature Matrix + +| Area | Status | Notes | +| --- | --- | --- | +| Multi-context session | Implemented | Context tabs with fast switching and warm-context retention | +| Namespace scoping | Implemented | Namespace picker and `all` namespace mode | +| Core resource browser | Implemented | Pods, Deployments, RS, STS, DS, Services, Ingresses, ConfigMaps, Secrets, Jobs, CronJobs, PVC/PV, Nodes, Namespaces, Events, SA, Roles, RB/CR/CRB, NetPol, HPA, PDB | +| Describe pane | Implemented | YAML/JSON view toggle with syntax highlighting | +| Secret decode pane | Implemented | Decoded view + edit/apply with automatic base64 re-encode | +| Logs pane | Implemented | Streaming with pause/tail/source filtering; pod multi-container and deploy/rs replica fan-out | +| Edit/apply | Implemented | External editor flow from describe/decode panes | +| Delete action | Implemented | Guarded delete path (currently Pods) | +| k9s-style commands/aliases | Partial parity | Core resource commands and navigation implemented; plugin commands intentionally out of scope | +| Plugin subsystem | Out of scope | Deliberately excluded for performance and simplicity | + +## Compatibility + +- OS: macOS, Linux +- Kubernetes: `1.33+` +- Auth: standard kubeconfig + exec auth providers +- Terminal: basic color through truecolor; OSC52 clipboard fallback supported + +## Installation and Quick Start + +### Install from GitHub Releases (recommended) + +Prebuilt archives are published for: + +- `x86_64-unknown-linux-gnu` +- `x86_64-apple-darwin` +- `aarch64-apple-darwin` + +Example (Linux x86_64): ```bash -krs -krs --context dev-cluster -krs --namespace payments -krs --readonly -krs --kubeconfig ~/.kube/config -krs --all-contexts # optional eager auth/client warmup across contexts +curl -L -o krust.tar.gz \ + https://github.com/ErfanY/krust/releases/latest/download/krust-x86_64-unknown-linux-gnu.tar.gz + +tar -xzf krust.tar.gz +sudo install krust-x86_64-unknown-linux-gnu/krust /usr/local/bin/krust ``` -## Config +### Install from source -`~/.config/krs/config.toml` +```bash +cargo install --git https://github.com/ErfanY/krust.git --locked krust +``` + +### Build locally + +```bash +cargo build --release +``` + +Binary path: + +```bash +target/release/krust +``` + +### Run + +```bash +krust +krust --context +krust --namespace +krust --readonly +krust --kubeconfig +krust --all-contexts +``` + +### CLI flags + +- `--context`: initial context +- `--namespace`: initial namespace scope +- `--readonly`: disable mutations +- `--kubeconfig`: explicit kubeconfig path +- `--all-contexts`: eager auth/client warmup across all contexts + +## Configuration + +Runtime/UI config path: + +- `~/.config/krust/config.toml` + +Keymap override path: + +- `~/.config/krust/keymap.toml` + +Example `config.toml`: ```toml [runtime] fps_limit = 60 delta_channel_capacity = 2048 -namespace = "default" -default_context = "dev-cluster" warm_contexts = 1 warm_context_ttl_secs = 20 @@ -47,94 +139,116 @@ theme = "default" show_help = true ``` -## Keymap +Defaults for initial context/namespace come from kubeconfig unless CLI flags override them. -`~/.config/krs/keymap.toml` +## Quick UX Reference -```toml -quit = "ctrl+c" -next_context = "tab" -prev_context = "shift+tab" -next_kind = "alt+right" -prev_kind = "alt+left" -move_down = "j" -move_up = "k" -goto_top = "g" -goto_bottom = "shift+g" -filter_mode = "/" -cycle_sort = "s" -toggle_desc = "r" -cycle_namespace = "n" -toggle_help = "?" -to_table = "t" -toggle_describe = "d" -to_events = "shift+e" -to_logs = "l" -delete = "ctrl+d" -confirm = "y" -cancel = "esc" -``` - -## Default Navigation +### Core navigation - `tab` / `shift+tab`: switch context tabs -- `j` / `k` (or arrows): move selection, or scroll when viewing boxed text panes -- `Enter`: on `Namespaces` selects namespace and returns to previous resource kind (fallback `Pods`); on resource tables opens describe -- `w` in content box: toggle wrap on/off -- when wrap is off: use left/right arrows for horizontal scroll -- `/`: open command field in filter mode (type pattern and press `Enter`) -- filter mode is realtime while typing; `Esc` cancels and restores previous filter -- `:`: open command field in command mode -- `tab` in command mode: autocomplete commands (and `:ctx` / `:kind` arguments) -- `[` / `]`: command history back/forward -- `-`: rerun previous command -- `ctrl+a`: open resource alias list -- `d`: toggle describe pane -- `v`: k9s-style view (opens detail view) -- `ctrl+d`: delete selected resource (with confirmation) -- `ctrl+k`: k9s-style kill shortcut (mapped to guarded delete flow) -- `esc`: close command/overlay and return focus -- mouse wheel: scrolls inside overlay/detail/table box (terminal scrollback is not used) -- top panes include live pod/cluster utilization from Pod requests/limits and Node allocatable +- `j` / `k` (or arrows): move in tables, scroll in detail panes +- `Enter`: select namespace in namespace view, otherwise open describe +- `d`: describe +- `ctrl+d`: delete (guarded) +- `x`: secret decode view toggle +- `l`: logs pane +- `s`: log tail toggle +- `p`: pause/resume logs +- `S`: log source selector +- `c`: container selector -## Command Field +### Search and command bar -Examples in the command field: +- `/`: filter in table/overlay panes, search in detail/log panes +- `?`: detail/search shortcut +- `n` / `N`: next/previous search match in detail/log panes +- `:`: command mode (`:ctx`, `:ns`, `:po`, `:kind`, `:edit`, `:fmt`, etc.) +- `tab` in command mode: autocomplete +- `ctrl+w` in command/filter mode: delete previous word -- `:ctx` show contexts list in a selectable box (`j/k`, arrows, mouse wheel, `/` filter, `Enter` to switch) -- `:ctx ` switch active context -- `:ns [namespace|all]` with arg sets namespace scope, without arg opens Namespaces -- `:all` (or `:0`) switch to all namespaces -- `:kind ` switch resource kind -- `:po`, `:svc`, `:deploy`, `:ns`, ... k9s-style direct resource switches -- `:resources` show shorthand resource aliases -- `:clear` clear current filter -- `:po kube-system`, `:po /api`, `:po @my-context`, `:deploy -l app=my-api` are supported -- `:po --context --namespace ` and `:po -A` are supported -- `/pod-xxx` apply table filter -- `Tab` autocompletes command names and context/kind arguments +### Detail viewing -## Watched Resource Kinds +- `w`: wrap toggle +- left/right: horizontal scroll (wrap off) +- `gg` / `G`: top/bottom +- `ctrl+u` / `ctrl+d`: half-page up/down +- `y`: copy current view +- `:dump `: write current view to file -Pods, Deployments, ReplicaSets, StatefulSets, DaemonSets, Services, Ingresses, ConfigMaps, -Secrets, Jobs, CronJobs, PVCs, PVs, Nodes, Namespaces, Events, ServiceAccounts, Roles, -RoleBindings, ClusterRoles, ClusterRoleBindings, NetworkPolicies, HPAs, PDBs. +### Format and edit -## Architecture Notes +- `:fmt yaml|json` or `:yaml` / `:json` +- `e` or `:edit [yaml|json]` -- `ResourceProvider` and `ActionExecutor` are stable internal interfaces. -- `StateDelta` is the boundary from cluster runtime to UI state. -- `ViewProjector` isolates table/detail projections from storage and rendering. -- Watch loops reconnect with exponential backoff and emit contextual error deltas. -- Context list is loaded from kubeconfig, but clients/watches are activated lazily on demand. -- Watchers are reconciled to the active tab/pane scope (context + kind + namespace) to avoid fan-out. -- One recently used context can be kept warm for fast tab switches (TTL-based). -- `--all-contexts` enables eager client warmup across contexts. -- `403 Forbidden` watchers are disabled per resource/context (no infinite retry flood). +## Architecture Overview -## Next Milestones +`krust` uses a layered architecture to isolate cluster IO, state mutation, projection, and rendering. -1. Add live pod logs streaming and resource-correlated event pane. -2. Expand mutation actions (scale/restart/apply/edit/port-forward/exec) with RBAC-aware UX. -3. Add integration tests against disposable clusters and load/perf benchmarks. -4. Harden CRD discovery and generic resource browsing. +```mermaid +flowchart LR + A["Kubernetes API\n(list/watch/logs/actions)"] --> B["ResourceProvider\nwatch + stream + action adapters"] + B --> C["StateDelta channel\n(bounded)"] + C --> D["StateStore\nnormalized entities + indexes"] + D --> E["ViewProjector\nactive-view projection"] + E --> F["TUI Renderer\ninvalidated draws + FPS cap"] + F --> G["Operator Input\nkeys, commands, filters"] + G --> E + G --> B +``` + +Design details are documented in: + +- [`docs/architecture.md`](docs/architecture.md) +- [`docs/performance.md`](docs/performance.md) + +## Performance Model (Operator-Facing) + +`krust` prioritizes consistent interactivity over maximum background data ingestion. + +- watch scope is reconciled to active context/pane needs +- recently used contexts can stay warm with TTL +- render loop is invalidation-driven with FPS cap +- logs are ring-buffered with source filtering and bounded drain +- forbidden RBAC watches are disabled for that context/resource (no retry floods) + +For deeper technical details and tuning guidance, see [`docs/performance.md`](docs/performance.md). + +## Safety Model + +- `--readonly` blocks mutating actions +- destructive action path is confirmation-guarded +- RBAC/action failures are surfaced with explicit status messaging +- secret decode editing converts decoded values back to base64 automatically + +## Troubleshooting + +### `403 Forbidden` warnings for some resources + +This is usually expected RBAC scope mismatch. `krust` disables forbidden watchers for that resource/context to avoid reconnect storms. + +### Long startup with many contexts + +Default mode is lazy. If startup still feels heavy, avoid `--all-contexts` and keep `warm_contexts` low. + +### Clipboard copy issues + +`krust` uses platform-native commands first (`pbcopy` / `wl-copy` / `xclip` / `xsel`) and falls back to OSC52 where supported. + +### Auth works in shell but not in app environment + +Ensure your terminal session has the same kube auth env/credentials as your shell where `kubectl` succeeds. + +## Contributing + +Contribution guide, PR standards, and performance review expectations live in [`CONTRIBUTING.md`](CONTRIBUTING.md). + +## Roadmap (Near Term) + +- wider mutation action set (scale/restart/etc.) with guardrails +- richer event correlation for non-Event resources +- expanded integration and load benchmarks +- continued k9s command/keybinding compatibility tightening + +## License + +MIT. See [`LICENSE`](LICENSE). diff --git a/docs/architecture.md b/docs/architecture.md new file mode 100644 index 0000000..1a63aed --- /dev/null +++ b/docs/architecture.md @@ -0,0 +1,146 @@ +# krust Architecture + +This document describes the current architecture and the design constraints behind it. + +## Design Intent + +`krust` is optimized for operators running large fleets: + +- many kubeconfig contexts +- large object counts per context +- frequent state churn + +The architecture is built to keep interaction latency stable under those conditions. + +## Layered Model + +`krust` is structured as four layers: + +1. Cluster IO +2. State cache +3. View projection +4. TUI renderer + +## 1) Cluster IO (`ResourceProvider`, `ActionExecutor`) + +Primary responsibilities: + +- initialize per-context Kubernetes clients lazily +- manage dynamic watch plans (`replace_watch_plan`) +- stream pod logs +- execute guarded mutation actions + +Notable behavior: + +- watchers are not started for every context/resource at startup +- watch plans are reconciled to active UI scope +- RBAC `403` watcher failures are treated as terminal for that resource/context +- exponential watch reconnect backoff is bounded and resets after healthy runs + +## 2) State Cache (`StateStore`) + +The cache stores normalized entities keyed by `(context, kind, namespace, name)` and keeps supporting indexes: + +- context/kind index for fast listing +- namespace index by context +- context-scoped error state +- monotonic revision counter + +The cache receives `StateDelta` messages (`Upsert`, `Remove`, `Reset`, `Error`) and applies them atomically. + +## 3) View Projection (`ViewProjector`) + +Projection transforms state cache data into active-view rows and detail content. + +Key properties: + +- projection is scoped to current `ViewRequest` (`context`, `kind`, `namespace`, `filter`, sort) +- cached projection is reused while request + state revision are unchanged +- avoids whole-state recompute on every keypress when inputs are stable + +## 4) TUI Renderer (`ratatui` + `crossterm`) + +Render strategy: + +- invalidation-driven draws +- frame cap (`fps_limit`) +- coalesced state/input events +- bounded scroll behavior for table/detail/log panes + +The UI can render table, describe, decode, events, and logs panes, plus selection overlays. + +## Watch Scope Reconciliation + +`replace_watch_plan(context, targets)` is the key boundary between UI intent and API load. + +Inputs: + +- active context +- active kind/pane requirements +- namespace scope +- warm-context retention policy + +Outputs: + +- start only missing required watchers +- stop watchers no longer needed +- retain watchers only for active and warm contexts + +This prevents default "watch everything" fan-out. + +## Warm Context Strategy + +To make tab switches fast without broad eager loading: + +- last-active contexts are tracked +- up to `warm_contexts` inactive contexts are retained +- retention expires after `warm_context_ttl_secs` + +## Logs Pipeline + +Logs are streamed per selected scope using pod log streams. + +Current behavior supports: + +- pod-level logs (all containers or selected container) +- deployment/replicaset fan-out to replica pod streams +- pause/resume, tail mode, latest jump +- source-level visibility filtering + +For stability: + +- bounded event drain per UI cycle +- bounded in-memory log buffer +- reconnect backoff for retryable stream failures + +## Detail Content and Editing + +Describe/decode/event detail views support: + +- YAML/JSON render format toggle +- syntax highlighting + search highlighting +- external editor mutation flow + +Mutation semantics: + +- describe edit applies raw manifest as entered +- secret decode edit expects decoded values, then re-encodes to `Secret.data` + +## Error Model + +Error handling is explicit and user-visible: + +- watcher errors are emitted as `StateDelta::Error` +- RBAC/action failures are surfaced in status line with context +- read-only mode blocks mutation actions early + +## Internal Stable Interfaces + +The code is intentionally organized around interface seams: + +- `ResourceProvider` +- `ActionExecutor` +- `ViewProjector` +- `StateDelta` + +These boundaries make it possible to evolve behavior while preserving operational semantics. diff --git a/docs/performance.md b/docs/performance.md new file mode 100644 index 0000000..822820a --- /dev/null +++ b/docs/performance.md @@ -0,0 +1,103 @@ +# krust Performance Guide + +This document explains how `krust` keeps latency predictable and how to reason about performance-sensitive changes. + +## Performance Priorities + +In order: + +1. Input responsiveness +2. Controlled API pressure +3. Bounded memory behavior +4. Visual stability (no redraw thrash) + +`krust` intentionally does not optimize for maximum background data breadth by default. + +## Core Performance Mechanisms + +### Lazy activation by default + +- clients are initialized lazily per context +- watchers are activated from the current watch plan +- inactive contexts are not fully watched unless retained via warm-context policy + +This avoids `contexts x resources x namespaces` startup explosion. + +### Watch lifecycle controls + +- dynamic watch-plan reconciliation (`replace_watch_plan`) +- bounded exponential reconnect backoff +- terminal handling for RBAC `403` watcher failures + +Result: fewer reconnect storms and less API/server noise. + +### Projection and render controls + +- state cache revision tracking +- cached projection reuse when request + revision unchanged +- invalidation-based render loop with FPS cap + +Result: no unconditional full redraw at fixed ticks. + +### Log-path controls + +- bounded line/byte buffer +- bounded per-cycle drain +- per-source filtering without restarting all streams +- tail/pause behavior to reduce churn during operator inspection + +Result: stable log UX under high stream volume. + +## Configuration Knobs + +`~/.config/krust/config.toml` + +```toml +[runtime] +fps_limit = 60 +delta_channel_capacity = 2048 +warm_contexts = 1 +warm_context_ttl_secs = 20 +``` + +Tuning suggestions: + +- many contexts: keep `warm_contexts` low (`0-2`) +- bursty clusters: raise `delta_channel_capacity` carefully +- low-power terminals: reduce `fps_limit` (for example `30`) + +## Operational Recommendations + +- default startup: use lazy mode (`krust`) +- only use `--all-contexts` when eager auth/client warmup is explicitly desired +- scope namespace when debugging dense clusters +- in logs, pause or source-filter when hunting specific signals + +## Benchmark and Validation Hooks + +`krust` includes perf-focused test hooks (some ignored by default) and should always pass full validation: + +```bash +cargo fmt --all +cargo check +cargo test +cargo build --release +``` + +You can run ignored perf tests explicitly when profiling local changes: + +```bash +cargo test -- --ignored --nocapture +``` + +## Change Review Checklist (Perf-Sensitive PRs) + +When changing watches, rendering, projection, or logs, include: + +- hot path changed +- expected complexity/cost change +- API pressure impact +- memory impact and bounds +- potential regressions and how to observe them + +This keeps performance discussion concrete and reviewable. diff --git a/src/cli.rs b/src/cli.rs index 76566b9..fabaf31 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -4,7 +4,7 @@ use clap::Parser; #[derive(Debug, Clone, Parser)] #[command( - name = "krs", + name = "krust", version, about = "High-performance Kubernetes terminal navigator" )] diff --git a/src/cluster/kube_provider.rs b/src/cluster/kube_provider.rs index 6a34953..f963d5f 100644 --- a/src/cluster/kube_provider.rs +++ b/src/cluster/kube_provider.rs @@ -26,8 +26,9 @@ use k8s_openapi::{ }; use kube::{ Api, Client, Config, Resource, ResourceExt, - api::{DeleteParams, LogParams}, + api::{DeleteParams, LogParams, PostParams}, config::{KubeConfigOptions, Kubeconfig}, + core::{ApiResource, DynamicObject, GroupVersionKind}, }; use kube_runtime::watcher::{self, Event}; use serde::Serialize; @@ -46,7 +47,6 @@ pub struct KubeProviderOptions { pub kubeconfig: Option, pub context: Option, pub all_contexts: bool, - pub namespace: Option, pub readonly: bool, pub warm_contexts: usize, pub warm_context_ttl_secs: u64, @@ -172,6 +172,15 @@ impl KubeResourceProvider { clients.insert(context.to_string(), client.clone()); Ok(client) } + + pub fn default_namespace_for_context(&self, context: &str) -> Option { + self.kubeconfig + .contexts + .iter() + .find(|named| named.name == context) + .and_then(|named| named.context.as_ref()) + .and_then(|ctx| ctx.namespace.clone()) + } } #[async_trait] @@ -239,10 +248,8 @@ impl ResourceProvider for KubeResourceProvider { } else { warm_contexts.contains(&key.context) }; - if !keep_existing { - if let Some(task) = watched.remove(&key) { - task.abort(); - } + if !keep_existing && let Some(task) = watched.remove(&key) { + task.abort(); } } @@ -367,6 +374,228 @@ impl ActionExecutor for KubeResourceProvider { ))), } } + + async fn replace_resource( + &self, + key: &ResourceKey, + manifest: serde_json::Value, + ) -> Result { + if self.readonly { + return Err(ActionError::ReadOnly); + } + + let client = self + .client_for_context(&key.context) + .await + .map_err(|err| ActionError::Failed(err.to_string()))?; + + let spec = api_resource_spec_for_kind(key.kind).ok_or_else(|| { + ActionError::Unsupported(format!("replace {} is not implemented yet", key.kind)) + })?; + let gvk = GroupVersionKind::gvk(spec.group, spec.version, spec.kind); + let mut ar = ApiResource::from_gvk(&gvk); + ar.plural = spec.plural.to_string(); + let obj: DynamicObject = serde_json::from_value(manifest) + .map_err(|err| ActionError::Failed(format!("invalid manifest: {err}")))?; + + if spec.namespaced { + let namespace = key.namespace.as_deref().unwrap_or("default"); + let api: Api = Api::namespaced_with(client, namespace, &ar); + api.replace(&key.name, &PostParams::default(), &obj) + .await + .map_err(map_kube_error)?; + } else { + let api: Api = Api::all_with(client, &ar); + api.replace(&key.name, &PostParams::default(), &obj) + .await + .map_err(map_kube_error)?; + } + + Ok(ActionResult { + message: format!("{} {} updated", key.kind.short_name(), key.name), + }) + } +} + +struct ApiResourceSpec { + group: &'static str, + version: &'static str, + kind: &'static str, + plural: &'static str, + namespaced: bool, +} + +fn api_resource_spec_for_kind(kind: ResourceKind) -> Option { + Some(match kind { + ResourceKind::Pods => ApiResourceSpec { + group: "", + version: "v1", + kind: "Pod", + plural: "pods", + namespaced: true, + }, + ResourceKind::Deployments => ApiResourceSpec { + group: "apps", + version: "v1", + kind: "Deployment", + plural: "deployments", + namespaced: true, + }, + ResourceKind::ReplicaSets => ApiResourceSpec { + group: "apps", + version: "v1", + kind: "ReplicaSet", + plural: "replicasets", + namespaced: true, + }, + ResourceKind::StatefulSets => ApiResourceSpec { + group: "apps", + version: "v1", + kind: "StatefulSet", + plural: "statefulsets", + namespaced: true, + }, + ResourceKind::DaemonSets => ApiResourceSpec { + group: "apps", + version: "v1", + kind: "DaemonSet", + plural: "daemonsets", + namespaced: true, + }, + ResourceKind::Services => ApiResourceSpec { + group: "", + version: "v1", + kind: "Service", + plural: "services", + namespaced: true, + }, + ResourceKind::Ingresses => ApiResourceSpec { + group: "networking.k8s.io", + version: "v1", + kind: "Ingress", + plural: "ingresses", + namespaced: true, + }, + ResourceKind::ConfigMaps => ApiResourceSpec { + group: "", + version: "v1", + kind: "ConfigMap", + plural: "configmaps", + namespaced: true, + }, + ResourceKind::Secrets => ApiResourceSpec { + group: "", + version: "v1", + kind: "Secret", + plural: "secrets", + namespaced: true, + }, + ResourceKind::Jobs => ApiResourceSpec { + group: "batch", + version: "v1", + kind: "Job", + plural: "jobs", + namespaced: true, + }, + ResourceKind::CronJobs => ApiResourceSpec { + group: "batch", + version: "v1", + kind: "CronJob", + plural: "cronjobs", + namespaced: true, + }, + ResourceKind::PersistentVolumeClaims => ApiResourceSpec { + group: "", + version: "v1", + kind: "PersistentVolumeClaim", + plural: "persistentvolumeclaims", + namespaced: true, + }, + ResourceKind::PersistentVolumes => ApiResourceSpec { + group: "", + version: "v1", + kind: "PersistentVolume", + plural: "persistentvolumes", + namespaced: false, + }, + ResourceKind::Nodes => ApiResourceSpec { + group: "", + version: "v1", + kind: "Node", + plural: "nodes", + namespaced: false, + }, + ResourceKind::Namespaces => ApiResourceSpec { + group: "", + version: "v1", + kind: "Namespace", + plural: "namespaces", + namespaced: false, + }, + ResourceKind::Events => ApiResourceSpec { + group: "", + version: "v1", + kind: "Event", + plural: "events", + namespaced: true, + }, + ResourceKind::ServiceAccounts => ApiResourceSpec { + group: "", + version: "v1", + kind: "ServiceAccount", + plural: "serviceaccounts", + namespaced: true, + }, + ResourceKind::Roles => ApiResourceSpec { + group: "rbac.authorization.k8s.io", + version: "v1", + kind: "Role", + plural: "roles", + namespaced: true, + }, + ResourceKind::RoleBindings => ApiResourceSpec { + group: "rbac.authorization.k8s.io", + version: "v1", + kind: "RoleBinding", + plural: "rolebindings", + namespaced: true, + }, + ResourceKind::ClusterRoles => ApiResourceSpec { + group: "rbac.authorization.k8s.io", + version: "v1", + kind: "ClusterRole", + plural: "clusterroles", + namespaced: false, + }, + ResourceKind::ClusterRoleBindings => ApiResourceSpec { + group: "rbac.authorization.k8s.io", + version: "v1", + kind: "ClusterRoleBinding", + plural: "clusterrolebindings", + namespaced: false, + }, + ResourceKind::NetworkPolicies => ApiResourceSpec { + group: "networking.k8s.io", + version: "v1", + kind: "NetworkPolicy", + plural: "networkpolicies", + namespaced: true, + }, + ResourceKind::HorizontalPodAutoscalers => ApiResourceSpec { + group: "autoscaling", + version: "v2", + kind: "HorizontalPodAutoscaler", + plural: "horizontalpodautoscalers", + namespaced: true, + }, + ResourceKind::PodDisruptionBudgets => ApiResourceSpec { + group: "policy", + version: "v1", + kind: "PodDisruptionBudget", + plural: "poddisruptionbudgets", + namespaced: true, + }, + }) } fn load_kubeconfig(path: Option<&PathBuf>) -> anyhow::Result { diff --git a/src/cluster/mod.rs b/src/cluster/mod.rs index 7f1f7e8..54054a9 100644 --- a/src/cluster/mod.rs +++ b/src/cluster/mod.rs @@ -75,4 +75,9 @@ pub trait ResourceProvider: Send + Sync { #[async_trait] pub trait ActionExecutor: Send + Sync { async fn delete_resource(&self, key: &ResourceKey) -> Result; + async fn replace_resource( + &self, + key: &ResourceKey, + manifest: serde_json::Value, + ) -> Result; } diff --git a/src/config.rs b/src/config.rs index 6d768a3..a954a44 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,22 +3,13 @@ use std::{fs, path::PathBuf}; use anyhow::Context; use serde::{Deserialize, Serialize}; -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize, Default)] #[serde(default)] pub struct AppConfig { pub runtime: RuntimeConfig, pub ui: UiConfig, } -impl Default for AppConfig { - fn default() -> Self { - Self { - runtime: RuntimeConfig::default(), - ui: UiConfig::default(), - } - } -} - impl AppConfig { pub fn load() -> anyhow::Result { let path = default_config_path(); @@ -39,8 +30,6 @@ impl AppConfig { pub struct RuntimeConfig { pub fps_limit: u16, pub delta_channel_capacity: usize, - pub namespace: Option, - pub default_context: Option, pub warm_contexts: usize, pub warm_context_ttl_secs: u64, } @@ -50,8 +39,6 @@ impl Default for RuntimeConfig { Self { fps_limit: 60, delta_channel_capacity: 2048, - namespace: None, - default_context: None, warm_contexts: 1, warm_context_ttl_secs: 20, } @@ -76,14 +63,14 @@ impl Default for UiConfig { pub fn default_config_path() -> PathBuf { let mut base = dirs::config_dir().unwrap_or_else(|| PathBuf::from(".")); - base.push("krs"); + base.push("krust"); base.push("config.toml"); base } pub fn default_keymap_path() -> PathBuf { let mut base = dirs::config_dir().unwrap_or_else(|| PathBuf::from(".")); - base.push("krs"); + base.push("krust"); base.push("keymap.toml"); base } diff --git a/src/lib.rs b/src/lib.rs index faa655b..de23fa6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,10 +31,6 @@ pub async fn run() -> anyhow::Result<()> { kubeconfig: cli.kubeconfig.clone(), context: cli.context.clone(), all_contexts: cli.all_contexts, - namespace: cli - .namespace - .clone() - .or(app_config.runtime.namespace.clone()), readonly: cli.readonly, warm_contexts: app_config.runtime.warm_contexts, warm_context_ttl_secs: app_config.runtime.warm_context_ttl_secs, @@ -44,14 +40,24 @@ pub async fn run() -> anyhow::Result<()> { ); let contexts = provider.context_names().to_vec(); + let context_default_namespaces = contexts + .iter() + .map(|context| { + ( + context.clone(), + provider.default_namespace_for_context(context.as_str()), + ) + }) + .collect(); let initial_context = cli .context - .or(app_config.runtime.default_context) .or_else(|| provider.default_context().map(str::to_string)) .or_else(|| contexts.first().cloned()) .context("could not determine initial context")?; - let initial_namespace = cli.namespace.or(app_config.runtime.namespace); + let initial_namespace = cli + .namespace + .or_else(|| provider.default_namespace_for_context(&initial_context)); let (tx, rx) = mpsc::channel(app_config.runtime.delta_channel_capacity); provider.start(tx).await?; @@ -63,6 +69,7 @@ pub async fn run() -> anyhow::Result<()> { contexts, initial_context, initial_namespace, + context_default_namespaces, rx, action_executor, resource_provider, diff --git a/src/main.rs b/src/main.rs index 86e8f9c..363ee76 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ #[tokio::main] async fn main() -> anyhow::Result<()> { - krs::run().await + krust::run().await } diff --git a/src/model.rs b/src/model.rs index f024be4..f02c8d3 100644 --- a/src/model.rs +++ b/src/model.rs @@ -199,6 +199,7 @@ pub struct PendingConfirmation { pub enum Pane { Table, Describe, + SecretDecode, Events, Logs, } diff --git a/src/ui/app.rs b/src/ui/app.rs index f77b933..8b9e4c6 100644 --- a/src/ui/app.rs +++ b/src/ui/app.rs @@ -1,6 +1,9 @@ use std::{ - collections::{HashMap, HashSet, VecDeque}, - env, io, + collections::{BTreeMap, HashMap, HashSet, VecDeque}, + env, fs, + io::{self, Write}, + path::PathBuf, + process::{Command, Stdio}, sync::Arc, time::{Duration, Instant}, }; @@ -45,10 +48,13 @@ struct ContextTabState { namespace: Option, filter: String, detail_filter: String, + detail_active_match_line: Option, selected: usize, + table_offset: usize, detail_scroll: u16, detail_hscroll: u16, detail_wrap: bool, + detail_format: DetailFormat, kind_idx: usize, last_non_namespace_kind_idx: usize, sort: SortColumn, @@ -68,6 +74,33 @@ enum CommandMode { Filter, } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum DetailFormat { + Yaml, + Json, +} + +impl DetailFormat { + fn label(self) -> &'static str { + match self { + DetailFormat::Yaml => "yaml", + DetailFormat::Json => "json", + } + } + + fn extension(self) -> &'static str { + self.label() + } + + fn parse(token: &str) -> Option { + match token.to_ascii_lowercase().as_str() { + "yaml" | "yml" => Some(DetailFormat::Yaml), + "json" => Some(DetailFormat::Json), + _ => None, + } + } +} + #[derive(Debug, Clone)] struct CommandInput { mode: CommandMode, @@ -120,10 +153,12 @@ enum Overlay { }, } +#[allow(clippy::too_many_arguments)] pub async fn run( contexts: Vec, initial_context: String, initial_namespace: Option, + context_default_namespaces: HashMap>, mut delta_rx: mpsc::Receiver, action_executor: Arc, resource_provider: Arc, @@ -146,6 +181,7 @@ pub async fn run( contexts, initial_context, initial_namespace, + context_default_namespaces, action_executor, resource_provider, keymap, @@ -742,7 +778,11 @@ impl App { if tab.pane == Pane::Table { tab.filter = filter; } else { + let changed = tab.detail_filter != filter; tab.detail_filter = filter; + if changed { + tab.detail_active_match_line = None; + } } } @@ -754,6 +794,30 @@ impl App { true } + fn active_filter_label(&self) -> &'static str { + if let Some(overlay) = &self.overlay { + return match overlay { + Overlay::Contexts { .. } + | Overlay::Containers { .. } + | Overlay::LogSources { .. } => "Filter", + Overlay::Text { .. } => "Search", + }; + } + if self.current_tab().pane == Pane::Table { + "Filter" + } else { + "Search" + } + } + + fn filter_status_message(&self, value: &str) -> String { + if value.is_empty() { + format!("{} cleared", self.active_filter_label()) + } else { + format!("{} set: {value}", self.active_filter_label()) + } + } + fn pod_container_names_for_key(&self, pod_key: &ResourceKey) -> Vec { let Some(entity) = self.store.get(pod_key) else { return Vec::new(); @@ -789,15 +853,15 @@ impl App { containers.sort(); containers.dedup(); - if let Some(target_container) = container_override { - if containers.iter().any(|name| name == target_container) { - return vec![LogTarget { - context: pod_key.context.clone(), - namespace, - pod: pod_key.name.clone(), - container: Some(target_container.to_string()), - }]; - } + if let Some(target_container) = container_override + && containers.iter().any(|name| name == target_container) + { + return vec![LogTarget { + context: pod_key.context.clone(), + namespace, + pod: pod_key.name.clone(), + container: Some(target_container.to_string()), + }]; } if containers.is_empty() { @@ -1408,10 +1472,12 @@ impl App { matches } + #[allow(clippy::too_many_arguments)] fn new( contexts: Vec, initial_context: String, initial_namespace: Option, + context_default_namespaces: HashMap>, action_executor: Arc, resource_provider: Arc, keymap: Keymap, @@ -1423,16 +1489,21 @@ impl App { .map(|context| ContextTabState { context: context.clone(), namespace: if context == &initial_context { - initial_namespace.clone() + initial_namespace + .clone() + .or_else(|| context_default_namespaces.get(context).cloned().flatten()) } else { - None + context_default_namespaces.get(context).cloned().flatten() }, filter: String::new(), detail_filter: String::new(), + detail_active_match_line: None, selected: 0, + table_offset: 0, detail_scroll: 0, detail_hscroll: 0, detail_wrap: true, + detail_format: DetailFormat::Yaml, kind_idx: 0, last_non_namespace_kind_idx: 0, sort: SortColumn::Name, @@ -1546,7 +1617,7 @@ impl App { { let existing_filter = self.active_filter_value(); self.command_input = Some(CommandInput::new(CommandMode::Filter, existing_filter)); - self.status_line = "Filter mode".to_string(); + self.status_line = format!("{} mode", self.active_filter_label()); return Ok(false); } } @@ -1560,6 +1631,15 @@ impl App { self.status_line = "Command mode".to_string(); return Ok(false); } + if key.code == KeyCode::Char('y') + && key.modifiers.is_empty() + && self.pending_confirmation.is_none() + && self.in_detail_pane() + && self.overlay.is_none() + { + self.copy_current_view_to_clipboard(); + return Ok(false); + } if key.code == KeyCode::Enter { self.handle_enter_key(); self.ensure_active_watch().await; @@ -1618,13 +1698,19 @@ impl App { self.current_tab_mut().pane = Pane::Describe; self.current_tab_mut().detail_scroll = 0; self.current_tab_mut().detail_hscroll = 0; + self.current_tab_mut().detail_format = DetailFormat::Yaml; self.overlay = None; - self.status_line = "View opened".to_string(); + self.status_line = "View opened (yaml)".to_string(); self.ensure_active_watch().await; return Ok(false); } if key.modifiers.is_empty() && key.code == KeyCode::Char('e') { - self.status_line = "Edit action is not implemented yet".to_string(); + self.edit_current_view(None).await; + self.ensure_active_watch().await; + return Ok(false); + } + if key.modifiers.is_empty() && key.code == KeyCode::Char('x') { + self.toggle_secret_decode(); self.ensure_active_watch().await; return Ok(false); } @@ -1720,7 +1806,7 @@ impl App { } else if self.keymap.is(Action::FilterMode, &key) { let existing_filter = self.active_filter_value(); self.command_input = Some(CommandInput::new(CommandMode::Filter, existing_filter)); - self.status_line = "Filter mode".to_string(); + self.status_line = format!("{} mode", self.active_filter_label()); } else if self.keymap.is(Action::CycleSort, &key) { if self.current_tab().pane == Pane::Logs { self.logs.auto_scroll = !self.logs.auto_scroll; @@ -1782,7 +1868,7 @@ impl App { self.overlay = None; self.status_line = "Closed view".to_string(); } else if self.clear_active_filter_value() { - self.status_line = "Filter cleared".to_string(); + self.status_line = self.filter_status_message(""); } else { self.status_line = "Nothing to cancel".to_string(); } @@ -1848,16 +1934,12 @@ impl App { match tab.pane { Pane::Table => None, Pane::Logs => Some(self.log_body_text()), - Pane::Describe | Pane::Events => { + Pane::Describe | Pane::SecretDecode | Pane::Events => { let request = self.view_request_for_tab(&tab); let vm = self.projected_view(&request); let selected = tab.selected.min(vm.rows.len().saturating_sub(1)); - let raw = self.detail_text(&vm.rows, selected, tab.pane); - if tab.detail_filter.trim().is_empty() { - Some(raw) - } else { - Some(filter_text_lines(&raw, &tab.detail_filter)) - } + let raw = self.detail_text(&vm.rows, selected, tab.pane, tab.detail_format); + Some(raw) } } } @@ -1895,23 +1977,18 @@ impl App { return; } - let current = self.current_tab().detail_scroll as usize; - let target = if forward { - matches - .iter() - .copied() - .find(|idx| *idx > current) - .unwrap_or(matches[0]) - } else { - matches - .iter() - .copied() - .rev() - .find(|idx| *idx < current) - .unwrap_or(*matches.last().unwrap_or(&matches[0])) + let current_line = self + .current_tab() + .detail_active_match_line + .filter(|line| matches.contains(line)) + .unwrap_or(self.current_tab().detail_scroll as usize); + let Some((target, match_pos)) = step_match_line(&matches, current_line, forward) else { + self.status_line = format!("No matches for '{}'", needle.trim()); + return; }; - let match_pos = matches.iter().position(|idx| *idx == target).unwrap_or(0) + 1; - self.current_tab_mut().detail_scroll = target.min(u16::MAX as usize) as u16; + let tab = self.current_tab_mut(); + tab.detail_scroll = target.min(u16::MAX as usize) as u16; + tab.detail_active_match_line = Some(target); if self.current_tab().pane == Pane::Logs { self.logs.auto_scroll = false; } @@ -1950,15 +2027,15 @@ impl App { if let Some(input) = self.command_input.take() { if matches!(input.mode, CommandMode::Filter) { if self.clear_active_filter_value() { - self.status_line = "Filter cleared".to_string(); + self.status_line = self.filter_status_message(""); } else { - self.status_line = "Filter canceled".to_string(); + self.status_line = format!("{} canceled", self.active_filter_label()); } } else { self.status_line = "Command canceled".to_string(); } } - return Ok(false); + Ok(false) } KeyCode::Backspace => { if let Some(input) = &mut self.command_input { @@ -1966,14 +2043,10 @@ impl App { if matches!(input.mode, CommandMode::Filter) { let filter = input.value.trim().to_string(); self.set_active_filter_value(filter.clone()); - self.status_line = if filter.is_empty() { - "Filter cleared".to_string() - } else { - format!("Filter set: {filter}") - }; + self.status_line = self.filter_status_message(&filter); } } - return Ok(false); + Ok(false) } KeyCode::Char('w') if key.modifiers.contains(KeyModifiers::CONTROL) => { if let Some(input) = &mut self.command_input { @@ -1981,24 +2054,20 @@ impl App { if matches!(input.mode, CommandMode::Filter) { let filter = input.value.trim().to_string(); self.set_active_filter_value(filter.clone()); - self.status_line = if filter.is_empty() { - "Filter cleared".to_string() - } else { - format!("Filter set: {filter}") - }; + self.status_line = self.filter_status_message(&filter); } } - return Ok(false); + Ok(false) } KeyCode::Tab => { self.autocomplete_command_input(); - return Ok(false); + Ok(false) } KeyCode::Enter => { let Some(input) = self.command_input.take() else { return Ok(false); }; - return Ok(self.execute_command_input(input).await); + Ok(self.execute_command_input(input).await) } KeyCode::Char(ch) => { if let Some(input) = &mut self.command_input { @@ -2006,16 +2075,12 @@ impl App { if matches!(input.mode, CommandMode::Filter) { let filter = input.value.trim().to_string(); self.set_active_filter_value(filter.clone()); - self.status_line = if filter.is_empty() { - "Filter cleared".to_string() - } else { - format!("Filter set: {filter}") - }; + self.status_line = self.filter_status_message(&filter); } } - return Ok(false); + Ok(false) } - _ => return Ok(false), + _ => Ok(false), } } @@ -2024,11 +2089,7 @@ impl App { CommandMode::Filter => { let filter = input.value.trim().to_string(); self.set_active_filter_value(filter.clone()); - self.status_line = if filter.is_empty() { - "Filter cleared".to_string() - } else { - format!("Filter set: {filter}") - }; + self.status_line = self.filter_status_message(&filter); false } CommandMode::Command => self.execute_colon_command(input.value.trim()).await, @@ -2050,7 +2111,7 @@ impl App { match cmd.as_str() { "q" | "quit" | "exit" => true, "help" | "?" => { - self.status_line = "Commands: :ctx [name] | :ns [name|all] | :kind | :c | :sources | :pause | :resume | :tail | :resources | :clear | :quit | :all".to_string(); + self.status_line = "Commands: :ctx [name] | :ns [name|all] | :kind | :fmt [yaml|json] | :c | :sources | :pause | :resume | :edit [yaml|json] | :tail | :copy | :dump | :resources | :clear | :quit | :all".to_string(); false } "contexts" | "ctxs" => { @@ -2121,7 +2182,39 @@ impl App { } else { self.current_tab_mut().detail_filter.clear(); } - self.status_line = "Filter cleared".to_string(); + self.status_line = self.filter_status_message(""); + false + } + "fmt" | "format" => { + if args.is_empty() { + self.status_line = format!( + "Detail format: {}", + self.current_tab().detail_format.label() + ); + return false; + } + let Some(format) = DetailFormat::parse(args[0]) else { + self.status_line = "Usage: :fmt ".to_string(); + return false; + }; + self.current_tab_mut().detail_format = format; + self.current_tab_mut().detail_scroll = 0; + self.current_tab_mut().detail_hscroll = 0; + self.status_line = format!("Detail format: {}", format.label()); + false + } + "yaml" | "yml" => { + self.current_tab_mut().detail_format = DetailFormat::Yaml; + self.current_tab_mut().detail_scroll = 0; + self.current_tab_mut().detail_hscroll = 0; + self.status_line = "Detail format: yaml".to_string(); + false + } + "json" => { + self.current_tab_mut().detail_format = DetailFormat::Json; + self.current_tab_mut().detail_scroll = 0; + self.current_tab_mut().detail_hscroll = 0; + self.status_line = "Detail format: json".to_string(); false } "c" | "container" | "containers" => { @@ -2140,12 +2233,31 @@ impl App { self.set_log_paused(false); false } + "edit" => { + if let Some(token) = args.first() { + let Some(format) = DetailFormat::parse(token) else { + self.status_line = "Usage: :edit [yaml|json]".to_string(); + return false; + }; + self.edit_current_view(Some(format)).await; + } else { + self.edit_current_view(None).await; + } + false + } "tail" => { self.jump_logs_to_latest(); false } - "pulse" | "pulses" | "pu" | "xray" | "popeye" | "pop" | "plugins" | "plugin" - | "screendump" | "sd" => { + "copy" | "yank" => { + self.copy_current_view_to_clipboard(); + false + } + "dump" | "screendump" | "sd" => { + self.dump_current_view(args); + false + } + "pulse" | "pulses" | "pu" | "xray" | "popeye" | "pop" | "plugins" | "plugin" => { self.status_line = format!("Command ':{cmd}' is recognized but not implemented yet"); false @@ -2228,6 +2340,15 @@ impl App { .collect(); self.apply_completion_for_argument(&command, candidates, &prefix); } + "fmt" | "format" | "edit" => { + let prefix = arg_prefix.to_ascii_lowercase(); + let candidates: Vec = ["yaml", "yml", "json"] + .iter() + .filter(|fmt| fmt.starts_with(&prefix)) + .map(|fmt| (*fmt).to_string()) + .collect(); + self.apply_completion_for_argument(&command, candidates, &prefix); + } _ => { self.status_line = "No autocomplete candidates".to_string(); } @@ -2327,9 +2448,7 @@ impl App { } fn history_step_forward(&mut self) -> Option { - let Some(current) = self.history_cursor else { - return None; - }; + let current = self.history_cursor?; if current + 1 >= self.command_history.len() { self.history_cursor = None; return None; @@ -2382,15 +2501,7 @@ impl App { } else { Some(ns.to_string()) }; - } else if arg == "-n" && idx + 1 < args.len() { - idx += 1; - let ns = args[idx]; - self.current_tab_mut().namespace = if ns.eq_ignore_ascii_case("all") { - None - } else { - Some(ns.to_string()) - }; - } else if arg == "--namespace" && idx + 1 < args.len() { + } else if (arg == "-n" || arg == "--namespace") && idx + 1 < args.len() { idx += 1; let ns = args[idx]; self.current_tab_mut().namespace = if ns.eq_ignore_ascii_case("all") { @@ -2493,7 +2604,8 @@ impl App { "pvc|claim, pv, no|node, ns|namespace, ev|event, sa, role, rb".to_string(), "crole, crb, netpol|np, hpa, pdb".to_string(), String::new(), - "Commands: :ctx :ns :kind :c :resources :clear :quit".to_string(), + "Commands: :ctx :ns :kind :fmt :c :sources :edit :copy :dump :resources :clear :quit" + .to_string(), String::new(), "Examples:".to_string(), ":po".to_string(), @@ -2517,6 +2629,165 @@ impl App { self.status_line = "Resource aliases opened".to_string(); } + fn current_view_text(&mut self) -> Option { + if let Some(overlay) = &self.overlay { + return match overlay { + Overlay::Text { lines, .. } => Some(lines.join("\n")), + Overlay::Contexts { + contexts, + selected, + filter, + .. + } => { + let filtered = context_filtered_indices(contexts, filter); + let mut out = vec!["context".to_string()]; + for idx in filtered { + let marker = if idx == *selected { "*" } else { " " }; + out.push(format!("{marker} {}", contexts[idx])); + } + Some(out.join("\n")) + } + Overlay::Containers { + containers, + selected, + filter, + .. + } => { + let filtered = list_filtered_indices(containers, filter); + let mut out = vec!["container".to_string()]; + for idx in filtered { + let marker = if idx == *selected { "*" } else { " " }; + out.push(format!("{marker} {}", containers[idx])); + } + Some(out.join("\n")) + } + Overlay::LogSources { + sources, + selected, + filter, + .. + } => { + let filtered = list_filtered_indices(sources, filter); + let mut out = vec!["use\tsource".to_string()]; + for idx in filtered { + let marker = if self.logs.hidden_sources.contains(&sources[idx]) { + "off" + } else { + "on" + }; + let active = if idx == *selected { "*" } else { " " }; + out.push(format!("{active}{marker}\t{}", sources[idx])); + } + Some(out.join("\n")) + } + }; + } + + let active = self.current_tab().clone(); + match active.pane { + Pane::Table => { + let request = self.view_request_for_tab(&active); + let vm = self.projected_view(&request); + let mut lines = Vec::with_capacity(vm.rows.len().saturating_add(1)); + lines.push("namespace\tname\tstatus\tage\tsummary".to_string()); + for row in &vm.rows { + lines.push(format!( + "{}\t{}\t{}\t{}\t{}", + row.namespace, row.name, row.status, row.age, row.summary + )); + } + Some(lines.join("\n")) + } + Pane::Describe | Pane::SecretDecode | Pane::Events => { + let request = self.view_request_for_tab(&active); + let vm = self.projected_view(&request); + let selected = active.selected.min(vm.rows.len().saturating_sub(1)); + let raw = self.detail_text(&vm.rows, selected, active.pane, active.detail_format); + Some(raw) + } + Pane::Logs => { + let text = if self.logs.hidden_sources.is_empty() { + self.log_joined_text().to_string() + } else { + self.filtered_log_body_text() + }; + if text.is_empty() { + Some(self.log_body_text()) + } else { + Some(text) + } + } + } + } + + fn copy_current_view_to_clipboard(&mut self) { + let Some(raw) = self.current_view_text() else { + self.status_line = "Nothing to copy".to_string(); + return; + }; + if raw.is_empty() { + self.status_line = "Nothing to copy".to_string(); + return; + } + + let (payload, truncated) = truncate_for_clipboard(&raw, 1_000_000); + match copy_to_native_clipboard(&payload) { + Ok(method) => { + self.status_line = if truncated { + format!( + "Copied to clipboard via {method} (truncated to {} bytes)", + payload.len() + ) + } else { + format!("Copied to clipboard via {method}") + }; + } + Err(native_err) => { + let encoded = base64_encode(payload.as_bytes()); + let osc52 = format!("\u{1b}]52;c;{encoded}\u{7}"); + match io::stdout() + .write_all(osc52.as_bytes()) + .and_then(|_| io::stdout().flush()) + { + Ok(_) => { + self.status_line = if truncated { + format!( + "Copied via OSC52 (native unavailable: {native_err}; truncated to {} bytes)", + payload.len() + ) + } else { + format!("Copied via OSC52 (native unavailable: {native_err})") + }; + } + Err(err) => { + self.status_line = + format!("Clipboard copy failed: native={native_err}; osc52={err}"); + } + } + } + }; + } + + fn dump_current_view(&mut self, args: &[&str]) { + let Some(path_raw) = args.first() else { + self.status_line = "Usage: :dump ".to_string(); + return; + }; + let Some(text) = self.current_view_text() else { + self.status_line = "Nothing to dump".to_string(); + return; + }; + let path = expand_user_path(path_raw); + match fs::write(&path, text) { + Ok(_) => { + self.status_line = format!("Dumped view to {}", path.display()); + } + Err(err) => { + self.status_line = format!("Dump failed: {}", err); + } + } + } + async fn handle_overlay_key(&mut self, key: KeyEvent) { match key.code { KeyCode::Esc => { @@ -2526,7 +2797,7 @@ impl App { KeyCode::Char('/') => { let existing_filter = self.active_filter_value(); self.command_input = Some(CommandInput::new(CommandMode::Filter, existing_filter)); - self.status_line = "Filter mode".to_string(); + self.status_line = format!("{} mode", self.active_filter_label()); } KeyCode::Char('w') => { if let Some(Overlay::Text { wrap, hscroll, .. }) = &mut self.overlay { @@ -2620,17 +2891,16 @@ impl App { if let Some(Overlay::LogSources { selected, sources, .. }) = &self.overlay + && *selected < sources.len() { - if *selected < sources.len() { - let source = sources[*selected].clone(); - if self.toggle_log_source(&source) { - let hidden = self.logs.hidden_sources.contains(&source); - self.status_line = if hidden { - format!("Log source hidden: {source}") - } else { - format!("Log source shown: {source}") - }; - } + let source = sources[*selected].clone(); + if self.toggle_log_source(&source) { + let hidden = self.logs.hidden_sources.contains(&source); + self.status_line = if hidden { + format!("Log source hidden: {source}") + } else { + format!("Log source shown: {source}") + }; } } } @@ -2890,12 +3160,13 @@ impl App { let vm = self.projected_view(&request); let visible_rows = vm.rows.len(); let max_selection = visible_rows.saturating_sub(1); - let selected = active.selected.min(max_selection); + let mut selected = active.selected.min(max_selection); self.current_tab_mut().selected = selected; let pane_label = match active.pane { Pane::Table => "table", Pane::Describe => "describe", + Pane::SecretDecode => "decode", Pane::Events => "events", Pane::Logs => "logs", }; @@ -3222,7 +3493,7 @@ impl App { .map(|(tag, body, _)| format!("{tag} {body}")) .collect::>() .join("\n"); - let help_text = "ctrl+c quit | : command | / ? filter/search | n/N next/prev | gg/G top/bottom | ctrl+d/u half-page | [ ] history | - repeat | ctrl+a aliases | tab switch-ctx | j/k move/scroll | left/right h-scroll (wrap off) | w wrap toggle | d describe | l logs(stream) | s tail on/off | p pause/resume logs | S sources | L latest | c container picker | ctrl+d delete(table) | ctrl+k kill"; + let help_text = "ctrl+c quit | : command | / ? filter/search | n/N next/prev | gg/G top/bottom | ctrl+d/u half-page | [ ] history | - repeat | ctrl+a aliases | tab switch-ctx | j/k move/scroll | left/right h-scroll (wrap off) | w wrap toggle | y copy detail | d describe | x decode secret | e edit | :fmt yaml|json | l logs(stream) | s tail on/off | p pause/resume logs | S sources | L latest | c container picker | ctrl+d delete(table) | ctrl+k kill"; let help_height = if self.show_help { max_vertical_scroll_for_text(help_text, frame.area().width.max(1), 1, true) + 1 } else { @@ -3258,6 +3529,18 @@ impl App { .constraints(constraints) .split(frame.area()); + let mut table_offset = active.table_offset; + if active.pane == Pane::Table { + let viewport_rows = table_viewport_rows(chunks[2].height); + let (synced_selected, synced_offset) = + sync_table_viewport(selected, table_offset, viewport_rows, vm.rows.len()); + selected = synced_selected; + table_offset = synced_offset; + let tab = self.current_tab_mut(); + tab.selected = synced_selected; + tab.table_offset = synced_offset; + } + let top_status = Paragraph::new(Line::from(top_line)).style(theme.header); frame.render_widget(top_status, chunks[0]); @@ -3502,17 +3785,15 @@ impl App { ))) .row_highlight_style(theme.row_highlight); - let mut state = - ratatui::widgets::TableState::default().with_selected(Some(selected)); + let mut state = ratatui::widgets::TableState::default() + .with_selected(Some(selected)) + .with_offset(table_offset); frame.render_stateful_widget(table, chunks[2], &mut state); } - Pane::Describe | Pane::Events => { - let raw_body = self.detail_text(&vm.rows, selected, active.pane); - let body = if active.detail_filter.trim().is_empty() { - raw_body - } else { - filter_text_lines(&raw_body, &active.detail_filter) - }; + Pane::Describe | Pane::SecretDecode | Pane::Events => { + let raw_body = + self.detail_text(&vm.rows, selected, active.pane, active.detail_format); + let body = raw_body; let content_width = chunks[2].width.saturating_sub(2); let content_height = chunks[2].height.saturating_sub(2); self.detail_page_step = (content_height / 2).max(1); @@ -3536,27 +3817,40 @@ impl App { }; let pane_title = match active.pane { Pane::Describe => "Describe", + Pane::SecretDecode => "Decode", Pane::Events => "Events", Pane::Logs => "Logs", Pane::Table => "Table", }; + let pane_title = format!("{pane_title} ({})", active.detail_format.label()); let search_query = active.detail_filter.trim(); let match_lines = search_match_lines(&body, search_query); + let active_line = resolved_active_match_line( + detail_scroll, + &match_lines, + active.detail_active_match_line, + ); + { + let tab = self.current_tab_mut(); + tab.detail_active_match_line = active_line; + } let total_lines = body.lines().count(); let title = detail_viewer_title( - pane_title, + &pane_title, detail_wrap, search_query, &match_lines, detail_scroll, total_lines, + active_line, + ); + let detail_text = highlighted_structured_text( + &body, + search_query, + active.detail_format, + self.color_support, + active_line, ); - let active_line = active_match_line(detail_scroll, &match_lines); - let detail_text = if active.pane == Pane::Describe { - highlighted_json_text(&body, search_query, self.color_support, active_line) - } else { - highlighted_text(&body, search_query, active_line) - }; let mut paragraph = Paragraph::new(detail_text) .block(Block::default().borders(Borders::ALL).title(title)) .scroll((detail_scroll, detail_hscroll)) @@ -3621,6 +3915,15 @@ impl App { } let search_query = active.detail_filter.trim(); let match_lines = self.log_search_match_lines(search_query); + let active_line = resolved_active_match_line( + detail_scroll, + match_lines.as_slice(), + active.detail_active_match_line, + ); + { + let tab = self.current_tab_mut(); + tab.detail_active_match_line = active_line; + } let total_lines = logs_line_count.max(1); let title = format!( "{} | {}", @@ -3631,10 +3934,10 @@ impl App { search_query, match_lines.as_slice(), detail_scroll, - total_lines + total_lines, + active_line ) ); - let active_line = active_match_line(detail_scroll, match_lines.as_slice()); if detail_wrap { let body = if logs_line_count > 0 { if self.logs.hidden_sources.is_empty() { @@ -3743,7 +4046,13 @@ impl App { ); } - fn detail_text(&self, rows: &[crate::view::ViewRow], selected: usize, pane: Pane) -> String { + fn detail_text( + &self, + rows: &[crate::view::ViewRow], + selected: usize, + pane: Pane, + format: DetailFormat, + ) -> String { let Some(row) = rows.get(selected) else { return "No resource selected".to_string(); }; @@ -3752,12 +4061,25 @@ impl App { }; match pane { - Pane::Describe => { - serde_json::to_string_pretty(&entity.raw).unwrap_or_else(|_| "{}".to_string()) + Pane::Describe => match format { + DetailFormat::Yaml => to_pretty_yaml(&entity.raw), + DetailFormat::Json => to_pretty_json(&entity.raw), + }, + Pane::SecretDecode => { + if row.key.kind != ResourceKind::Secrets { + return "Decode pane is available for Secret resources only".to_string(); + } + match format { + DetailFormat::Yaml => decoded_secret_text(&entity.raw), + DetailFormat::Json => decoded_secret_json_text(&entity.raw), + } } Pane::Events => { if row.key.kind == ResourceKind::Events { - serde_json::to_string_pretty(&entity.raw).unwrap_or_else(|_| "{}".to_string()) + match format { + DetailFormat::Yaml => to_pretty_yaml(&entity.raw), + DetailFormat::Json => to_pretty_json(&entity.raw), + } } else { "Events pane currently supports Event resources directly; resource-scoped event correlation is planned in next milestone.".to_string() } @@ -3791,6 +4113,147 @@ impl App { self.overlay = None; } + fn toggle_secret_decode(&mut self) { + if self.current_tab().pane == Pane::SecretDecode { + let tab = self.current_tab_mut(); + tab.pane = Pane::Describe; + tab.detail_scroll = 0; + tab.detail_hscroll = 0; + self.overlay = None; + self.status_line = "Decode: off".to_string(); + return; + } + + let Some(row) = self.selected_row() else { + self.status_line = "No resource selected".to_string(); + return; + }; + if row.key.kind != ResourceKind::Secrets { + self.status_line = "Decode is only available for Secret resources".to_string(); + return; + } + + let tab = self.current_tab_mut(); + tab.pane = Pane::SecretDecode; + tab.detail_scroll = 0; + tab.detail_hscroll = 0; + self.overlay = None; + self.status_line = format!( + "Decode: {} {}", + row.key.namespace.as_deref().unwrap_or("default"), + row.key.name + ); + } + + async fn edit_current_view(&mut self, format_override: Option) { + let pane = self.current_tab().pane; + if !matches!(pane, Pane::Describe | Pane::SecretDecode) { + self.status_line = "Edit is available in Describe/Decode panes".to_string(); + return; + } + + let detail_format = format_override.unwrap_or(self.current_tab().detail_format); + if format_override.is_some() { + self.current_tab_mut().detail_format = detail_format; + } + + let active = self.current_tab().clone(); + let request = self.view_request_for_tab(&active); + let vm = self.projected_view(&request); + let selected = active.selected.min(vm.rows.len().saturating_sub(1)); + let Some(row) = vm.rows.get(selected) else { + self.status_line = "No resource selected".to_string(); + return; + }; + let Some(entity) = self.store.get(&row.key) else { + self.status_line = "Resource details unavailable".to_string(); + return; + }; + let key = row.key.clone(); + let original = entity.raw.clone(); + + let initial_text = match pane { + Pane::Describe => match detail_format { + DetailFormat::Yaml => to_pretty_yaml(&original), + DetailFormat::Json => to_pretty_json(&original), + }, + Pane::SecretDecode => { + if key.kind != ResourceKind::Secrets { + self.status_line = + "Decode edit is only available for Secret resources".to_string(); + return; + } + match detail_format { + DetailFormat::Yaml => decoded_secret_text(&original), + DetailFormat::Json => decoded_secret_json_text(&original), + } + } + _ => { + self.status_line = "Edit is available in Describe/Decode panes".to_string(); + return; + } + }; + + let edited_text = match run_external_editor(&initial_text, detail_format.extension()) { + Ok(Some(text)) => text, + Ok(None) => { + self.status_line = "Edit canceled".to_string(); + return; + } + Err(err) => { + self.status_line = format!("Editor failed: {err}"); + return; + } + }; + + if edited_text == initial_text { + self.status_line = "No changes to apply".to_string(); + return; + } + + let manifest = match (pane, detail_format) { + (Pane::Describe, DetailFormat::Yaml) => match parse_yaml_to_json(&edited_text) { + Ok(manifest) => manifest, + Err(err) => { + self.status_line = format!("Invalid YAML: {err}"); + return; + } + }, + (Pane::Describe, DetailFormat::Json) => match parse_json_to_json(&edited_text) { + Ok(manifest) => manifest, + Err(err) => { + self.status_line = format!("Invalid JSON: {err}"); + return; + } + }, + (Pane::SecretDecode, DetailFormat::Yaml) => { + match apply_decoded_secret_yaml(&original, &edited_text) { + Ok(manifest) => manifest, + Err(err) => { + self.status_line = format!("Invalid decoded secret YAML: {err}"); + return; + } + } + } + (Pane::SecretDecode, DetailFormat::Json) => { + match apply_decoded_secret_json(&original, &edited_text) { + Ok(manifest) => manifest, + Err(err) => { + self.status_line = format!("Invalid decoded secret JSON: {err}"); + return; + } + } + } + _ => unreachable!(), + }; + + let result = self.action_executor.replace_resource(&key, manifest).await; + self.status_line = match result { + Ok(outcome) => outcome.message, + Err(error) => render_action_error(error, &key), + }; + } + fn next_context(&mut self) { self.active_tab = (self.active_tab + 1) % self.tabs.len().max(1); self.status_line = format!("Context: {}", self.current_tab().context); @@ -4051,6 +4514,11 @@ fn command_names() -> &'static [&'static str] { "aliases", "clear", "clear-filter", + "fmt", + "format", + "yaml", + "yml", + "json", "c", "container", "containers", @@ -4058,7 +4526,11 @@ fn command_names() -> &'static [&'static str] { "src", "pause", "resume", + "edit", "tail", + "copy", + "yank", + "dump", "help", "?", "quit", @@ -4174,6 +4646,392 @@ fn common_prefix(values: &[String]) -> String { prefix } +fn expand_user_path(raw: &str) -> PathBuf { + if let Some(stripped) = raw.strip_prefix("~/") + && let Some(home) = dirs::home_dir() + { + return home.join(stripped); + } + PathBuf::from(raw) +} + +fn run_clipboard_command(program: &str, args: &[&str], text: &str) -> io::Result<()> { + let mut child = Command::new(program) + .args(args) + .stdin(Stdio::piped()) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn()?; + + if let Some(mut stdin) = child.stdin.take() { + stdin.write_all(text.as_bytes())?; + } + + let status = child.wait()?; + if status.success() { + Ok(()) + } else { + Err(io::Error::other(format!( + "{program} exited with status {status}" + ))) + } +} + +fn copy_to_native_clipboard(text: &str) -> Result<&'static str, String> { + #[cfg(target_os = "macos")] + { + run_clipboard_command("pbcopy", &[], text) + .map(|_| "pbcopy") + .map_err(|err| err.to_string()) + } + + #[cfg(target_os = "linux")] + { + let mut errors = Vec::new(); + if env::var_os("WAYLAND_DISPLAY").is_some() { + match run_clipboard_command("wl-copy", &[], text) { + Ok(()) => return Ok("wl-copy"), + Err(err) => errors.push(format!("wl-copy: {err}")), + } + } + match run_clipboard_command("xclip", &["-selection", "clipboard"], text) { + Ok(()) => return Ok("xclip"), + Err(err) => errors.push(format!("xclip: {err}")), + } + match run_clipboard_command("xsel", &["--clipboard", "--input"], text) { + Ok(()) => return Ok("xsel"), + Err(err) => errors.push(format!("xsel: {err}")), + } + return Err(errors.join("; ")); + } + + #[cfg(not(any(target_os = "macos", target_os = "linux")))] + { + let _ = text; + Err("native clipboard backend is not configured for this platform".to_string()) + } +} + +fn truncate_for_clipboard(text: &str, max_bytes: usize) -> (String, bool) { + if text.len() <= max_bytes { + return (text.to_string(), false); + } + let mut end = max_bytes.min(text.len()); + while end > 0 && !text.is_char_boundary(end) { + end = end.saturating_sub(1); + } + (text[..end].to_string(), true) +} + +fn base64_encode(data: &[u8]) -> String { + const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + if data.is_empty() { + return String::new(); + } + + let mut out = String::with_capacity(data.len().div_ceil(3) * 4); + let mut i = 0usize; + while i + 3 <= data.len() { + let chunk = ((data[i] as u32) << 16) | ((data[i + 1] as u32) << 8) | (data[i + 2] as u32); + out.push(ALPHABET[((chunk >> 18) & 0x3f) as usize] as char); + out.push(ALPHABET[((chunk >> 12) & 0x3f) as usize] as char); + out.push(ALPHABET[((chunk >> 6) & 0x3f) as usize] as char); + out.push(ALPHABET[(chunk & 0x3f) as usize] as char); + i += 3; + } + + let rem = data.len() - i; + if rem == 1 { + let chunk = (data[i] as u32) << 16; + out.push(ALPHABET[((chunk >> 18) & 0x3f) as usize] as char); + out.push(ALPHABET[((chunk >> 12) & 0x3f) as usize] as char); + out.push('='); + out.push('='); + } else if rem == 2 { + let chunk = ((data[i] as u32) << 16) | ((data[i + 1] as u32) << 8); + out.push(ALPHABET[((chunk >> 18) & 0x3f) as usize] as char); + out.push(ALPHABET[((chunk >> 12) & 0x3f) as usize] as char); + out.push(ALPHABET[((chunk >> 6) & 0x3f) as usize] as char); + out.push('='); + } + + out +} + +fn base64_value(byte: u8) -> Option { + match byte { + b'A'..=b'Z' => Some(byte - b'A'), + b'a'..=b'z' => Some(byte - b'a' + 26), + b'0'..=b'9' => Some(byte - b'0' + 52), + b'+' | b'-' => Some(62), + b'/' | b'_' => Some(63), + _ => None, + } +} + +fn base64_decode(input: &str) -> Result, String> { + let mut cleaned: Vec = input + .bytes() + .filter(|byte| !byte.is_ascii_whitespace()) + .collect(); + if cleaned.is_empty() { + return Ok(Vec::new()); + } + if cleaned.len() % 4 == 1 { + return Err("invalid base64 length".to_string()); + } + while !cleaned.len().is_multiple_of(4) { + cleaned.push(b'='); + } + + let mut out = Vec::with_capacity((cleaned.len() / 4) * 3); + let mut idx = 0usize; + while idx < cleaned.len() { + let c0 = cleaned[idx]; + let c1 = cleaned[idx + 1]; + let c2 = cleaned[idx + 2]; + let c3 = cleaned[idx + 3]; + idx += 4; + + let v0 = base64_value(c0).ok_or_else(|| "invalid base64 character".to_string())?; + let v1 = base64_value(c1).ok_or_else(|| "invalid base64 character".to_string())?; + let v2 = if c2 == b'=' { + 0 + } else { + base64_value(c2).ok_or_else(|| "invalid base64 character".to_string())? + }; + let v3 = if c3 == b'=' { + 0 + } else { + base64_value(c3).ok_or_else(|| "invalid base64 character".to_string())? + }; + + let chunk = ((v0 as u32) << 18) | ((v1 as u32) << 12) | ((v2 as u32) << 6) | v3 as u32; + out.push(((chunk >> 16) & 0xff) as u8); + if c2 != b'=' { + out.push(((chunk >> 8) & 0xff) as u8); + } + if c3 != b'=' { + out.push((chunk & 0xff) as u8); + } + } + + Ok(out) +} + +fn is_mostly_printable_text(text: &str) -> bool { + let mut total = 0usize; + let mut printable = 0usize; + for ch in text.chars() { + total += 1; + if ch == '\n' || ch == '\r' || ch == '\t' || !ch.is_control() { + printable += 1; + } + } + if total == 0 { + return true; + } + printable * 100 / total >= 95 +} + +fn format_decoded_secret_value(bytes: &[u8]) -> String { + if let Ok(text) = std::str::from_utf8(bytes) + && is_mostly_printable_text(text) + { + let trimmed = text.trim(); + if !trimmed.is_empty() + && ((trimmed.starts_with('{') && trimmed.ends_with('}')) + || (trimmed.starts_with('[') && trimmed.ends_with(']'))) + && let Ok(json) = serde_json::from_str::(trimmed) + && let Ok(pretty) = serde_json::to_string_pretty(&json) + { + return pretty; + } + return text.to_string(); + } + + let preview_len = bytes.len().min(32); + let preview = bytes + .iter() + .take(preview_len) + .map(|byte| format!("{byte:02x}")) + .collect::>() + .join(""); + if bytes.len() > preview_len { + format!("[binary {} bytes] hex-preview={}...", bytes.len(), preview) + } else { + format!("[binary {} bytes] hex-preview={}", bytes.len(), preview) + } +} + +fn to_pretty_yaml(value: &serde_json::Value) -> String { + serde_yaml::to_string(value).unwrap_or_else(|_| "--- {}\n".to_string()) +} + +fn to_pretty_json(value: &serde_json::Value) -> String { + serde_json::to_string_pretty(value).unwrap_or_else(|_| "{}\n".to_string()) +} + +fn decoded_secret_data_map(raw: &serde_json::Value) -> BTreeMap { + let mut out = BTreeMap::new(); + if let Some(data) = raw.get("data").and_then(serde_json::Value::as_object) { + for (key, value) in data { + let encoded = value.as_str().unwrap_or_default(); + let rendered = match base64_decode(encoded) { + Ok(decoded) => format_decoded_secret_value(&decoded), + Err(err) => format!(""), + }; + out.insert(key.clone(), rendered); + } + } + out +} + +fn decoded_secret_text(raw: &serde_json::Value) -> String { + let decoded = decoded_secret_data_map(raw); + serde_yaml::to_string(&decoded).unwrap_or_else(|_| "--- {}\n".to_string()) +} + +fn decoded_secret_json_text(raw: &serde_json::Value) -> String { + let decoded = decoded_secret_data_map(raw); + serde_json::to_string_pretty(&decoded).unwrap_or_else(|_| "{}\n".to_string()) +} + +fn yaml_value_to_secret_string(value: &serde_yaml::Value) -> Result { + match value { + serde_yaml::Value::Null => Ok(String::new()), + serde_yaml::Value::Bool(v) => Ok(v.to_string()), + serde_yaml::Value::Number(v) => Ok(v.to_string()), + serde_yaml::Value::String(v) => Ok(v.clone()), + serde_yaml::Value::Sequence(_) | serde_yaml::Value::Mapping(_) => { + serde_yaml::to_string(value) + .map(|s| s.trim_end().to_string()) + .map_err(|err| err.to_string()) + } + _ => Err("unsupported YAML value type".to_string()), + } +} + +fn parse_yaml_to_json(text: &str) -> Result { + let yaml: serde_yaml::Value = serde_yaml::from_str(text).map_err(|err| err.to_string())?; + serde_json::to_value(yaml).map_err(|err| err.to_string()) +} + +fn parse_json_to_json(text: &str) -> Result { + serde_json::from_str(text).map_err(|err| err.to_string()) +} + +fn apply_decoded_secret_yaml( + original: &serde_json::Value, + edited: &str, +) -> Result { + let yaml: serde_yaml::Value = serde_yaml::from_str(edited).map_err(|err| err.to_string())?; + let mapping = yaml + .as_mapping() + .ok_or_else(|| "decoded secret edit must be a YAML mapping".to_string())?; + + let mut data = serde_json::Map::new(); + for (key, value) in mapping { + let key_str = key + .as_str() + .ok_or_else(|| "all decoded secret keys must be strings".to_string())?; + let raw_value = yaml_value_to_secret_string(value)?; + data.insert( + key_str.to_string(), + serde_json::Value::String(base64_encode(raw_value.as_bytes())), + ); + } + + let mut out = original.clone(); + if !out.is_object() { + return Err("secret manifest is not an object".to_string()); + } + if let Some(obj) = out.as_object_mut() { + obj.insert("data".to_string(), serde_json::Value::Object(data)); + obj.remove("stringData"); + } + Ok(out) +} + +fn json_value_to_secret_string(value: &serde_json::Value) -> Result { + match value { + serde_json::Value::Null => Ok(String::new()), + serde_json::Value::Bool(v) => Ok(v.to_string()), + serde_json::Value::Number(v) => Ok(v.to_string()), + serde_json::Value::String(v) => Ok(v.clone()), + serde_json::Value::Array(_) | serde_json::Value::Object(_) => { + serde_json::to_string_pretty(value).map_err(|err| err.to_string()) + } + } +} + +fn apply_decoded_secret_json( + original: &serde_json::Value, + edited: &str, +) -> Result { + let json: serde_json::Value = serde_json::from_str(edited).map_err(|err| err.to_string())?; + let mapping = json + .as_object() + .ok_or_else(|| "decoded secret edit must be a JSON object".to_string())?; + + let mut data = serde_json::Map::new(); + for (key, value) in mapping { + let raw_value = json_value_to_secret_string(value)?; + data.insert( + key.to_string(), + serde_json::Value::String(base64_encode(raw_value.as_bytes())), + ); + } + + let mut out = original.clone(); + if !out.is_object() { + return Err("secret manifest is not an object".to_string()); + } + if let Some(obj) = out.as_object_mut() { + obj.insert("data".to_string(), serde_json::Value::Object(data)); + obj.remove("stringData"); + } + Ok(out) +} + +fn run_external_editor(initial_text: &str, extension: &str) -> Result, String> { + let editor = env::var("VISUAL") + .or_else(|_| env::var("EDITOR")) + .unwrap_or_else(|_| "vi".to_string()); + let mut path = env::temp_dir(); + let now_nanos = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_nanos()) + .unwrap_or(0); + let nonce = format!("krust-edit-{}-{now_nanos}.{extension}", std::process::id()); + path.push(nonce); + + fs::write(&path, initial_text).map_err(|err| err.to_string())?; + + let leave_result = execute!(io::stdout(), DisableMouseCapture, LeaveAlternateScreen); + let _ = disable_raw_mode(); + if let Err(err) = leave_result { + let _ = fs::remove_file(&path); + return Err(err.to_string()); + } + + let status = Command::new(&editor).arg(&path).status(); + + let _ = execute!(io::stdout(), EnterAlternateScreen, EnableMouseCapture); + let _ = enable_raw_mode(); + + let result = match status { + Ok(status) if status.success() => fs::read_to_string(&path) + .map(Some) + .map_err(|err| err.to_string()), + Ok(status) => Err(format!("editor exited with status {status}")), + Err(err) => Err(err.to_string()), + }; + + let _ = fs::remove_file(&path); + result +} + fn list_filtered_indices(values: &[String], filter: &str) -> Vec { let needle = filter.trim().to_ascii_lowercase(); if needle.is_empty() { @@ -4192,22 +5050,6 @@ fn list_filtered_indices(values: &[String], filter: &str) -> Vec { .collect() } -fn filter_text_lines(text: &str, filter: &str) -> String { - let needle = filter.trim().to_ascii_lowercase(); - if needle.is_empty() { - return text.to_string(); - } - - let matched: Vec<&str> = text - .lines() - .filter(|line| line.to_ascii_lowercase().contains(&needle)) - .collect(); - if matched.is_empty() { - return format!("No matches for '{}'", filter.trim()); - } - matched.join("\n") -} - fn search_match_lines(text: &str, query: &str) -> Vec { let needle = query.trim().to_ascii_lowercase(); if needle.is_empty() { @@ -4307,6 +5149,66 @@ fn active_match_line(scroll: u16, match_lines: &[usize]) -> Option { ) } +fn resolved_active_match_line( + scroll: u16, + match_lines: &[usize], + preferred_line: Option, +) -> Option { + if match_lines.is_empty() { + return None; + } + if let Some(line) = preferred_line + && match_lines.contains(&line) + { + return Some(line); + } + active_match_line(scroll, match_lines) +} + +fn step_match_line( + match_lines: &[usize], + current_line: usize, + forward: bool, +) -> Option<(usize, usize)> { + if match_lines.is_empty() { + return None; + } + + if forward { + if let Some(pos) = match_lines.iter().position(|line| *line == current_line) { + let next = (pos + 1) % match_lines.len(); + return Some((match_lines[next], next + 1)); + } + if let Some((idx, line)) = match_lines + .iter() + .enumerate() + .find(|(_, line)| **line > current_line) + { + return Some((*line, idx + 1)); + } + Some((match_lines[0], 1)) + } else { + if let Some(pos) = match_lines.iter().position(|line| *line == current_line) { + let prev = if pos == 0 { + match_lines.len() - 1 + } else { + pos - 1 + }; + return Some((match_lines[prev], prev + 1)); + } + if let Some((idx, line)) = match_lines + .iter() + .enumerate() + .rev() + .find(|(_, line)| **line < current_line) + { + return Some((*line, idx + 1)); + } + let last = match_lines.len() - 1; + Some((match_lines[last], last + 1)) + } +} + fn highlighted_text(text: &str, query: &str, active_line: Option) -> Text<'static> { let needle = query.trim().to_ascii_lowercase(); if needle.is_empty() { @@ -4342,6 +5244,19 @@ fn highlighted_text(text: &str, query: &str, active_line: Option) -> Text Text::from(out) } +fn highlighted_structured_text( + text: &str, + query: &str, + format: DetailFormat, + support: ColorSupport, + active_line: Option, +) -> Text<'static> { + match format { + DetailFormat::Yaml => highlighted_yaml_text(text, query, support, active_line), + DetailFormat::Json => highlighted_json_text(text, query, support, active_line), + } +} + #[derive(Clone, Copy)] enum JsonTokenKind { Key, @@ -4576,6 +5491,324 @@ fn highlighted_json_text( Text::from(out) } +#[derive(Clone, Copy)] +enum YamlTokenKind { + Key, + String, + Number, + Bool, + Null, + Comment, + Punct, + Plain, +} + +fn yaml_token_style(kind: YamlTokenKind, support: ColorSupport) -> Style { + match support { + ColorSupport::NoColor => match kind { + YamlTokenKind::Key => Style::default().add_modifier(Modifier::BOLD), + YamlTokenKind::Comment => Style::default().add_modifier(Modifier::DIM), + _ => Style::default(), + }, + ColorSupport::Basic => match kind { + YamlTokenKind::Key => Style::default() + .fg(Color::Cyan) + .add_modifier(Modifier::BOLD), + YamlTokenKind::String => Style::default().fg(Color::Green), + YamlTokenKind::Number => Style::default().fg(Color::Magenta), + YamlTokenKind::Bool | YamlTokenKind::Null => Style::default() + .fg(Color::Yellow) + .add_modifier(Modifier::BOLD), + YamlTokenKind::Comment => Style::default() + .fg(Color::DarkGray) + .add_modifier(Modifier::DIM), + YamlTokenKind::Punct => Style::default().fg(Color::DarkGray), + YamlTokenKind::Plain => Style::default(), + }, + ColorSupport::Ansi256 | ColorSupport::TrueColor => match kind { + YamlTokenKind::Key => Style::default() + .fg(Color::Indexed(117)) + .add_modifier(Modifier::BOLD), + YamlTokenKind::String => Style::default().fg(Color::Indexed(114)), + YamlTokenKind::Number => Style::default().fg(Color::Indexed(213)), + YamlTokenKind::Bool => Style::default() + .fg(Color::Indexed(220)) + .add_modifier(Modifier::BOLD), + YamlTokenKind::Null => Style::default().fg(Color::Indexed(180)), + YamlTokenKind::Comment => Style::default() + .fg(Color::Indexed(244)) + .add_modifier(Modifier::DIM), + YamlTokenKind::Punct => Style::default().fg(Color::Indexed(245)), + YamlTokenKind::Plain => Style::default(), + }, + } +} + +fn find_yaml_comment_index(line: &str) -> Option { + let mut in_single = false; + let mut in_double = false; + let mut escaped = false; + let mut prev: Option = None; + for (idx, ch) in line.char_indices() { + if in_double && escaped { + escaped = false; + prev = Some(ch); + continue; + } + if ch == '\\' && in_double { + escaped = true; + prev = Some(ch); + continue; + } + if ch == '\'' && !in_double { + in_single = !in_single; + prev = Some(ch); + continue; + } + if ch == '"' && !in_single { + in_double = !in_double; + prev = Some(ch); + continue; + } + if ch == '#' && !in_single && !in_double { + let starts_comment = idx == 0 || prev.map(|c| c.is_whitespace()).unwrap_or(true); + if starts_comment { + return Some(idx); + } + } + prev = Some(ch); + } + None +} + +fn is_yaml_scalar_boundary(line: &str, index: usize) -> bool { + line.get(index..) + .and_then(|rest| rest.chars().next()) + .map(|ch| ch.is_whitespace() || matches!(ch, ',' | ']' | '}' | ':' | '#')) + .unwrap_or(true) +} + +fn is_yaml_number_token(token: &str) -> bool { + let token = token.trim(); + if token.is_empty() { + return false; + } + if token.starts_with("0x") || token.starts_with("0o") || token.starts_with("0b") { + return token.len() > 2; + } + if token == "-" || token == "+" { + return false; + } + let mut dots = 0usize; + let mut digits = 0usize; + for (idx, ch) in token.chars().enumerate() { + if ch.is_ascii_digit() { + digits += 1; + continue; + } + if (ch == '-' || ch == '+') && idx == 0 { + continue; + } + if ch == '.' { + dots += 1; + if dots > 1 { + return false; + } + continue; + } + return false; + } + digits > 0 +} + +fn is_yaml_word_char(ch: char) -> bool { + ch.is_ascii_alphanumeric() || matches!(ch, '_' | '-' | '.' | '/') +} + +fn yaml_spans_for_line(line: &str, support: ColorSupport) -> Vec<(String, Style)> { + let (code, comment) = if let Some(comment_start) = find_yaml_comment_index(line) { + (&line[..comment_start], Some(&line[comment_start..])) + } else { + (line, None) + }; + + let mut out = Vec::new(); + let mut idx = 0usize; + while idx < code.len() { + let Some(ch) = code[idx..].chars().next() else { + break; + }; + + if ch.is_whitespace() { + let start = idx; + idx = idx.saturating_add(ch.len_utf8()); + while idx < code.len() { + let Some(next) = code[idx..].chars().next() else { + break; + }; + if !next.is_whitespace() { + break; + } + idx = idx.saturating_add(next.len_utf8()); + } + out.push(( + code.get(start..idx).unwrap_or("").to_string(), + yaml_token_style(YamlTokenKind::Plain, support), + )); + continue; + } + + if ch == '\'' || ch == '"' { + let quote = ch; + let start = idx; + idx = idx.saturating_add(ch.len_utf8()); + let mut escaped = false; + while idx < code.len() { + let Some(next) = code[idx..].chars().next() else { + break; + }; + if quote == '"' && escaped { + escaped = false; + idx = idx.saturating_add(next.len_utf8()); + continue; + } + if quote == '"' && next == '\\' { + escaped = true; + idx = idx.saturating_add(next.len_utf8()); + continue; + } + idx = idx.saturating_add(next.len_utf8()); + if next == quote { + break; + } + } + out.push(( + code.get(start..idx).unwrap_or("").to_string(), + yaml_token_style(YamlTokenKind::String, support), + )); + continue; + } + + if matches!(ch, '[' | ']' | '{' | '}' | ',' | '?') { + out.push(( + ch.to_string(), + yaml_token_style(YamlTokenKind::Punct, support), + )); + idx = idx.saturating_add(ch.len_utf8()); + continue; + } + + if ch == '-' { + let next = code[idx + ch.len_utf8()..].chars().next(); + if next.is_some_and(char::is_whitespace) { + out.push(( + "-".to_string(), + yaml_token_style(YamlTokenKind::Punct, support), + )); + idx = idx.saturating_add(ch.len_utf8()); + continue; + } + } + + if is_yaml_word_char(ch) { + let start = idx; + idx = idx.saturating_add(ch.len_utf8()); + while idx < code.len() { + let Some(next) = code[idx..].chars().next() else { + break; + }; + if !is_yaml_word_char(next) { + break; + } + idx = idx.saturating_add(next.len_utf8()); + } + let token = code.get(start..idx).unwrap_or(""); + + let mut probe = idx; + while probe < code.len() { + let Some(ws) = code[probe..].chars().next() else { + break; + }; + if ws.is_whitespace() { + probe = probe.saturating_add(ws.len_utf8()); + } else { + break; + } + } + if code[probe..].starts_with(':') { + out.push(( + token.to_string(), + yaml_token_style(YamlTokenKind::Key, support), + )); + if probe > idx { + out.push(( + code.get(idx..probe).unwrap_or("").to_string(), + yaml_token_style(YamlTokenKind::Plain, support), + )); + } + out.push(( + ":".to_string(), + yaml_token_style(YamlTokenKind::Punct, support), + )); + idx = probe + 1; + continue; + } + + let lower = token.to_ascii_lowercase(); + let kind = if (lower == "true" || lower == "false" || lower == "yes" || lower == "no") + && is_yaml_scalar_boundary(code, idx) + { + YamlTokenKind::Bool + } else if (lower == "null" || lower == "~") && is_yaml_scalar_boundary(code, idx) { + YamlTokenKind::Null + } else if is_yaml_number_token(token) { + YamlTokenKind::Number + } else { + YamlTokenKind::Plain + }; + out.push((token.to_string(), yaml_token_style(kind, support))); + continue; + } + + out.push(( + ch.to_string(), + yaml_token_style(YamlTokenKind::Plain, support), + )); + idx = idx.saturating_add(ch.len_utf8()); + } + + if let Some(comment) = comment { + out.push(( + comment.to_string(), + yaml_token_style(YamlTokenKind::Comment, support), + )); + } + + out +} + +fn highlighted_yaml_text( + text: &str, + query: &str, + support: ColorSupport, + active_line: Option, +) -> Text<'static> { + let needle = query.trim().to_ascii_lowercase(); + let mut out = Vec::new(); + for (line_idx, line) in text.lines().enumerate() { + let mut spans = Vec::new(); + let highlight_style = search_match_highlight_style(active_line == Some(line_idx)); + for (segment, style) in yaml_spans_for_line(line, support) { + push_query_highlighted_span(&mut spans, &segment, style, &needle, highlight_style); + } + if spans.is_empty() { + spans.push(Span::raw(String::new())); + } + out.push(Line::from(spans)); + } + Text::from(out) +} + fn detail_viewer_title( pane_title: &str, wrap: bool, @@ -4583,18 +5816,24 @@ fn detail_viewer_title( match_lines: &[usize], scroll: u16, total_lines: usize, + active_line: Option, ) -> String { let search = if search_query.trim().is_empty() { "search:-".to_string() } else if match_lines.is_empty() { format!("search:/{} 0/0", search_query.trim()) } else { - let current_line = scroll as usize; - let current_idx = match_lines - .iter() - .position(|line| *line >= current_line) - .unwrap_or(match_lines.len() - 1) - + 1; + let current_idx = active_line + .and_then(|line| match_lines.iter().position(|candidate| *candidate == line)) + .map(|idx| idx + 1) + .or_else(|| { + let current_line = scroll as usize; + match_lines + .iter() + .position(|line| *line >= current_line) + .map(|idx| idx + 1) + }) + .unwrap_or(match_lines.len()); format!( "search:/{} {}/{}", search_query.trim(), @@ -4714,6 +5953,7 @@ fn pane_icon(pane: Pane) -> &'static str { match pane { Pane::Table => "[TB]", Pane::Describe => "[DS]", + Pane::SecretDecode => "[SX]", Pane::Events => "[EV]", Pane::Logs => "[LG]", } @@ -5099,6 +6339,36 @@ fn delete_previous_word(value: &mut String) { } } +fn table_viewport_rows(table_height: u16) -> usize { + table_height.saturating_sub(3) as usize +} + +fn sync_table_viewport( + selected: usize, + offset: usize, + viewport_rows: usize, + total_rows: usize, +) -> (usize, usize) { + if total_rows == 0 || viewport_rows == 0 { + return (0, 0); + } + + let selected = selected.min(total_rows.saturating_sub(1)); + let max_offset = total_rows.saturating_sub(viewport_rows); + let mut offset = offset.min(max_offset); + + if selected < offset { + offset = selected; + } else { + let visible_end_exclusive = offset.saturating_add(viewport_rows); + if selected >= visible_end_exclusive { + offset = selected.saturating_add(1).saturating_sub(viewport_rows); + } + } + + (selected, offset.min(max_offset)) +} + fn max_vertical_scroll_for_text( text: &str, viewport_width: u16, @@ -5246,10 +6516,20 @@ fn parse_resource_alias(token: &str) -> ResourceAlias { } } +struct TerminalGuard; + +impl Drop for TerminalGuard { + fn drop(&mut self) { + let _ = disable_raw_mode(); + let mut stdout = io::stdout(); + let _ = execute!(stdout, DisableMouseCapture, LeaveAlternateScreen); + } +} + #[cfg(test)] mod tests { use std::{ - collections::{HashSet, VecDeque}, + collections::{BTreeMap, HashMap, HashSet, VecDeque}, sync::Arc, time::Instant, }; @@ -5260,11 +6540,14 @@ mod tests { use tokio::sync::mpsc; use super::{ - App, ColorSupport, ResourceAlias, classify_status_severity, color_support_label, - command_names, detect_color_support_from_env, highlighted_json_text, highlighted_text, - is_auth_refresh_log_error, is_retryable_log_error, is_visible_log_line, - json_spans_for_line, next_log_reconnect_backoff_ms, parse_log_source, parse_resource_alias, - resource_alias_names, search_match_lines_in_logs, severity_tag, slice_chars, ui_theme_for, + App, ColorSupport, ResourceAlias, base64_decode, base64_encode, classify_status_severity, + color_support_label, command_names, decoded_secret_text, detect_color_support_from_env, + highlighted_json_text, highlighted_text, highlighted_yaml_text, is_auth_refresh_log_error, + is_retryable_log_error, is_visible_log_line, json_spans_for_line, + next_log_reconnect_backoff_ms, parse_log_source, parse_resource_alias, + resolved_active_match_line, resource_alias_names, search_match_lines_in_logs, severity_tag, + slice_chars, step_match_line, sync_table_viewport, table_viewport_rows, + truncate_for_clipboard, ui_theme_for, yaml_spans_for_line, }; use crate::{ cluster::{ @@ -5313,6 +6596,14 @@ mod tests { async fn delete_resource(&self, _key: &ResourceKey) -> Result { Err(ActionError::Unsupported("noop".to_string())) } + + async fn replace_resource( + &self, + _key: &ResourceKey, + _manifest: serde_json::Value, + ) -> Result { + Err(ActionError::Unsupported("noop".to_string())) + } } fn mk_entity( @@ -5341,6 +6632,7 @@ mod tests { contexts, "ctx-dev".to_string(), Some("default".to_string()), + HashMap::new(), Arc::new(NoopExecutor), Arc::new(NoopProvider { contexts: vec!["ctx-dev".to_string()], @@ -5430,6 +6722,11 @@ mod tests { "all", "0", "kind", + "fmt", + "format", + "yaml", + "yml", + "json", "resources", "res", "aliases", @@ -5442,7 +6739,11 @@ mod tests { "src", "pause", "resume", + "edit", "tail", + "copy", + "yank", + "dump", "pulse", "pulses", "pu", @@ -5564,6 +6865,46 @@ mod tests { ); } + #[test] + fn yaml_syntax_highlighter_marks_key_number_bool_and_comment_tokens() { + let spans = yaml_spans_for_line("cpu: 123\nenabled: true # ok", ColorSupport::Basic); + assert!( + spans + .iter() + .any(|(text, style)| text == "cpu" && style.fg == Some(Color::Cyan)) + ); + assert!( + spans + .iter() + .any(|(text, style)| text == "123" && style.fg == Some(Color::Magenta)) + ); + assert!( + spans + .iter() + .any(|(text, style)| text == "true" && style.fg == Some(Color::Yellow)) + ); + } + + #[test] + fn yaml_syntax_highlighter_keeps_search_highlight() { + let rendered = highlighted_yaml_text( + "kind: Pod\nname: demo # marker", + "demo", + ColorSupport::Basic, + Some(1), + ); + assert!( + rendered + .lines + .iter() + .any(|line| line.spans.iter().any(|span| { + span.content.contains("demo") + && (span.style.bg == Some(Color::Yellow) + || span.style.bg == Some(Color::Cyan)) + })) + ); + } + #[test] fn active_search_match_uses_distinct_highlight_color() { let rendered = highlighted_text("alpha\ndemo\nomega demo", "demo", Some(1)); @@ -5585,6 +6926,62 @@ mod tests { assert!(saw_regular); } + #[test] + fn base64_encode_matches_known_values() { + assert_eq!(base64_encode(b""), ""); + assert_eq!(base64_encode(b"f"), "Zg=="); + assert_eq!(base64_encode(b"fo"), "Zm8="); + assert_eq!(base64_encode(b"foo"), "Zm9v"); + assert_eq!(base64_encode(b"foobar"), "Zm9vYmFy"); + } + + #[test] + fn base64_decode_handles_standard_and_urlsafe_forms() { + assert_eq!(base64_decode("Zm9v").expect("decode"), b"foo"); + assert_eq!(base64_decode("Zm9vYg==").expect("decode"), b"foob"); + assert!(base64_decode("%%%").is_err()); + } + + #[test] + fn decoded_secret_text_decodes_data_entries() { + let raw = serde_json::json!({ + "metadata": { "namespace": "default", "name": "demo" }, + "type": "Opaque", + "data": { + "username": "YWRtaW4=", + "password": "czNjcjN0" + } + }); + let out = decoded_secret_text(&raw); + let parsed: BTreeMap = + serde_yaml::from_str(&out).expect("decoded secret output yaml"); + assert_eq!(parsed.get("username"), Some(&"admin".to_string())); + assert_eq!(parsed.get("password"), Some(&"s3cr3t".to_string())); + } + + #[test] + fn clipboard_truncate_preserves_utf8_boundary() { + let (out, truncated) = truncate_for_clipboard("abc\u{1f680}def", 5); + assert!(truncated); + assert_eq!(out, "abc"); + } + + #[test] + fn step_match_line_moves_cursor_and_wraps() { + let matches = vec![3, 8, 13]; + assert_eq!(step_match_line(&matches, 3, true), Some((8, 2))); + assert_eq!(step_match_line(&matches, 13, true), Some((3, 1))); + assert_eq!(step_match_line(&matches, 8, false), Some((3, 1))); + assert_eq!(step_match_line(&matches, 3, false), Some((13, 3))); + } + + #[test] + fn resolved_active_match_line_prefers_explicit_cursor_if_present() { + let matches = vec![5, 15, 25]; + assert_eq!(resolved_active_match_line(0, &matches, Some(15)), Some(15)); + assert_eq!(resolved_active_match_line(14, &matches, None), Some(15)); + } + #[test] fn classifies_status_tags() { assert_eq!(severity_tag(classify_status_severity("Running")), "[OK]"); @@ -5680,10 +7077,28 @@ mod tests { app.current_tab_mut().detail_filter = "metadata".to_string(); let snap = render_snapshot(&mut app, 120, 32); - assert!(snap.contains("Describe | NORMAL")); + assert!(snap.contains("Describe (yaml) | NORMAL")); assert!(snap.contains("search:/metadata")); } + #[test] + fn detail_search_keeps_full_body_visible() { + let mut app = test_app(); + app.store.apply(StateDelta::Upsert(mk_entity( + "ctx-dev", + ResourceKind::Pods, + Some("default"), + "pod-a", + "Running", + ))); + app.current_tab_mut().pane = Pane::Describe; + app.current_tab_mut().detail_filter = "metadata".to_string(); + + let body = app.current_view_text().expect("detail body"); + assert!(body.contains("metadata")); + assert!(body.contains("status")); + } + #[test] fn snapshot_logs_pane_has_state_and_icons() { let mut app = test_app(); @@ -5737,14 +7152,40 @@ mod tests { elapsed / 2_000 ); } -} -struct TerminalGuard; + #[test] + fn table_viewport_rows_accounts_for_header_and_borders() { + assert_eq!(table_viewport_rows(0), 0); + assert_eq!(table_viewport_rows(2), 0); + assert_eq!(table_viewport_rows(3), 0); + assert_eq!(table_viewport_rows(6), 3); + } -impl Drop for TerminalGuard { - fn drop(&mut self) { - let _ = disable_raw_mode(); - let mut stdout = io::stdout(); - let _ = execute!(stdout, DisableMouseCapture, LeaveAlternateScreen); + #[test] + fn table_viewport_sync_scrolls_down_when_selection_leaves_bottom() { + let (selected, offset) = sync_table_viewport(15, 0, 10, 100); + assert_eq!(selected, 15); + assert_eq!(offset, 6); + } + + #[test] + fn table_viewport_sync_scrolls_up_when_selection_leaves_top() { + let (selected, offset) = sync_table_viewport(89, 90, 10, 100); + assert_eq!(selected, 89); + assert_eq!(offset, 89); + } + + #[test] + fn table_viewport_sync_keeps_selection_moving_inside_viewport() { + let (selected, offset) = sync_table_viewport(98, 90, 10, 100); + assert_eq!(selected, 98); + assert_eq!(offset, 90); + } + + #[test] + fn table_viewport_sync_handles_empty_data() { + let (selected, offset) = sync_table_viewport(5, 3, 10, 0); + assert_eq!(selected, 0); + assert_eq!(offset, 0); } }