summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2018-04-25 10:01:36 +0200
committerGitHub <noreply@github.com>2018-04-25 10:01:36 +0200
commit0e567e235823108de81cbf15cd385d488691ec50 (patch)
tree439ee16dd18355aa8f9bf56a8cf212aab453782d
parent2ac4fa42b3135b3b93be9be1aeabd211ba0410ab (diff)
parent95a48166045e924d84b25d4bbbce5f404769d352 (diff)
downloadimag-0e567e235823108de81cbf15cd385d488691ec50.zip
imag-0e567e235823108de81cbf15cd385d488691ec50.tar.gz
Merge pull request #1445 from matthiasbeyer/imag-view/seperator
imag-view argument to put seperator between shown entries
-rw-r--r--bin/core/imag-view/src/main.rs47
-rw-r--r--bin/core/imag-view/src/ui.rs10
2 files changed, 52 insertions, 5 deletions
diff --git a/bin/core/imag-view/src/main.rs b/bin/core/imag-view/src/main.rs
index bc9e83b..5cafb00 100644
--- a/bin/core/imag-view/src/main.rs
+++ b/bin/core/imag-view/src/main.rs
@@ -61,6 +61,8 @@ use libimagrt::runtime::Runtime;
use libimagerror::str::ErrFromStr;
use libimagerror::trace::MapErrTrace;
use libimagerror::iter::TraceIterator;
+use libimagerror::io::ToExitCode;
+use libimagerror::exit::ExitUnwrap;
use libimagentryview::builtin::stdout::StdoutViewer;
use libimagentryview::builtin::md::MarkdownViewer;
use libimagentryview::viewer::Viewer;
@@ -185,10 +187,33 @@ fn main() {
let out = rt.stdout();
let mut outlock = out.lock();
+ let basesep = if rt.cli().occurrences_of("seperator") != 0 { // checker for default value
+ rt.cli().value_of("seperator").map(String::from)
+ } else {
+ None
+ };
+
+ let mut sep_width = 80; // base width, automatically overridden by wrap width
+
+ // Helper to build the seperator with a base string `sep` and a `width`
+ let build_seperator = |sep: String, width: usize| -> String {
+ sep.repeat(width / sep.len())
+ };
+
if rt.cli().is_present("compile-md") {
- let viewer = MarkdownViewer::new(&rt);
- for entry in iter {
- viewer.view_entry(&entry, &mut outlock).map_err_trace_exit_unwrap(1);
+ let viewer = MarkdownViewer::new(&rt);
+ let seperator = basesep.map(|s| build_seperator(s, sep_width));
+
+ for (n, entry) in iter.enumerate() {
+ if n != 0 {
+ seperator
+ .as_ref()
+ .map(|s| writeln!(outlock, "{}", s).to_exit_code().unwrap_or_exit());
+ }
+
+ viewer
+ .view_entry(&entry, &mut outlock)
+ .map_err_trace_exit_unwrap(1);
}
} else {
let mut viewer = StdoutViewer::new(view_header, !hide_content);
@@ -202,11 +227,23 @@ fn main() {
::std::process::exit(1)
});
+ // Copying this value over, so that the seperator has the right len as well
+ sep_width = width;
+
viewer.wrap_at(width);
}
- for entry in iter {
- viewer.view_entry(&entry, &mut outlock).map_err_trace_exit_unwrap(1);
+ let seperator = basesep.map(|s| build_seperator(s, sep_width));
+ for (n, entry) in iter.enumerate() {
+ if n != 0 {
+ seperator
+ .as_ref()
+ .map(|s| writeln!(outlock, "{}", s).to_exit_code().unwrap_or_exit());
+ }
+
+ viewer
+ .view_entry(&entry, &mut outlock)
+ .map_err_trace_exit_unwrap(1);
}
}
}
diff --git a/bin/core/imag-view/src/ui.rs b/bin/core/imag-view/src/ui.rs
index 157a701..c97bd3f 100644
--- a/bin/core/imag-view/src/ui.rs
+++ b/bin/core/imag-view/src/ui.rs
@@ -73,6 +73,16 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.conflicts_with("not-view-content")
.conflicts_with("autowrap")) // markdown viewer does not support wrapping
+ .arg(Arg::with_name("seperator")
+ .long("seperate")
+ .short("s")
+ .required(false)
+ .takes_value(true)
+ .value_name("SEPCHR")
+ .default_value("-")
+ .help("Do seperate entries with a string if viewing multiple entries"))
+
+
.arg(Arg::with_name("in")
.long("in")
.takes_value(true)