Skip to content
Open
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
36 changes: 34 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ crossterm = {version = "0.29", optional = false}
chrono = {version = "0.4", optional = false}
textwrap = {version = "0.16", default-features = false, optional = false, features = ["unicode-width"]}
gleisbau = "0.7.5"
pager = "0.16.1"
53 changes: 52 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ use gleisbau::settings::{
};
use itertools::enumerate;
use platform_dirs::AppDirs;
use std::path::PathBuf;
use std::borrow::Borrow;
use std::io::IsTerminal;
use std::path::PathBuf;
use std::rc::Rc;
use std::str::FromStr;
use std::time::Instant;
Expand Down Expand Up @@ -56,6 +57,7 @@ fn from_args() -> Result<(), String> {
ses.svg,
ses.commit_limit,
ses.refspecs,
ses.pager,
)
}

Expand Down Expand Up @@ -134,6 +136,13 @@ fn match_args() -> ArgMatches {
.required(false)
.num_args(1),
)
.arg(
Arg::new("no-pager")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you happen to know if there is a NO_PAGER or other env var equivalent to the fairly standardized NO_COLOR that can disable this universally in a shell?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the more common thing people do is PAGER=cat in front of the command (or GIT_PAGER=cat of course). I cannot remember that I saw NO_PAGER ever... 🤔

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I've certainly used the cat stand in a time or two myself. I just looked around and it seems like you're right and there isn't a widely recognized env var far this. SYSTEMD_PAGER and MANPAGER are worth having in a back pocked along with GIT_PAGER, but nothing I see is generic and should be implemented here. So carry on!

.long("no-pager")
.help("Use no pager (print everything at once without prompt).")
.required(false)
.num_args(0),
)
.arg(
Arg::new("refspecs")
.help(
Expand Down Expand Up @@ -210,6 +219,11 @@ fn configure_session(ses: &mut Session, matches: &ArgMatches) -> Result<bool, St
.map(|vals| vals.cloned().collect())
.unwrap_or_default();

ses.pager = matches
.get_one::<bool>("no-pager")
.map(|b| !*b)
.unwrap_or(true);

Ok(run_application)
}

Expand All @@ -225,6 +239,7 @@ struct Session {
pub svg: bool,
pub commit_limit: Option<usize>,
pub refspecs: Vec<String>,
pub pager: bool,
}

impl Session {
Expand All @@ -241,6 +256,8 @@ impl Session {
svg: false,
commit_limit: None,
refspecs: Vec::new(),

pager: true,
}
}
}
Expand Down Expand Up @@ -620,6 +637,7 @@ fn run(
svg: bool,
max_commits: Option<usize>,
refspecs: Vec<String>,
pager: bool,
) -> Result<(), String> {
let now = Instant::now();
let mut graph_builder = GraphBuilder::new()
Expand Down Expand Up @@ -665,6 +683,13 @@ fn run(
if svg {
println!("{}", print_svg(&graph, settings)?);
} else {
if pager && should_page() {
let pager_command = git_pager();

let mut pager = pager::Pager::with_pager(&pager_command);
pager.setup();
}

let (g_lines, t_lines, _indices) = print_unicode(&graph, settings)?;
print_unpaged(&g_lines, &t_lines);
};
Expand All @@ -682,6 +707,32 @@ fn run(
Ok(())
}

fn git_pager() -> String {
std::env::var("GIT_PAGER")
.ok()
.or_else(|| {
std::process::Command::new("git")
.args(["config", "--get", "core.pager"])
.output()
.ok()
.filter(|o| o.status.success())
.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_owned())
.filter(|s| !s.is_empty())
})
.or_else(|| std::env::var("PAGER").ok())
.unwrap_or_else(|| "less".to_owned())
}

fn should_page() -> bool {
if !std::io::stdout().is_terminal() {
return false;
}

let (width, height) = crossterm::terminal::size().unwrap_or((80, 24));

width > 0 && height > 0
}

/// Print the graph, un-paged.
fn print_unpaged(graph_lines: &[String], text_lines: &[String]) {
for (g_line, t_line) in graph_lines.iter().zip(text_lines.iter()) {
Expand Down
Loading