summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2018-04-20 09:24:14 +0200
committerGitHub <noreply@github.com>2018-04-20 09:24:14 +0200
commitca5e150f3a83dbdf15634ece030ca7bcbd559cc4 (patch)
tree42dcbf17d00fd5fa3d5d0e6ceab91bfd27745f0a
parente0decb72943e28109c228290414822cc7dd53620 (diff)
parente80bf7b57d14879d9659bdbd45468653fcb918d8 (diff)
downloadimag-ca5e150f3a83dbdf15634ece030ca7bcbd559cc4.zip
imag-ca5e150f3a83dbdf15634ece030ca7bcbd559cc4.tar.gz
Merge pull request #1421 from matthiasbeyer/imag-view/wrapping
imag-view: wrapping
-rw-r--r--bin/core/imag-view/Cargo.toml1
-rw-r--r--bin/core/imag-view/src/main.rs17
-rw-r--r--bin/core/imag-view/src/ui.rs11
-rw-r--r--lib/entry/libimagentryview/Cargo.toml1
-rw-r--r--lib/entry/libimagentryview/src/builtin/stdout.rs13
-rw-r--r--lib/entry/libimagentryview/src/lib.rs1
6 files changed, 41 insertions, 3 deletions
diff --git a/bin/core/imag-view/Cargo.toml b/bin/core/imag-view/Cargo.toml
index 6244274..3e4c512 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 0e5b29b..e8241be 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 ef0be8a..7dbc969 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 3d00b01..4c70204 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 1aa7b97..5a27074 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 b543c6f..170f415 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;