feat: option to save logs

This commit is contained in:
Jeevitha Kannan K S 2024-10-17 13:10:33 +05:30
parent 79eb752552
commit ae37957664
No known key found for this signature in database
GPG Key ID: 5904C34A2F7CE333
2 changed files with 34 additions and 2 deletions

View File

@ -22,6 +22,7 @@ oneshot = "0.1.8"
portable-pty = "0.8.1" portable-pty = "0.8.1"
ratatui = "0.28.1" ratatui = "0.28.1"
tui-term = "0.1.12" tui-term = "0.1.12"
chrono = "0.4.33"
temp-dir = "0.1.14" temp-dir = "0.1.14"
unicode-width = "0.2.0" unicode-width = "0.2.0"
rand = { version = "0.8.5", optional = true } rand = { version = "0.8.5", optional = true }

View File

@ -37,6 +37,7 @@ pub struct RunningCommand {
writer: Box<dyn Write + Send>, writer: Box<dyn Write + Send>,
/// Only set after the process has ended /// Only set after the process has ended
status: Option<ExitStatus>, status: Option<ExitStatus>,
log_saved_path: Option<String>,
scroll_offset: usize, scroll_offset: usize,
} }
@ -78,9 +79,19 @@ impl FloatContent for RunningCommand {
.style(Style::default()), .style(Style::default()),
); );
Block::default() let mut block = Block::default()
.borders(Borders::ALL) .borders(Borders::ALL)
.title_top(title_line.centered()) .title_top(title_line.centered());
if let Some(log_path) = &self.log_saved_path {
block =
block.title_bottom(Line::from(format!(" Log saved: {} ", log_path)).centered());
} else {
block =
block.title_bottom(Line::from(" Press 'l' to save command log ").centered());
}
block
}; };
// Process the buffer and create the pseudo-terminal widget // Process the buffer and create the pseudo-terminal widget
@ -109,6 +120,11 @@ impl FloatContent for RunningCommand {
KeyCode::PageDown => { KeyCode::PageDown => {
self.scroll_offset = self.scroll_offset.saturating_sub(10); self.scroll_offset = self.scroll_offset.saturating_sub(10);
} }
KeyCode::Char('l') => {
if let Ok(log_path) = self.save_log() {
self.log_saved_path = Some(log_path);
}
}
// Pass other key events to the terminal // Pass other key events to the terminal
_ => self.handle_passthrough_key_event(key), _ => self.handle_passthrough_key_event(key),
} }
@ -235,6 +251,7 @@ impl RunningCommand {
pty_master: pair.master, pty_master: pair.master,
writer, writer,
status: None, status: None,
log_saved_path: None,
scroll_offset: 0, scroll_offset: 0,
} }
} }
@ -282,6 +299,20 @@ impl RunningCommand {
} }
} }
fn save_log(&self) -> std::io::Result<String> {
let mut log_path = std::env::temp_dir();
log_path.push(format!(
"linutil_log_{}.log",
chrono::Local::now().format("%Y%m%d_%H%M%S")
));
let mut file = std::fs::File::create(&log_path)?;
let buffer = self.buffer.lock().unwrap();
file.write_all(&buffer)?;
Ok(log_path.to_string_lossy().into_owned())
}
/// Convert the KeyEvent to pty key codes, and send them to the virtual terminal /// Convert the KeyEvent to pty key codes, and send them to the virtual terminal
fn handle_passthrough_key_event(&mut self, key: &KeyEvent) { fn handle_passthrough_key_event(&mut self, key: &KeyEvent) {
let input_bytes = match key.code { let input_bytes = match key.code {