Skip to content

Commit

Permalink
Limit process and thread width
Browse files Browse the repository at this point in the history
  • Loading branch information
flxoacn committed Jun 6, 2024
1 parent 7706b25 commit 071eedb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
22 changes: 3 additions & 19 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ use nom::{
};

use serde_json::from_str;
use std::{
io::{Cursor, Read},
iter::once,
};
use std::io::{Cursor, Read};

use time::Tm;

Expand Down Expand Up @@ -271,18 +268,6 @@ impl FormatParser for JsonParser {
// [seconds][pid][tid][tags] LEVEL: message
// [01086.023158][boot-drivers:dev][driver,platform_bus] INFO: [platform-bus.cc(292)] Boot Item ZBI_TYPE_SERIAL_NUMBER not found
fn parse_fuchsia(line: &str) -> IResult<&str, Record> {
fn trim_str<const N: usize>(s: &str) -> String {
if s.len() > N {
s.chars()
.take(N - 1)
.chain(once('…'))
.take(N)
.collect::<String>()
} else {
s.to_string()
}
}

// Timestamp
let (rest, _) = char('[')(line)?;
let (rest, timestamp) = take_until1("]")(rest)?;
Expand All @@ -292,16 +277,15 @@ fn parse_fuchsia(line: &str) -> IResult<&str, Record> {
// Process
let (rest, _) = char('[')(rest)?;
let (rest, process) = take_until1("]")(rest)?;
let process = trim_str::<16>(process);
let process = process.to_string();
let (rest, _) = char(']')(rest)?;

// Tid
let (rest, thread) = if rest.starts_with('[') {
let (rest, _) = char('[')(rest)?;
let (rest, thread) = take_until1("]")(rest)?;
let (rest, _) = char(']')(rest)?;
let thread = trim_str::<16>(thread);
(rest, thread)
(rest, thread.to_string())
} else {
(rest, String::new())
};
Expand Down
14 changes: 12 additions & 2 deletions src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ struct Human {
date_format: Option<(&'static str, usize)>,
highlight: Vec<Regex>,
process_width: usize,
max_process_width: usize,
tag_width: Option<usize>,
thread_width: usize,
max_thread_width: usize,
dimm_color: Option<Color>,
bright_colors: bool,
}
Expand Down Expand Up @@ -125,7 +127,9 @@ impl Human {
date_format,
tag_width,
process_width: 0,
max_process_width: 16,
thread_width: 0,
max_thread_width: 16,
bright_colors,
}
}
Expand Down Expand Up @@ -196,13 +200,19 @@ impl Human {
width = tag_width
);

self.process_width = max(self.process_width, record.process.chars().count());
self.process_width = min(
max(self.process_width, record.process.chars().count()),
self.max_process_width,
);
let pid = if record.process.is_empty() {
" ".repeat(self.process_width)
} else {
format!("{:<width$}", record.process, width = self.process_width)
};
self.thread_width = max(self.thread_width, record.thread.chars().count());
self.thread_width = min(
max(self.thread_width, record.thread.chars().count()),
self.max_thread_width,
);
let tid = if !record.thread.is_empty() {
format!(" {:>width$}", record.thread, width = self.thread_width)
} else if self.thread_width != 0 {
Expand Down

0 comments on commit 071eedb

Please sign in to comment.