Implement imag-view argument to put seperator between shown entries

This commit is contained in:
Matthias Beyer 2018-04-24 22:47:46 +02:00
parent 16747aa257
commit 95a4816604
2 changed files with 52 additions and 5 deletions

View file

@ -61,6 +61,8 @@ use libimagrt::runtime::Runtime;
use libimagerror::str::ErrFromStr; use libimagerror::str::ErrFromStr;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagerror::iter::TraceIterator; use libimagerror::iter::TraceIterator;
use libimagerror::io::ToExitCode;
use libimagerror::exit::ExitUnwrap;
use libimagentryview::builtin::stdout::StdoutViewer; use libimagentryview::builtin::stdout::StdoutViewer;
use libimagentryview::builtin::md::MarkdownViewer; use libimagentryview::builtin::md::MarkdownViewer;
use libimagentryview::viewer::Viewer; use libimagentryview::viewer::Viewer;
@ -185,10 +187,33 @@ fn main() {
let out = rt.stdout(); let out = rt.stdout();
let mut outlock = out.lock(); 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") { if rt.cli().is_present("compile-md") {
let viewer = MarkdownViewer::new(&rt); let viewer = MarkdownViewer::new(&rt);
for entry in iter { let seperator = basesep.map(|s| build_seperator(s, sep_width));
viewer.view_entry(&entry, &mut outlock).map_err_trace_exit_unwrap(1);
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 { } else {
let mut viewer = StdoutViewer::new(view_header, !hide_content); let mut viewer = StdoutViewer::new(view_header, !hide_content);
@ -202,11 +227,23 @@ fn main() {
::std::process::exit(1) ::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); viewer.wrap_at(width);
} }
for entry in iter { let seperator = basesep.map(|s| build_seperator(s, sep_width));
viewer.view_entry(&entry, &mut outlock).map_err_trace_exit_unwrap(1); 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);
} }
} }
} }

View file

@ -73,6 +73,16 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.conflicts_with("not-view-content") .conflicts_with("not-view-content")
.conflicts_with("autowrap")) // markdown viewer does not support wrapping .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") .arg(Arg::with_name("in")
.long("in") .long("in")
.takes_value(true) .takes_value(true)