diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index c36a8b9..6b5381c 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -3,7 +3,11 @@ on: [pull_request] jobs: build: - runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@0c366fd6a839edf440554fa01a7085ccba70ac98 diff --git a/CLAUDE.md b/CLAUDE.md index 814c30f..66089e1 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -34,7 +34,7 @@ gitrat is a terminal UI (TUI) for staging, viewing diffs, and committing — a l 1. `git.rs` — `Repo` (found via `git rev-parse --show-toplevel`) runs every git command from the repo root with stdin closed, `GIT_TERMINAL_PROMPT=0` and `GIT_OPTIONAL_LOCKS=0`. Parses `git status --porcelain=v1 -z --branch` into `Status { branch, files }`, builds diffs (`--no-color --no-ext-diff`, capped at 20k lines, control chars sanitized), and exposes stage/unstage/revert/remove/commit/push returning `GitResult<_>` (`Result<_, String>` with a readable message). Has integration tests that create temporary repositories. 2. `types.rs` — plain data types: `FileEntry` (path, optional rename origin, raw `XY` status chars, `FileStatus`). 2b. `diff.rs` — turns raw `DiffSection`s into a `DiffView` of `Row`s (`RowKind`, old/new line numbers, styled `Seg`s). Hides git headers (keeps notes like "new file"/"renamed from"), syntax-highlights with syntect + two-face (bat's syntaxes), and marks changed words via a token-level LCS between paired removed/added lines. Caps: 20k rows, highlighting off above 8k rows or 1k-byte lines. -2c. `theme.rs` — `Theme` palette (diff backgrounds, accents, syntax theme OneHalfDark/GitHub). `--theme auto` uses `COLORFGBG`, then macOS `AppleInterfaceStyle`. Truecolor if `COLORTERM`/`TERM_PROGRAM` says so, else hand-picked xterm-256 fallbacks. +2c. `theme.rs` — `Theme` palette (diff backgrounds, accents, syntax theme OneHalfDark/GitHub). `--theme auto` uses `COLORFGBG`, then the OS setting (macOS `AppleInterfaceStyle`, Windows `AppsUseLightTheme`, GNOME `color-scheme`). Truecolor if `COLORTERM`/`WT_SESSION`/`TERM_PROGRAM` says so, else hand-picked xterm-256 fallbacks. 3. `app.rs` — `App` owns UI state: files, selection (kept by path across refreshes), `DiffView` + scroll (rebuilt only when the raw diff changes), `Mode` (`Normal`, `Input`, `Confirm`, `Help`), commit input, transient `Message`. `tick()` collects background push results, expires messages and auto-refreshes every 3 s. 4. `ui.rs` — render function; reads `App` (and records panel rects for mouse hit-testing). 30/70 side-by-side layout, stacked below 80 columns. Diff rows: `old new ± code` gutter, full-width tinted backgrounds, hunk header bars. Syntax tokens using the theme's default foreground render as `Color::Reset` so text stays readable if light/dark detection is wrong. 5. `terminal.rs` — raw mode, alternate screen, bracketed paste, focus events, optional mouse capture, and a panic hook that restores the terminal. diff --git a/README.md b/README.md index aa2d329..063071a 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ CLI project to see git changes and commit. I created this project to personal use, feel free to contribute. +![gitrat running in the terminal](assets/screenshot.png) + ## Installing #### Cargo 🦀 @@ -58,23 +60,21 @@ files change, and runs `git push` in the background so the UI never freezes. In the commit bar: `Ctrl-W` (or `Option-Delete` with "Use Option as Meta key") deletes a word, `Ctrl-U` clears, and pasting multi-line text is safe (newlines become spaces). -### macOS Terminal.app +### Platforms -Light or dark colours are picked from `COLORFGBG` or, on macOS, the system -appearance (which Terminal.app's default profile follows); override with -`--theme light|dark`. 24-bit colour is used when the terminal advertises it -(`COLORTERM=truecolor`, iTerm2, Ghostty, WezTerm…), otherwise a 256-colour palette. Terminal.app keeps `PageUp`, -`PageDown`, `Home` and `End` for its own scrollback, so every diff action also has a -letter binding. To select text with the mouse while gitrat is running, hold `Fn` -(or start with `--no-mouse`). `git push` never prompts inside the TUI: use the -macOS keychain credential helper or an SSH agent. +gitrat runs on macOS, Linux and Windows; it only needs `git` on the `PATH`. -## Development +Light or dark colours are picked from `COLORFGBG` or, failing that, the system +appearance: macOS dark mode, the GNOME/GTK `color-scheme` setting on Linux, or +the Windows app theme. Override with `--theme light|dark`. 24-bit colour is used +when the terminal advertises it (`COLORTERM=truecolor`, Windows Terminal, iTerm2, +Ghostty, WezTerm…), otherwise a 256-colour palette. -Requires Rust 1.100 (pinned in `mise.toml`): +`git push` never prompts inside the TUI: use a credential helper (macOS keychain, +Git Credential Manager, libsecret…) or an SSH agent. -```shell -mise install -mise test -mise lint -``` +To select text with the mouse while gitrat is running, hold `Fn` on macOS or +`Shift` in most Linux and Windows terminals (or start with `--no-mouse`). + +Terminal.app keeps `PageUp`, `PageDown`, `Home` and `End` for its own scrollback, +so every diff action also has a letter binding. diff --git a/assets/screenshot.png b/assets/screenshot.png new file mode 100644 index 0000000..995fbbc Binary files /dev/null and b/assets/screenshot.png differ diff --git a/src/git.rs b/src/git.rs index a132775..656355a 100644 --- a/src/git.rs +++ b/src/git.rs @@ -545,6 +545,8 @@ pub(crate) mod tests { git(&["config", "user.email", "test@example.com"]); git(&["config", "commit.gpgsign", "false"]); git(&["config", "core.hooksPath", "/dev/null"]); + // Git for Windows defaults to CRLF conversion, which would change diffs. + git(&["config", "core.autocrlf", "false"]); let repo = Repo::discover(&dir).unwrap(); Self { dir, repo } diff --git a/src/main.rs b/src/main.rs index ea84c1f..973f3a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,7 @@ struct Cli { #[arg(long)] no_mouse: bool, - /// Colour scheme for the diff: auto-detects light/dark from the terminal or macOS. + /// Colour scheme for the diff: auto-detects light/dark from the terminal or the OS. #[arg(long, value_enum, default_value_t = theme::ThemeChoice::Auto)] theme: theme::ThemeChoice, } diff --git a/src/theme.rs b/src/theme.rs index 97411f7..94675ec 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -8,7 +8,7 @@ use two_face::theme::EmbeddedThemeName; /// Which colour scheme to use, chosen on the command line. #[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)] pub enum ThemeChoice { - /// Detect from `COLORFGBG` or the macOS appearance setting. + /// Detect from `COLORFGBG` or the system light/dark setting. Auto, /// Colours for a dark terminal background. Dark, @@ -124,6 +124,10 @@ fn detect_truecolor() -> bool { if matches!(colorterm.as_str(), "truecolor" | "24bit") { return true; } + // Windows Terminal supports 24-bit colour but does not set `COLORTERM`. + if std::env::var_os("WT_SESSION").is_some() { + return true; + } let program = std::env::var("TERM_PROGRAM").unwrap_or_default(); matches!( program.as_str(), @@ -139,17 +143,53 @@ fn detect_dark() -> bool { return matches!(bg, 0..=6 | 8); } - // Terminal.app's default profile follows the system appearance. + system_prefers_dark().unwrap_or(true) +} + +/// The desktop's light/dark setting, which most default terminal profiles follow. +fn system_prefers_dark() -> Option { if cfg!(target_os = "macos") { - return Command::new("defaults") - .args(["read", "-g", "AppleInterfaceStyle"]) - .stdin(Stdio::null()) - .stderr(Stdio::null()) - .output() - .is_ok_and(|o| String::from_utf8_lossy(&o.stdout).trim() == "Dark"); + // The key only exists in dark mode, so a failed read means light. + let style = command_stdout("defaults", &["read", "-g", "AppleInterfaceStyle"]); + return Some(style.as_deref() == Some("Dark")); } - true + if cfg!(windows) { + let out = command_stdout( + "reg", + &[ + "query", + r"HKCU\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", + "/v", + "AppsUseLightTheme", + ], + )?; + // `AppsUseLightTheme REG_DWORD 0x0` means dark. + return Some(out.ends_with("0x0")); + } + + // Linux and other Unix desktops: GNOME/GTK expose the preference via gsettings. + let scheme = command_stdout( + "gsettings", + &["get", "org.gnome.desktop.interface", "color-scheme"], + )?; + match scheme.trim_matches('\'') { + "prefer-dark" => Some(true), + "prefer-light" => Some(false), + _ => None, + } +} + +/// Trimmed stdout of a successful command, or `None` if it is missing or fails. +fn command_stdout(program: &str, args: &[&str]) -> Option { + let out = Command::new(program) + .args(args) + .stdin(Stdio::null()) + .stderr(Stdio::null()) + .output() + .ok() + .filter(|o| o.status.success()); + Some(String::from_utf8_lossy(&out?.stdout).trim().to_owned()) } /// Nearest xterm-256 colour (6×6×6 cube or grey ramp).