From ae37957664c5ffcb579d9072ed9f00a74b05405d Mon Sep 17 00:00:00 2001 From: Jeevitha Kannan K S Date: Thu, 17 Oct 2024 13:10:33 +0530 Subject: [PATCH 1/5] feat: option to save logs --- tui/Cargo.toml | 1 + tui/src/running_command.rs | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/tui/Cargo.toml b/tui/Cargo.toml index 40d3c357..b577f349 100644 --- a/tui/Cargo.toml +++ b/tui/Cargo.toml @@ -22,6 +22,7 @@ oneshot = "0.1.8" portable-pty = "0.8.1" ratatui = "0.28.1" tui-term = "0.1.12" +chrono = "0.4.33" temp-dir = "0.1.14" unicode-width = "0.2.0" rand = { version = "0.8.5", optional = true } diff --git a/tui/src/running_command.rs b/tui/src/running_command.rs index 89daa755..785f8835 100644 --- a/tui/src/running_command.rs +++ b/tui/src/running_command.rs @@ -37,6 +37,7 @@ pub struct RunningCommand { writer: Box, /// Only set after the process has ended status: Option, + log_saved_path: Option, scroll_offset: usize, } @@ -78,9 +79,19 @@ impl FloatContent for RunningCommand { .style(Style::default()), ); - Block::default() + let mut block = Block::default() .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 @@ -109,6 +120,11 @@ impl FloatContent for RunningCommand { KeyCode::PageDown => { 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 _ => self.handle_passthrough_key_event(key), } @@ -235,6 +251,7 @@ impl RunningCommand { pty_master: pair.master, writer, status: None, + log_saved_path: None, scroll_offset: 0, } } @@ -282,6 +299,20 @@ impl RunningCommand { } } + fn save_log(&self) -> std::io::Result { + 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 fn handle_passthrough_key_event(&mut self, key: &KeyEvent) { let input_bytes = match key.code { From a429ab2667c507f6d3831351db4f5f59c99a9a5a Mon Sep 17 00:00:00 2001 From: JEEVITHA KANNAN K S Date: Fri, 1 Nov 2024 17:49:21 +0530 Subject: [PATCH 2/5] refact: use time crate --- Cargo.lock | 72 +++++++++++++++++++++++++++++++++++--- tui/Cargo.toml | 1 + tui/src/running_command.rs | 8 +++-- 3 files changed, 75 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fc49af4c..a1c9b3e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -242,6 +242,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "deranged" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" +dependencies = [ + "powerfmt", +] + [[package]] name = "downcast-rs" version = "1.2.1" @@ -412,9 +421,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.158" +version = "0.2.161" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" +checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" [[package]] name = "linutil_core" @@ -443,6 +452,7 @@ dependencies = [ "rand", "ratatui", "temp-dir", + "time", "tree-sitter-bash", "tree-sitter-highlight", "tui-term", @@ -539,6 +549,21 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + +[[package]] +name = "num_threads" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c7398b9c8b70908f6371f47ed36737907c87c52af34c268fed0bf0ceb92ead9" +dependencies = [ + "libc", +] + [[package]] name = "once_cell" version = "1.19.0" @@ -607,6 +632,12 @@ dependencies = [ "winreg", ] +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + [[package]] name = "ppv-lite86" version = "0.2.20" @@ -725,9 +756,9 @@ checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" [[package]] name = "rustix" -version = "0.38.37" +version = "0.38.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" +checksum = "aa260229e6538e52293eeb577aabd09945a09d6d9cc0fc550ed7529056c2e32a" dependencies = [ "bitflags 2.6.0", "errno", @@ -975,6 +1006,39 @@ dependencies = [ "syn", ] +[[package]] +name = "time" +version = "0.3.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" +dependencies = [ + "deranged", + "itoa", + "libc", + "num-conv", + "num_threads", + "powerfmt", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" + +[[package]] +name = "time-macros" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" +dependencies = [ + "num-conv", + "time-core", +] + [[package]] name = "toml" version = "0.8.19" diff --git a/tui/Cargo.toml b/tui/Cargo.toml index 337dc5c7..67bf4831 100644 --- a/tui/Cargo.toml +++ b/tui/Cargo.toml @@ -22,6 +22,7 @@ portable-pty = "0.8.1" ratatui = "0.29.0" tui-term = "0.2.0" temp-dir = "0.1.14" +time = { version = "0.3.36", features = ["local-offset", "macros", "formatting"] } unicode-width = "0.2.0" rand = { version = "0.8.5", optional = true } linutil_core = { path = "../core", version = "24.9.28" } diff --git a/tui/src/running_command.rs b/tui/src/running_command.rs index 06058df2..2f51f2ee 100644 --- a/tui/src/running_command.rs +++ b/tui/src/running_command.rs @@ -17,11 +17,11 @@ use std::{ sync::{Arc, Mutex}, thread::JoinHandle, }; +use time::{macros::format_description, OffsetDateTime}; use tui_term::{ vt100::{self, Screen}, widget::PseudoTerminal, }; - pub struct RunningCommand { /// A buffer to save all the command output (accumulates, until the command exits) buffer: Arc>>, @@ -301,9 +301,13 @@ impl RunningCommand { fn save_log(&self) -> std::io::Result { let mut log_path = std::env::temp_dir(); + let format = format_description!("[year]-[month]-[day]-[hour]-[minute]-[second]"); log_path.push(format!( "linutil_log_{}.log", - chrono::Local::now().format("%Y%m%d_%H%M%S") + OffsetDateTime::now_local() + .unwrap() + .format(&format) + .unwrap() )); let mut file = std::fs::File::create(&log_path)?; From 661398da44e892d710c60c024e6484163ea03cd5 Mon Sep 17 00:00:00 2001 From: JEEVITHA KANNAN K S Date: Fri, 1 Nov 2024 18:04:03 +0530 Subject: [PATCH 3/5] fix: panics --- tui/src/running_command.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tui/src/running_command.rs b/tui/src/running_command.rs index 2f51f2ee..01d6cff4 100644 --- a/tui/src/running_command.rs +++ b/tui/src/running_command.rs @@ -37,7 +37,7 @@ pub struct RunningCommand { writer: Box, /// Only set after the process has ended status: Option, - log_saved_path: Option, + log_path: Option, scroll_offset: usize, } @@ -83,7 +83,7 @@ impl FloatContent for RunningCommand { .borders(Borders::ALL) .title_top(title_line.centered()); - if let Some(log_path) = &self.log_saved_path { + if let Some(log_path) = &self.log_path { block = block.title_bottom(Line::from(format!(" Log saved: {} ", log_path)).centered()); } else { @@ -120,9 +120,9 @@ impl FloatContent for RunningCommand { KeyCode::PageDown => { self.scroll_offset = self.scroll_offset.saturating_sub(10); } - KeyCode::Char('l') => { + KeyCode::Char('l') if self.is_finished() => { if let Ok(log_path) = self.save_log() { - self.log_saved_path = Some(log_path); + self.log_path = Some(log_path); } } // Pass other key events to the terminal @@ -251,7 +251,7 @@ impl RunningCommand { pty_master: pair.master, writer, status: None, - log_saved_path: None, + log_path: None, scroll_offset: 0, } } @@ -301,12 +301,12 @@ impl RunningCommand { fn save_log(&self) -> std::io::Result { let mut log_path = std::env::temp_dir(); - let format = format_description!("[year]-[month]-[day]-[hour]-[minute]-[second]"); + let date_format = format_description!("[year]-[month]-[day]-[hour]-[minute]-[second]"); log_path.push(format!( "linutil_log_{}.log", OffsetDateTime::now_local() - .unwrap() - .format(&format) + .unwrap_or(OffsetDateTime::now_utc()) + .format(&date_format) .unwrap() )); From 49dcbd12967ea1cda812b77f8089bc3483e85458 Mon Sep 17 00:00:00 2001 From: JEEVITHA KANNAN K S Date: Fri, 1 Nov 2024 18:18:59 +0530 Subject: [PATCH 4/5] hints --- tui/src/running_command.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tui/src/running_command.rs b/tui/src/running_command.rs index 01d6cff4..158d7f6e 100644 --- a/tui/src/running_command.rs +++ b/tui/src/running_command.rs @@ -148,6 +148,7 @@ impl FloatContent for RunningCommand { Shortcut::new("Close window", ["Enter", "q"]), Shortcut::new("Scroll up", ["Page up"]), Shortcut::new("Scroll down", ["Page down"]), + Shortcut::new("Save log", ["l"]), ]), ) } else { From 89d8b062b2c0a0ce372eaeff554ab5db5dcf8e4c Mon Sep 17 00:00:00 2001 From: JEEVITHA KANNAN K S Date: Fri, 1 Nov 2024 20:29:09 +0530 Subject: [PATCH 5/5] update roadmap --- docs/roadmap.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/roadmap.md b/docs/roadmap.md index cc334348..ba7b6c62 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -14,7 +14,7 @@ ### Q4 2024 - [x] Finish the foundation of the project's CLI - [ ] Implement CLI arguments and configuration support -- [ ] Add an option for logging script executions +- [x] Add an option for logging script executions ### Q1 2025 - [ ] GUI Brainstorming and Planning