Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Add [select] pager config with backward-compatible auto-detection (#449)

Maximilian Roosba012899d9e0
diff --git a/dev/config.example.toml b/dev/config.example.toml
index 8584078df..fbaf9713e 100644
--- a/dev/config.example.toml
+++ b/dev/config.example.toml
@@ -64,6 +64,13 @@ rebase = true # Rebase onto target before merging
remove = true # Remove worktree after merge
verify = true # Run project hooks
+# Select Command Defaults
+# Configure pager behavior for `wt select` diff previews
+[select]
+# Pager command with flags for diff preview (overrides git's core.pager)
+# Use this to specify pager flags needed for non-TTY contexts
+# Example: pager = "delta --paging=never"
+
# Approved Commands
# Commands approved for project hooks in this repo
# Auto-populated when approving hooks (prompt on first run) or via `wt hook approvals add`
diff --git a/docs/content/config.md b/docs/content/config.md
index d9b020b01..0ed42d2f4 100644
--- a/docs/content/config.md
+++ b/docs/content/config.md
@@ -348,6 +348,13 @@ rebase = true # Rebase onto target before merging
remove = true # Remove worktree after merge
verify = true # Run project hooks
+# Select Command Defaults
+# Configure pager behavior for `wt select` diff previews
+[select]
+# Pager command with flags for diff preview (overrides git's core.pager)
+# Use this to specify pager flags needed for non-TTY contexts
+# Example: pager = "delta --paging=never"
+
# Approved Commands
# Commands approved for project hooks in this repo
# Auto-populated when approving hooks (prompt on first run) or via `wt hook approvals add`
diff --git a/src/commands/select.rs b/src/commands/select.rs
index 8df9718b5..7ccdeefff 100644
--- a/src/commands/select.rs
+++ b/src/commands/select.rs
@@ -25,10 +25,25 @@ use crate::pager::{git_config_pager, parse_pager_value};
static CACHED_PAGER: OnceLock<Option<String>> = OnceLock::new();
/// Get the cached pager command, initializing if needed.
+///
+/// Precedence (highest to lowest):
+/// 1. `[select] pager` in user config (explicit override, used as-is)
+/// 2. `GIT_PAGER` environment variable (with auto-detection applied)
+/// 3. `core.pager` git config (with auto-detection applied)
fn get_diff_pager() -> Option<&'static String> {
CACHED_PAGER
.get_or_init(|| {
- // GIT_PAGER takes precedence - if set (even to "cat" or empty), don't fall back
+ // Check user config first for explicit pager override
+ // When set, use exactly as specified (no auto-detection)
+ if let Ok(config) = WorktrunkConfig::load()
+ && let Some(select_config) = config.select
+ && let Some(pager) = select_config.pager
+ && !pager.trim().is_empty()
+ {
+ return Some(pager);
+ }
+
+ // GIT_PAGER takes precedence over core.pager
if let Ok(pager) = std::env::var("GIT_PAGER") {
return parse_pager_value(&pager);
}
@@ -44,10 +59,8 @@ fn get_diff_pager() -> Option<&'static String> {
/// Some pagers like delta and bat spawn `less` by default, which hangs in
/// non-TTY contexts like skim's preview panel. These need `--paging=never`.
///
-/// TODO: Replace this hardcoded detection with a config option like
-/// `select.pager = "delta --paging=never"` so users can specify their own
-/// pager command with appropriate flags. This would eliminate the need to
-/// maintain a list of pagers that need special handling.
+/// Used only when user hasn't set `[select] pager` config explicitly.
+/// When config is set, that value is used as-is without modification.
fn pager_needs_paging_disabled(pager_cmd: &str) -> bool {
// Split on whitespace to get the command name, then extract basename
// Uses extract_filename_from_path for consistent handling of Windows paths and .exe
@@ -64,6 +77,15 @@ fn pager_needs_paging_disabled(pager_cmd: &str) -> bool {
})
}
+/// Check if user has explicitly configured a select-specific pager.
+fn has_explicit_pager_config() -> bool {
+ WorktrunkConfig::load()
+ .ok()
+ .and_then(|config| config.select)
+ .and_then(|select| select.pager)
+ .is_some_and(|p| !p.trim().is_empty())
+}
+
/// Maximum time to wait for pager to complete.
///
/// Pager blocking can freeze skim's event loop, making the UI unresponsive.
@@ -85,15 +107,19 @@ const MIN_PREVIEW_LINES: usize = 5;
/// Run git diff piped directly through the pager as a streaming pipeline.
///
/// Runs `git <args> | pager` as a single shell command, avoiding intermediate
-/// buffering. For pagers that spawn their own sub-pager (delta, bat), adds
-/// `--paging=never` to prevent them from spawning less.
-/// Returns None if pipeline fails or times out (caller should fall back to raw diff).
+/// buffering. Returns None if pipeline fails or times out (caller should fall back to raw diff).
+///
+/// When `[select] pager` is not configured, automatically appends `--paging=never` for
+/// delta/bat/batcat pagers to prevent hangs. To override this behavior, set an explicit
+/// pager command in config: `[select] pager = "delta"` (or with custom flags).
fn run_git_diff_with_pager(git_args: &[&str], pager_cmd: &str) -> Option<String> {
// Note: pager_cmd is expected to be valid shell code (like git's core.pager).
// Users with paths containing special chars must quote them in their config.
- // Some pagers spawn `less` by default which hangs in non-TTY contexts
- let pager_with_args = if pager_needs_paging_disabled(pager_cmd) {
+ // Apply auto-detection only when user hasn't set explicit config
+ // If config is set, use the value as-is (user has full control)
+ let pager_with_args = if !has_explicit_pager_config() && pager_needs_paging_disabled(pager_cmd)
+ {
format!("{} --paging=never", pager_cmd)
} else {
pager_cmd.to_string()
@@ -1061,6 +1087,13 @@ mod tests {
assert!(pager_needs_paging_disabled("Delta.EXE"));
}
+ #[test]
+ fn test_has_explicit_pager_config() {
+ // This function loads real config, so we just test that it doesn't panic
+ // The behavior is covered by integration tests that set actual config
+ let _ = has_explicit_pager_config();
+ }
+
#[test]
fn test_render_preview_tabs_working_tree_mode() {
let output = WorktreeSkimItem::render_preview_tabs(PreviewMode::WorkingTree);
diff --git a/src/config/user.rs b/src/config/user.rs
index e2de4bf25..ad7d9047e 100644
--- a/src/config/user.rs
+++ b/src/config/user.rs
@@ -153,6 +153,10 @@ pub struct WorktrunkConfig {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub merge: Option<MergeConfig>,
+ /// Configuration for the `wt select` command
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub select: Option<SelectConfig>,
+
// =========================================================================
// User-level hooks (same syntax as project hooks, run before project hooks)
// =========================================================================
@@ -297,6 +301,19 @@ pub struct MergeConfig {
pub verify: Option<bool>,
}
+/// Configuration for the `wt select` command
+#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
+pub struct SelectConfig {
+ /// Pager command with flags for diff preview
+ ///
+ /// Overrides git's core.pager for the select command's preview panel.
+ /// Use this to specify pager flags needed for non-TTY contexts.
+ ///
+ /// Example: `pager = "delta --paging=never"`
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub pager: Option<String>,
+}
+
/// Default worktree path template
fn default_worktree_path() -> String {
"../{{ repo }}.{{ branch | sanitize }}".to_string()
diff --git a/tests/integration_tests/mod.rs b/tests/integration_tests/mod.rs
index 27c4e260f..c6c11101a 100644
--- a/tests/integration_tests/mod.rs
+++ b/tests/integration_tests/mod.rs
@@ -44,6 +44,7 @@ pub mod remove;
pub mod repository;
pub mod security;
pub mod select;
+pub mod select_config;
pub mod shell_integration_prompt;
pub mod shell_integration_windows;
pub mod shell_wrapper;
diff --git a/tests/integration_tests/select_config.rs b/tests/integration_tests/select_config.rs
new file mode 100644
index 000000000..8c244d582
--- /dev/null
+++ b/tests/integration_tests/select_config.rs
@@ -0,0 +1,43 @@
+use worktrunk::config::WorktrunkConfig;
+
+#[test]
+fn test_select_pager_config_deserialization() {
+ // Verify that SelectConfig with pager field deserializes correctly
+ let config_content = r#"
+[select]
+pager = "test-pager --custom-flag"
+"#;
+
+ let config: WorktrunkConfig = toml::from_str(config_content).unwrap();
+
+ assert!(config.select.is_some(), "Select config should be present");
+ let select = config.select.unwrap();
+ assert_eq!(select.pager, Some("test-pager --custom-flag".to_string()));
+}
+
+#[test]
+fn test_select_pager_config_empty_string() {
+ // Verify that empty string is valid TOML and deserializes
+ let config_content = r#"
+[select]
+pager = ""
+"#;
+
+ let config: WorktrunkConfig = toml::from_str(config_content).unwrap();
+
+ assert!(config.select.is_some(), "Select config should be present");
+ let select = config.select.unwrap();
+ assert_eq!(select.pager, Some("".to_string()));
+}
+
+#[test]
+fn test_select_config_optional() {
+ // Verify that config without [select] section is still valid
+ let config_content = r#"
+[list]
+full = true
+"#;
+
+ let config: WorktrunkConfig = toml::from_str(config_content).unwrap();
+ assert!(config.select.is_none(), "Select config should be absent");
+}
diff --git a/tests/snapshots/integration__integration_tests__help__help_config_create.snap b/tests/snapshots/integration__integration_tests__help__help_config_create.snap
index 641bd039b..bd64d043e 100644
--- a/tests/snapshots/integration__integration_tests__help__help_config_create.snap
+++ b/tests/snapshots/integration__integration_tests__help__help_config_create.snap
@@ -112,6 +112,13 @@ Creates ~/.config/worktrunk/config.toml with the following content:
remove = true # Remove worktree after merge
verify = true # Run project hooks

+ # Select Command Defaults
+ # Configure pager behavior for `wt select` diff previews
+ [select]
+ # Pager command with flags for diff preview (overrides git's core.pager)
+ # Use this to specify pager flags needed for non-TTY contexts
+ # Example: pager = "delta --paging=never"
+ 
# Approved Commands
# Commands approved for project hooks in this repo
# Auto-populated when approving hooks (prompt on first run) or via `wt hook approvals add`