Skip to content

Commit 702ea4b

Browse files
committed
streampager: allow running Streampager without reading its config files
Currently, there is no way for a calling program (like `sl` or `jj`) to start streampager without it trying to read `$CONFIG/streampager/streampager.toml` and the environment variables `SP_INTERFACE_MODE`, `SP_SCROLL_PAST_EOF`, and `SP_READ_AHEAD_LINES`. This is undesireable if we'd like to control Streampager's behavior fully from `jj` config (or `sl` config). Sapling currently overrides the most important Streampager config from its own config anyway. I'd like there to be a way to invoke Streampager with a given config. I carefully implemented it so that no changes to Sapling are required, though I think it'd also benefit from using the API I introduce here (and the old UI could be deprecated). Another, less invasive, possibility would be to simply make `Pager::config` public. However, this would mean that Streampager would uselessly read `$CONFIG/streampager/streampager.toml` even if the config is later fully overriden by `jj`. As an aside, the docs for the config from https://github.com/markbt/streampager#example-configuration do not seem to be fully correct, setting `interface_mode = "delayed"` as described there did not work for me. However, I'd rather not use that configuration method at all for my purposes with `jj`. Cc: jj-vcs/jj#4203 (comment), @yuja. As ever, thanks for making it convenient for us to use Streampager in `jj`! :)
1 parent 065bfde commit 702ea4b

File tree

2 files changed

+61
-16
lines changed

2 files changed

+61
-16
lines changed

Diff for: eden/scm/lib/third-party/streampager/src/config.rs

+4
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ impl Config {
233233
}
234234
self
235235
}
236+
237+
pub(crate) fn from_user_config() -> Self {
238+
Self::from_config_file().with_env()
239+
}
236240
}
237241

238242
fn parse_bool(value: &str) -> Option<bool> {

Diff for: eden/scm/lib/third-party/streampager/src/pager.rs

+57-16
Original file line numberDiff line numberDiff line change
@@ -61,42 +61,84 @@ fn termcaps() -> Result<Capabilities> {
6161
}
6262

6363
impl Pager {
64-
/// Build a `Pager` using the system terminal.
64+
/// Build a `Pager` using the system terminal and config read from the
65+
/// config file and environment variables.
6566
pub fn new_using_system_terminal() -> Result<Self> {
66-
Self::new_with_terminal_func(move |caps| SystemTerminal::new(caps).map_err(Error::Termwiz))
67+
Self::new_using_system_terminal_with_config(Config::from_user_config())
6768
}
6869

69-
/// Build a `Pager` using the system stdio.
70+
/// Build a `Pager` using the system terminal and the given config
71+
pub fn new_using_system_terminal_with_config(config: Config) -> Result<Self> {
72+
Self::new_with_terminal_func_with_config(
73+
move |caps| SystemTerminal::new(caps).map_err(Error::Termwiz),
74+
config,
75+
)
76+
}
77+
78+
/// Build a `Pager` using the system stdio and config read from the config
79+
/// file and environment variables.
7080
pub fn new_using_stdio() -> Result<Self> {
71-
Self::new_with_terminal_func(move |caps| {
72-
SystemTerminal::new_from_stdio(caps).map_err(Error::Termwiz)
73-
})
81+
Self::new_using_stdio_with_config(Config::from_user_config())
82+
}
83+
84+
/// Build a `Pager` using the system stdio and the given config.
85+
pub fn new_using_stdio_with_config(config: Config) -> Result<Self> {
86+
Self::new_with_terminal_func_with_config(
87+
move |caps| SystemTerminal::new_from_stdio(caps).map_err(Error::Termwiz),
88+
config,
89+
)
7490
}
7591

7692
#[cfg(unix)]
77-
/// Build a `Pager` using the specified terminal input and output.
93+
/// Build a `Pager` using the specified terminal input and output and config
94+
/// read from the config file and environment variables.
7895
pub fn new_with_input_output(
7996
input: &impl std::os::unix::io::AsRawFd,
8097
output: &impl std::os::unix::io::AsRawFd,
8198
) -> Result<Self> {
82-
Self::new_with_terminal_func(move |caps| {
83-
SystemTerminal::new_with(caps, input, output).map_err(Error::Termwiz)
84-
})
99+
Self::new_with_input_output_with_config(input, output, Config::from_user_config())
100+
}
101+
102+
#[cfg(unix)]
103+
/// Build a `Pager` using the specified terminal input and output and the
104+
/// given config.
105+
pub fn new_with_input_output_with_config(
106+
input: &impl std::os::unix::io::AsRawFd,
107+
output: &impl std::os::unix::io::AsRawFd,
108+
config: Config,
109+
) -> Result<Self> {
110+
Self::new_with_terminal_func_with_config(
111+
move |caps| SystemTerminal::new_with(caps, input, output).map_err(Error::Termwiz),
112+
config,
113+
)
85114
}
86115

87116
#[cfg(windows)]
88-
/// Build a `Pager` using the specified terminal input and output.
117+
/// Build a `Pager` using the specified terminal input and output and config
118+
/// read from the config file and environment variables.
89119
pub fn new_with_input_output(
90120
input: impl std::io::Read + termwiz::istty::IsTty + std::os::windows::io::AsRawHandle,
91121
output: impl std::io::Write + termwiz::istty::IsTty + std::os::windows::io::AsRawHandle,
92122
) -> Result<Self> {
93-
Self::new_with_terminal_func(move |caps| {
94-
SystemTerminal::new_with(caps, input, output).map_err(Error::Termwiz)
95-
})
123+
Self::new_with_input_output_with_config(input, output, Config::from_user_config())
124+
}
125+
126+
#[cfg(windows)]
127+
/// Build a `Pager` using the specified terminal input and output and the given config.
128+
pub fn new_with_input_output_with_config(
129+
input: impl std::io::Read + termwiz::istty::IsTty + std::os::windows::io::AsRawHandle,
130+
output: impl std::io::Write + termwiz::istty::IsTty + std::os::windows::io::AsRawHandle,
131+
config: Config,
132+
) -> Result<Self> {
133+
Self::new_with_terminal_func_with_config(
134+
move |caps| SystemTerminal::new_with(caps, input, output).map_err(Error::Termwiz),
135+
config,
136+
)
96137
}
97138

98-
fn new_with_terminal_func(
139+
fn new_with_terminal_func_with_config(
99140
create_term: impl FnOnce(Capabilities) -> Result<SystemTerminal>,
141+
config: Config,
100142
) -> Result<Self> {
101143
let caps = termcaps()?;
102144
let term = create_term(caps.clone())?;
@@ -105,7 +147,6 @@ impl Pager {
105147
let files = Vec::new();
106148
let error_files = VecMap::new();
107149
let progress = None;
108-
let config = Config::from_config_file().with_env();
109150

110151
Ok(Self {
111152
term,

0 commit comments

Comments
 (0)