Skip to content

Commit 6108221

Browse files
feat: add cross-platform pager support for Windows (#491)
- Remove #[cfg(unix)] guards from pager code - Use 'more' as default pager on Windows, 'less -R' on Unix - Respect PAGER environment variable on all platforms - should_use_pager() now works on all platforms Closes #138
1 parent 172a6ba commit 6108221

File tree

1 file changed

+36
-40
lines changed
  • crates/redisctl/src/commands/cloud

1 file changed

+36
-40
lines changed

crates/redisctl/src/commands/cloud/utils.rs

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::io::{self, Write};
99
use tabled::Tabled;
1010
use unicode_segmentation::UnicodeSegmentation;
1111

12-
#[cfg(unix)]
1312
use std::io::IsTerminal;
1413

1514
use crate::cli::OutputFormat;
@@ -55,53 +54,50 @@ pub fn extract_field(value: &Value, field: &str, default: &str) -> String {
5554

5655
/// Output with automatic pager for long content
5756
pub fn output_with_pager(content: &str) {
58-
// Check if we should use a pager (Unix only)
59-
#[cfg(unix)]
60-
{
61-
use std::io::Write;
62-
use std::process::{Command, Stdio};
63-
64-
let lines: Vec<&str> = content.lines().collect();
65-
if should_use_pager(&lines) {
66-
// Get pager command from environment or use default
67-
let pager_cmd = std::env::var("PAGER").unwrap_or_else(|_| "less -R".to_string());
68-
69-
// Split pager command into program and args
70-
let mut parts = pager_cmd.split_whitespace();
71-
let program = parts.next().unwrap_or("less");
72-
let args: Vec<&str> = parts.collect();
73-
74-
// Try to spawn pager process
75-
match Command::new(program)
76-
.args(&args)
77-
.stdin(Stdio::piped())
78-
.spawn()
79-
{
80-
Ok(mut child) => {
81-
// Write content to pager's stdin
82-
if let Some(mut stdin) = child.stdin.take() {
83-
let _ = stdin.write_all(content.as_bytes());
84-
let _ = stdin.flush();
85-
// Close stdin to signal EOF to pager
86-
drop(stdin);
87-
}
88-
89-
// Wait for pager to finish
90-
let _ = child.wait();
91-
return;
92-
}
93-
Err(_) => {
94-
// If pager fails to spawn, fall through to regular println
57+
use std::io::Write;
58+
use std::process::{Command, Stdio};
59+
60+
let lines: Vec<&str> = content.lines().collect();
61+
if should_use_pager(&lines) {
62+
// Get pager command from environment or use platform-specific default
63+
let default_pager = if cfg!(windows) { "more" } else { "less -R" };
64+
let pager_cmd = std::env::var("PAGER").unwrap_or_else(|_| default_pager.to_string());
65+
66+
// Split pager command into program and args
67+
let mut parts = pager_cmd.split_whitespace();
68+
let default_program = if cfg!(windows) { "more" } else { "less" };
69+
let program = parts.next().unwrap_or(default_program);
70+
let args: Vec<&str> = parts.collect();
71+
72+
// Try to spawn pager process
73+
match Command::new(program)
74+
.args(&args)
75+
.stdin(Stdio::piped())
76+
.spawn()
77+
{
78+
Ok(mut child) => {
79+
// Write content to pager's stdin
80+
if let Some(mut stdin) = child.stdin.take() {
81+
let _ = stdin.write_all(content.as_bytes());
82+
let _ = stdin.flush();
83+
// Close stdin to signal EOF to pager
84+
drop(stdin);
9585
}
86+
87+
// Wait for pager to finish
88+
let _ = child.wait();
89+
return;
90+
}
91+
Err(_) => {
92+
// If pager fails to spawn, fall through to regular println
9693
}
9794
}
9895
}
9996

10097
println!("{}", content);
10198
}
10299

103-
/// Check if we should use a pager for output (Unix only)
104-
#[cfg(unix)]
100+
/// Check if we should use a pager for output
105101
fn should_use_pager(lines: &[&str]) -> bool {
106102
// Only page if we're in a TTY
107103
if !std::io::stdout().is_terminal() {

0 commit comments

Comments
 (0)