summaryrefslogtreecommitdiff
path: root/utils/src/display/pager.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-03-12 15:54:59 +0800
committer魏曹先生 <1992414357@qq.com>2026-03-12 15:54:59 +0800
commit9d812580557cdc343378816cd65678b8aa75d944 (patch)
treeb1a3397e38d9620a487aed409fc94310f101bc27 /utils/src/display/pager.rs
parent0a95bae451c1847f4f0b9601e60959f4e8e6b669 (diff)
Add lang field to command context and reorganize utils modules
Diffstat (limited to 'utils/src/display/pager.rs')
-rw-r--r--utils/src/display/pager.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/utils/src/display/pager.rs b/utils/src/display/pager.rs
new file mode 100644
index 0000000..79c2ccb
--- /dev/null
+++ b/utils/src/display/pager.rs
@@ -0,0 +1,37 @@
+use crate::env::pager::get_default_pager;
+use tokio::{fs, process::Command};
+
+/// Show text using the system pager (less)
+/// Opens the system pager (less) with the given text content written to the specified file
+/// If less is not found, directly outputs the content to stdout
+pub async fn pager(
+ content: impl AsRef<str>,
+ cache_file: impl AsRef<std::path::Path>,
+) -> Result<(), std::io::Error> {
+ let content_str = content.as_ref();
+ let cache_path = cache_file.as_ref();
+
+ // Write content to cache file
+ fs::write(cache_path, content_str).await?;
+
+ // Get the default pager
+ let pager_cmd = get_default_pager().await;
+
+ // Try to use the pager
+ let status = Command::new(&pager_cmd).arg(cache_path).status().await;
+
+ match status {
+ Ok(status) if status.success() => Ok(()),
+ _ => {
+ // If pager failed, output directly to stdout
+ use tokio::io::{self, AsyncWriteExt};
+ let mut stdout = io::stdout();
+ stdout
+ .write_all(content_str.as_bytes())
+ .await
+ .expect("Failed to write content");
+ stdout.flush().await.expect("Failed to flush stdout");
+ Ok(())
+ }
+ }
+}