Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 πŸ¦€
Expand Down Expand Up @@ -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.
Binary file added assets/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
58 changes: 49 additions & 9 deletions src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand All @@ -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<bool> {
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<String> {
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).
Expand Down
Loading