Add --width and --max-width options
This patch adds the --width option to select a fixed output width and the --max-width to set a maximum output width if the width is dynamically set using the terminal size.
This commit is contained in:
parent
1a518d7509
commit
28c025fd4f
@ -19,6 +19,8 @@ SPDX-License-Identifier: MIT
|
||||
configuration file can be used to set defaults for the command-line
|
||||
options.
|
||||
- Add the `--config-file [file]` option to set a custom configuration file.
|
||||
- Add the `--width [width]` option to set a fixed output width and the
|
||||
`--max-width [max]` option to set the maximum output width.
|
||||
- Improve line break rendering when displaying code.
|
||||
- Add integration test suite.
|
||||
|
||||
|
15
src/args.rs
15
src/args.rs
@ -96,6 +96,21 @@ pub struct ViewerArgs {
|
||||
/// Default value: base16-eighties.dark.
|
||||
#[structopt(long)]
|
||||
pub theme: Option<String>,
|
||||
|
||||
/// The width of the text output
|
||||
///
|
||||
/// Per default, rusty-man sets the width of the text output based on the width of the terminal
|
||||
/// with the maximum width given by --max-width. If this option is set, it uses the given
|
||||
/// width instead.
|
||||
#[structopt(long)]
|
||||
pub width: Option<usize>,
|
||||
|
||||
/// The maximum width of the text output
|
||||
///
|
||||
/// Unless the --width option is set, rusty-man sets the width of the text output based on the
|
||||
/// width of the terminal with the maximum width set with this optioj.
|
||||
#[structopt(long, default_value = "100")]
|
||||
pub max_width: usize,
|
||||
}
|
||||
|
||||
impl Args {
|
||||
|
@ -40,10 +40,12 @@ pub fn get_default() -> Box<dyn Viewer> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_line_length() -> usize {
|
||||
if let Ok((cols, _)) = crossterm::terminal::size() {
|
||||
cmp::min(cols.into(), 100)
|
||||
pub fn get_line_length(args: &args::ViewerArgs) -> usize {
|
||||
if let Some(width) = args.width {
|
||||
width
|
||||
} else if let Ok((cols, _)) = crossterm::terminal::size() {
|
||||
cmp::min(cols.into(), args.max_width)
|
||||
} else {
|
||||
100
|
||||
args.max_width
|
||||
}
|
||||
}
|
||||
|
@ -21,9 +21,9 @@ struct Decorator {
|
||||
}
|
||||
|
||||
impl super::Printer for PlainTextRenderer {
|
||||
fn new(_args: args::ViewerArgs) -> anyhow::Result<Self> {
|
||||
fn new(args: args::ViewerArgs) -> anyhow::Result<Self> {
|
||||
Ok(Self {
|
||||
line_length: viewer::get_line_length(),
|
||||
line_length: viewer::get_line_length(&args),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -65,7 +65,7 @@ impl super::Printer for RichTextRenderer {
|
||||
.remove(theme_name)
|
||||
.with_context(|| format!("Could not find theme {}", theme_name))?;
|
||||
Ok(Self {
|
||||
line_length: viewer::get_line_length(),
|
||||
line_length: viewer::get_line_length(&args),
|
||||
highlight: !args.no_syntax_highlight,
|
||||
syntax_set: syntect::parsing::SyntaxSet::load_defaults_newlines(),
|
||||
theme,
|
||||
|
@ -38,6 +38,7 @@ fn run(args: &[&str]) -> assert_cmd::assert::Assert {
|
||||
.args(&["--no-default-sources", "--source"])
|
||||
.arg(dir.path().join("doc"))
|
||||
.args(&["--viewer", "plain"])
|
||||
.args(&["--width", "100"])
|
||||
.args(args)
|
||||
.assert()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user