Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added terminal width cli and config option for terminal sink #393

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/bin/oura/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ 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 mapper = MapperConfig {
include_block_end_events: true,
..Default::default()
Expand Down Expand Up @@ -114,6 +119,7 @@ pub fn run(args: &ArgMatches) -> Result<(), Error> {

let sink_setup = oura::sinks::terminal::Config {
throttle_min_span_millis: throttle,
terminal_width: terminal_width
};

let (source_handle, source_output) = match source_setup {
Expand Down Expand Up @@ -152,10 +158,17 @@ pub(crate) fn command_definition<'a>() -> clap::Command<'a> {
.takes_value(true)
.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"),
)
.arg(
clap::Arg::new("mode")
.long("mode")
.takes_value(true)
.possible_values(&["node", "client"]),
)

}
14 changes: 11 additions & 3 deletions src/sinks/terminal/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,17 @@ impl Display for LogLine {

match self.content.len() {
x if x > max_width => {
let partial: String = self.content.chars().take(max_width - 3).collect();
partial.with(Color::Grey).fmt(f)?;
f.write_str("...")?;
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)
}
});
wrapped.with(Color::Grey).fmt(f)?;
}
_ => {
let full = &self.content[..];
Expand Down
7 changes: 6 additions & 1 deletion src/sinks/terminal/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use super::format::*;

pub fn reducer_loop(
throttle_min_span: Duration,
terminal_width: Option<u16>,
input: StageReceiver,
utils: Arc<Utils>,
) -> Result<(), Error> {
Expand All @@ -27,7 +28,11 @@ pub fn reducer_loop(
))?;

for evt in input.iter() {
let (width, _) = crossterm::terminal::size()?;

let width = match terminal_width {
Some(w) => w,
None => crossterm::terminal::size()?.0
};

throttle.wait_turn();
let line = LogLine::new(&evt, width as usize, &utils);
Expand Down
5 changes: 4 additions & 1 deletion src/sinks/terminal/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const THROTTLE_MIN_SPAN_MILLIS: u64 = 300;
#[derive(Default, Debug, Deserialize)]
pub struct Config {
pub throttle_min_span_millis: Option<u64>,
pub terminal_width: Option<u16>
}

impl SinkProvider for WithUtils<Config> {
Expand All @@ -24,10 +25,12 @@ impl SinkProvider for WithUtils<Config> {
.unwrap_or(THROTTLE_MIN_SPAN_MILLIS),
);

let terminal_width = self.inner.terminal_width;

let utils = self.utils.clone();

let handle = std::thread::spawn(move || {
reducer_loop(throttle_min_span, input, utils).expect("terminal sink loop failed");
reducer_loop(throttle_min_span, terminal_width, input, utils).expect("terminal sink loop failed");
});

Ok(handle)
Expand Down