Merge pull request #1421 from matthiasbeyer/imag-view/wrapping

imag-view: wrapping
This commit is contained in:
Matthias Beyer 2018-04-20 09:24:14 +02:00 committed by GitHub
commit ca5e150f3a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 3 deletions

View file

@ -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"

View file

@ -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()

View file

@ -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")

View file

@ -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" }

View file

@ -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(())

View file

@ -37,6 +37,7 @@
extern crate toml;
#[macro_use] extern crate error_chain;
extern crate textwrap;
extern crate libimagstore;
extern crate libimagrt;