diff --git a/book/src/sinks/terminal.md b/book/src/sinks/terminal.md index 47c5099e..b62b7fa0 100644 --- a/book/src/sinks/terminal.md +++ b/book/src/sinks/terminal.md @@ -8,7 +8,9 @@ A sink that outputs each event into the terminal through stdout using fancy colo [sink] type = "Terminal" throttle_min_span_millis = 500 +wrap = true ``` - `type` (required): the literal value `Terminal`. - `throttle_min_span_millis` (optional, default = `500`): the amount of time (milliseconds) to wait between printing each event into the console. This is used to facilitate the reading for human following the output. +- `wrap` (optional, default = `false`): a true value indicates that long output text should break and continue in the following line. If false, lines will be truncated to fit in the available terminal width. diff --git a/book/src/usage/watch.md b/book/src/usage/watch.md index 65e05dec..b7eb1b24 100644 --- a/book/src/usage/watch.md +++ b/book/src/usage/watch.md @@ -20,7 +20,7 @@ oura watch [OPTIONS] - `--magic `: the magic number of the network you're connecting to. Possible values are `mainnet`, `testnet`, `preview`, `preprod` or a numeric value. If omitted, the value `mainnet` is used as default. - `--mode `: an option to force the which set of mini-protocols to use when connecting to the Cardano node. Possible values: `node` and `client`. If omitted, _Oura_ will infer the standard protocols for the specified bearer. - `--since ,`: an option to specify from which point in the chain _Oura_ should start reading from. The point is referenced by passing the slot of the block followed by a comma and the hash of the block (`,`). If omitted, _Oura_ will start reading from the tail (tip) of the node. - +- `--wrap`: indicates that long output text should break and continue in the following line. If omitted, lines will be truncated to fit in the available terminal width. ## Examples diff --git a/src/bin/oura/watch.rs b/src/bin/oura/watch.rs index 9b4b56be..31cf1a7a 100644 --- a/src/bin/oura/watch.rs +++ b/src/bin/oura/watch.rs @@ -77,10 +77,7 @@ pub fn run(args: &ArgMatches) -> Result<(), Error> { false => None, }; - let terminal_width = match args.is_present("terminal-width") { - true => Some(args.value_of_t("terminal-width")?), - false => None - }; + let wrap = args.is_present("wrap"); let mapper = MapperConfig { include_block_end_events: true, @@ -119,7 +116,7 @@ pub fn run(args: &ArgMatches) -> Result<(), Error> { let sink_setup = oura::sinks::terminal::Config { throttle_min_span_millis: throttle, - terminal_width: terminal_width + wrap: Some(wrap), }; let (source_handle, source_output) = match source_setup { @@ -159,10 +156,11 @@ pub(crate) fn command_definition<'a>() -> clap::Command<'a> { .help("milliseconds to wait between output lines (for easier reading)"), ) .arg( - clap::Arg::new("terminal-width") - .long("terminal-width") - .takes_value(true) - .help("width of the terminal"), + clap::Arg::new("wrap") + .long("wrap") + .short('w') + .takes_value(false) + .help("long text output should break and continue in the following line"), ) .arg( clap::Arg::new("mode") @@ -170,5 +168,4 @@ pub(crate) fn command_definition<'a>() -> clap::Command<'a> { .takes_value(true) .possible_values(&["node", "client"]), ) - } diff --git a/src/sinks/terminal/format.rs b/src/sinks/terminal/format.rs index ca3f70f9..502d3d17 100644 --- a/src/sinks/terminal/format.rs +++ b/src/sinks/terminal/format.rs @@ -18,7 +18,7 @@ pub struct LogLine { tx_idx: Option, block_num: Option, content: String, - max_width: usize, + max_width: Option, } impl LogLine { @@ -26,7 +26,7 @@ impl LogLine { source: &Event, prefix: &'static str, color: Color, - max_width: usize, + max_width: Option, content: String, ) -> Self { LogLine { @@ -41,7 +41,7 @@ impl LogLine { } impl LogLine { - pub fn new(source: &Event, max_width: usize, utils: &Utils) -> LogLine { + pub fn new(source: &Event, max_width: Option, utils: &Utils) -> LogLine { match &source.data { EventData::Block(BlockRecord { era, @@ -361,8 +361,6 @@ impl LogLine { impl Display for LogLine { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let flex_width = self.max_width - 40; - format!( "BLOCK:{:0>7} █ TX:{:0>2}", self.block_num @@ -387,20 +385,12 @@ impl Display for LogLine { f.write_char(' ')?; { - let max_width = std::cmp::min(self.content.len(), flex_width); + let available_width = self.max_width.map(|x| x - 35); - match self.content.len() { - x if x > max_width => { - let wrapped: String = self.content - .chars() - .enumerate() - .fold(String::new(), |acc, (i, c)| { - if i != 0 && i % max_width == 0 { - format!("{}\n{}", acc, c) - } else { - format!("{}{}", acc, c) - } - }); + match available_width { + Some(width) if width < self.content.len() => { + let wrapped = &self.content[..width]; + let wrapped = format!("{wrapped}..."); wrapped.with(Color::Grey).fmt(f)?; } _ => { diff --git a/src/sinks/terminal/run.rs b/src/sinks/terminal/run.rs index c537841e..a772f31c 100644 --- a/src/sinks/terminal/run.rs +++ b/src/sinks/terminal/run.rs @@ -15,7 +15,7 @@ use super::format::*; pub fn reducer_loop( throttle_min_span: Duration, - terminal_width: Option, + wrap: bool, input: StageReceiver, utils: Arc, ) -> Result<(), Error> { @@ -28,14 +28,13 @@ pub fn reducer_loop( ))?; for evt in input.iter() { - - let width = match terminal_width { - Some(w) => w, - None => crossterm::terminal::size()?.0 + let width = match wrap { + true => None, + false => Some(crossterm::terminal::size()?.0 as usize), }; throttle.wait_turn(); - let line = LogLine::new(&evt, width as usize, &utils); + let line = LogLine::new(&evt, width, &utils); let result = stdout.execute(Print(line)); diff --git a/src/sinks/terminal/setup.rs b/src/sinks/terminal/setup.rs index c51671ac..b5725923 100644 --- a/src/sinks/terminal/setup.rs +++ b/src/sinks/terminal/setup.rs @@ -14,7 +14,7 @@ const THROTTLE_MIN_SPAN_MILLIS: u64 = 300; #[derive(Default, Debug, Deserialize)] pub struct Config { pub throttle_min_span_millis: Option, - pub terminal_width: Option + pub wrap: Option, } impl SinkProvider for WithUtils { @@ -25,12 +25,11 @@ impl SinkProvider for WithUtils { .unwrap_or(THROTTLE_MIN_SPAN_MILLIS), ); - let terminal_width = self.inner.terminal_width; - + let wrap = self.inner.wrap.unwrap_or(false); let utils = self.utils.clone(); let handle = std::thread::spawn(move || { - reducer_loop(throttle_min_span, terminal_width, input, utils).expect("terminal sink loop failed"); + reducer_loop(throttle_min_span, wrap, input, utils).expect("terminal sink loop failed"); }); Ok(handle)