mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 01:26:04 -08:00
Wrap log records using textwrap crate
This commit is contained in:
10
Cargo.lock
generated
10
Cargo.lock
generated
@@ -509,12 +509,22 @@ dependencies = [
|
|||||||
"rpassword",
|
"rpassword",
|
||||||
"ssh2",
|
"ssh2",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
|
"textwrap",
|
||||||
"tui",
|
"tui",
|
||||||
"unicode-width",
|
"unicode-width",
|
||||||
"users",
|
"users",
|
||||||
"whoami",
|
"whoami",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "textwrap"
|
||||||
|
version = "0.12.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "203008d98caf094106cfaba70acfed15e18ed3ddb7d94e49baec153a2b462789"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-width",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "time"
|
name = "time"
|
||||||
version = "0.1.44"
|
version = "0.1.44"
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ rpassword = "5.0.0"
|
|||||||
unicode-width = "0.1.7"
|
unicode-width = "0.1.7"
|
||||||
chrono = "0.4.19"
|
chrono = "0.4.19"
|
||||||
bytesize = "1.0.1"
|
bytesize = "1.0.1"
|
||||||
|
textwrap = "0.12.1"
|
||||||
|
|
||||||
[target.'cfg(any(unix, macos, linux))'.dependencies]
|
[target.'cfg(any(unix, macos, linux))'.dependencies]
|
||||||
users = "0.11.0"
|
users = "0.11.0"
|
||||||
|
|||||||
@@ -26,6 +26,7 @@
|
|||||||
// Dependencies
|
// Dependencies
|
||||||
extern crate chrono;
|
extern crate chrono;
|
||||||
extern crate crossterm;
|
extern crate crossterm;
|
||||||
|
extern crate textwrap;
|
||||||
extern crate tui;
|
extern crate tui;
|
||||||
extern crate unicode_width;
|
extern crate unicode_width;
|
||||||
|
|
||||||
@@ -1863,7 +1864,11 @@ impl FileTransferActivity {
|
|||||||
let mut log_state: ListState = ListState::default();
|
let mut log_state: ListState = ListState::default();
|
||||||
log_state.select(Some(self.log_index));
|
log_state.select(Some(self.log_index));
|
||||||
// Draw log
|
// Draw log
|
||||||
f.render_stateful_widget(self.draw_log_list(), chunks[2], &mut log_state);
|
f.render_stateful_widget(
|
||||||
|
self.draw_log_list(chunks[2].width),
|
||||||
|
chunks[2],
|
||||||
|
&mut log_state,
|
||||||
|
);
|
||||||
// Draw popup
|
// Draw popup
|
||||||
if let InputMode::Popup(popup) = &self.input_mode {
|
if let InputMode::Popup(popup) = &self.input_mode {
|
||||||
// Calculate popup size
|
// Calculate popup size
|
||||||
@@ -1983,34 +1988,47 @@ impl FileTransferActivity {
|
|||||||
/// ### draw_log_list
|
/// ### draw_log_list
|
||||||
///
|
///
|
||||||
/// Draw log list
|
/// Draw log list
|
||||||
fn draw_log_list(&self) -> List {
|
/// Chunk width must be provided to wrap text
|
||||||
|
fn draw_log_list(&self, width: u16) -> List {
|
||||||
let events: Vec<ListItem> = self
|
let events: Vec<ListItem> = self
|
||||||
.log_records
|
.log_records
|
||||||
.iter()
|
.iter()
|
||||||
.map(|record: &LogRecord| {
|
.map(|record: &LogRecord| {
|
||||||
|
let record_rows = textwrap::wrap(record.msg.as_str(), (width as usize) - 35); // -35 'cause log prefix
|
||||||
let s = match record.level {
|
let s = match record.level {
|
||||||
LogLevel::Error => Style::default().fg(Color::Red),
|
LogLevel::Error => Style::default().fg(Color::Red),
|
||||||
LogLevel::Warn => Style::default().fg(Color::Yellow),
|
LogLevel::Warn => Style::default().fg(Color::Yellow),
|
||||||
LogLevel::Info => Style::default().fg(Color::Green),
|
LogLevel::Info => Style::default().fg(Color::Green),
|
||||||
};
|
};
|
||||||
let log = Spans::from(vec![
|
let mut rows: Vec<Spans> = Vec::with_capacity(record_rows.len());
|
||||||
Span::from(format!("{}", record.time.format("%Y-%m-%dT%H:%M:%S%Z"))),
|
// Iterate over remaining rows
|
||||||
Span::raw(" ["),
|
for (idx, row) in record_rows.iter().enumerate() {
|
||||||
Span::styled(
|
let row: Spans = match idx {
|
||||||
format!(
|
0 => Spans::from(vec![
|
||||||
"{}",
|
Span::from(format!("{}", record.time.format("%Y-%m-%dT%H:%M:%S%Z"))),
|
||||||
match record.level {
|
Span::raw(" ["),
|
||||||
LogLevel::Error => "ERROR",
|
Span::styled(
|
||||||
LogLevel::Warn => "WARN",
|
format!(
|
||||||
LogLevel::Info => "INFO",
|
"{:5}",
|
||||||
}
|
match record.level {
|
||||||
),
|
LogLevel::Error => "ERROR",
|
||||||
s,
|
LogLevel::Warn => "WARN",
|
||||||
),
|
LogLevel::Info => "INFO",
|
||||||
Span::raw("]: "),
|
}
|
||||||
Span::from(record.msg.clone()),
|
),
|
||||||
]);
|
s,
|
||||||
ListItem::new(log)
|
),
|
||||||
|
Span::raw("]: "),
|
||||||
|
Span::from(String::from(row.as_ref())),
|
||||||
|
]),
|
||||||
|
_ => Spans::from(vec![Span::from(textwrap::indent(
|
||||||
|
row.as_ref(),
|
||||||
|
" ",
|
||||||
|
))]),
|
||||||
|
};
|
||||||
|
rows.push(row);
|
||||||
|
}
|
||||||
|
ListItem::new(rows)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
List::new(events)
|
List::new(events)
|
||||||
|
|||||||
Reference in New Issue
Block a user