diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2018-04-20 09:24:14 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-20 09:24:14 +0200 |
commit | ca5e150f3a83dbdf15634ece030ca7bcbd559cc4 (patch) | |
tree | 42dcbf17d00fd5fa3d5d0e6ceab91bfd27745f0a | |
parent | e0decb72943e28109c228290414822cc7dd53620 (diff) | |
parent | e80bf7b57d14879d9659bdbd45468653fcb918d8 (diff) |
Merge pull request #1421 from matthiasbeyer/imag-view/wrapping
imag-view: wrapping
-rw-r--r-- | bin/core/imag-view/Cargo.toml | 1 | ||||
-rw-r--r-- | bin/core/imag-view/src/main.rs | 17 | ||||
-rw-r--r-- | bin/core/imag-view/src/ui.rs | 11 | ||||
-rw-r--r-- | lib/entry/libimagentryview/Cargo.toml | 1 | ||||
-rw-r--r-- | lib/entry/libimagentryview/src/builtin/stdout.rs | 13 | ||||
-rw-r--r-- | lib/entry/libimagentryview/src/lib.rs | 1 |
6 files changed, 41 insertions, 3 deletions
diff --git a/bin/core/imag-view/Cargo.toml b/bin/core/imag-view/Cargo.toml index 62442744..3e4c5125 100644 --- a/bin/core/imag-view/Cargo.toml +++ b/bin/core/imag-view/Cargo.toml @@ -32,6 +32,7 @@ libimagstore = { version = "0.8.0", path = "../../../lib/core/libimagstore" libimagrt = { version = "0.8.0", path = "../../../lib/core/libimagrt" } libimagerror = { version = "0.8.0", path = "../../../lib/core/libimagerror" } libimagentryview = { version = "0.8.0", path = "../../../lib/entry/libimagentryview" } +libimagutil = { version = "0.8.0", path = "../../../lib/etc/libimagutil" } [dependencies.clap] version = "^2.29" diff --git a/bin/core/imag-view/src/main.rs b/bin/core/imag-view/src/main.rs index 0e5b29b6..e8241be6 100644 --- a/bin/core/imag-view/src/main.rs +++ b/bin/core/imag-view/src/main.rs @@ -43,7 +43,9 @@ extern crate libimagentryview; extern crate libimagerror; #[macro_use] extern crate libimagrt; extern crate libimagstore; +extern crate libimagutil; +use std::str::FromStr; use std::collections::BTreeMap; use std::io::Write; use std::io::Read; @@ -82,7 +84,6 @@ fn main() { let view_header = rt.cli().is_present("view-header"); let hide_content = rt.cli().is_present("not-view-content"); - if rt.cli().is_present("in") { let files = entry_ids .into_iter() @@ -170,7 +171,19 @@ fn main() { drop(files); } else { - let viewer = StdoutViewer::new(view_header, !hide_content); + let mut viewer = StdoutViewer::new(view_header, !hide_content); + + if rt.cli().is_present("autowrap") { + let width = rt.cli().value_of("autowrap").unwrap(); // ensured by clap + let width = usize::from_str(width).unwrap_or_else(|e| { + error!("Failed to parse argument to number: autowrap = {:?}", + rt.cli().value_of("autowrap").map(String::from)); + error!("-> {:?}", e); + ::std::process::exit(1) + }); + + viewer.wrap_at(width); + } entry_ids .into_iter() diff --git a/bin/core/imag-view/src/ui.rs b/bin/core/imag-view/src/ui.rs index ef0be8a1..7dbc9696 100644 --- a/bin/core/imag-view/src/ui.rs +++ b/bin/core/imag-view/src/ui.rs @@ -40,6 +40,17 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .args(&["id", "entries-from-stdin"]) .required(true)) + .arg(Arg::with_name("autowrap") + .long("autowrap") + .short("w") + .takes_value(true) + .required(false) + .multiple(false) + .value_name("WIDTH") + .default_value("80") + .validator(::libimagutil::cli_validators::is_integer) + .help("Automatically wrap long lines. Has only an effect when using stdout as output.")) + .arg(Arg::with_name("view-header") .long("header") .short("h") diff --git a/lib/entry/libimagentryview/Cargo.toml b/lib/entry/libimagentryview/Cargo.toml index 3d00b01e..4c702043 100644 --- a/lib/entry/libimagentryview/Cargo.toml +++ b/lib/entry/libimagentryview/Cargo.toml @@ -23,6 +23,7 @@ maintenance = { status = "actively-developed" } log = "0.4.0" toml = "0.4" error-chain = "0.11" +textwrap = "0.9" libimagrt = { version = "0.8.0", path = "../../../lib/core/libimagrt" } libimagstore = { version = "0.8.0", path = "../../../lib/core/libimagstore" } diff --git a/lib/entry/libimagentryview/src/builtin/stdout.rs b/lib/entry/libimagentryview/src/builtin/stdout.rs index 1aa7b97e..5a270745 100644 --- a/lib/entry/libimagentryview/src/builtin/stdout.rs +++ b/lib/entry/libimagentryview/src/builtin/stdout.rs @@ -27,6 +27,7 @@ use error::Result; pub struct StdoutViewer { view_header: bool, view_content: bool, + wrap_content: Option<usize>, } impl StdoutViewer { @@ -35,9 +36,14 @@ impl StdoutViewer { StdoutViewer { view_header: view_header, view_content: view_content, + wrap_content: None, } } + pub fn wrap_at(&mut self, wrap: usize) { + self.wrap_content = Some(wrap) + } + } impl Viewer for StdoutViewer { @@ -48,7 +54,12 @@ impl Viewer for StdoutViewer { } if self.view_content { - println!("{}", e.get_content()); + match self.wrap_content { + Some(limit) => ::textwrap::wrap(e.get_content(), limit).iter().for_each(|line| { + println!("{}", line) + }), + None => println!("{}", e.get_content()), + } } Ok(()) diff --git a/lib/entry/libimagentryview/src/lib.rs b/lib/entry/libimagentryview/src/lib.rs index b543c6f8..170f415c 100644 --- a/lib/entry/libimagentryview/src/lib.rs +++ b/lib/entry/libimagentryview/src/lib.rs @@ -37,6 +37,7 @@ extern crate toml; #[macro_use] extern crate error_chain; +extern crate textwrap; extern crate libimagstore; extern crate libimagrt; |