From a6c1ae14aecd7347069776ba700a369c8fdccb6f Mon Sep 17 00:00:00 2001 From: Anthony Shew Date: Wed, 27 Nov 2024 10:43:01 -0700 Subject: [PATCH] WIP --- crates/turborepo-ui/src/tui/app.rs | 23 +++++- crates/turborepo-ui/src/tui/preferences.rs | 89 ++++++++++++---------- 2 files changed, 71 insertions(+), 41 deletions(-) diff --git a/crates/turborepo-ui/src/tui/app.rs b/crates/turborepo-ui/src/tui/app.rs index d149d6bd75056..1d29e626b762a 100644 --- a/crates/turborepo-ui/src/tui/app.rs +++ b/crates/turborepo-ui/src/tui/app.rs @@ -26,6 +26,7 @@ const RESIZE_DEBOUNCE_DELAY: Duration = Duration::from_millis(10); use super::{ event::{CacheResult, Direction, OutputLogs, PaneSize, TaskResult}, input, + preferences::Preferences, search::SearchResults, AppReceiver, Debouncer, Error, Event, InputOptions, SizeInfo, TaskTable, TerminalPane, }; @@ -773,10 +774,23 @@ fn update( app.finish_task(&task, result)?; } Event::Up => { - preferences::Preferences::write_preferences(repo_root); + preferences::Preferences::write_preferences( + &Preferences { + is_task_list_visible: app.has_sidebar, + active_task: app.active_task()?.to_string(), + }, + repo_root, + ); app.previous(); } Event::Down => { + preferences::Preferences::write_preferences( + &Preferences { + is_task_list_visible: app.has_sidebar, + active_task: app.active_task()?.to_string(), + }, + repo_root, + ); app.next(); } Event::ScrollUp => { @@ -796,6 +810,13 @@ fn update( app.interact()?; } Event::ToggleSidebar => { + preferences::Preferences::write_preferences( + &Preferences { + is_task_list_visible: app.has_sidebar, + active_task: app.active_task()?.to_string(), + }, + repo_root, + ); app.has_sidebar = !app.has_sidebar; } Event::Input { bytes } => { diff --git a/crates/turborepo-ui/src/tui/preferences.rs b/crates/turborepo-ui/src/tui/preferences.rs index c34cac1e195f2..addec030e49ab 100644 --- a/crates/turborepo-ui/src/tui/preferences.rs +++ b/crates/turborepo-ui/src/tui/preferences.rs @@ -5,68 +5,77 @@ use std::{ use serde::{Deserialize, Serialize}; use serde_json::Value; -use turbopath::{AbsoluteSystemPath, AbsoluteSystemPathBuf}; +use turbopath::AbsoluteSystemPathBuf; #[derive(Serialize, Deserialize, Debug)] pub struct Preferences { pub is_task_list_visible: bool, -} - -fn save_to_json( - preferences: &Preferences, - path: AbsoluteSystemPathBuf, -) -> Result<(), Box> { - let json = serde_json::to_string_pretty(preferences)?; - let mut file = File::create(path.as_std_path())?; - file.write_all(json.as_bytes())?; - - Ok(()) -} - -fn update_json_field( - file_path: &str, - field: &str, - new_value: Value, -) -> Result<(), Box> { - let json_string = fs::read_to_string(file_path)?; - let mut json: Value = serde_json::from_str(&json_string)?; - - json[field] = new_value; - let updated_json_string = serde_json::to_string_pretty(&json)?; - - let mut file = fs::File::create(file_path)?; - file.write_all(updated_json_string.as_bytes())?; - - Ok(()) + pub active_task: String, } fn read_from_json(path: &str) -> Result> { let file = File::open(path)?; let reader = BufReader::new(file); - let person: Preferences = serde_json::from_reader(reader)?; + let preferences: Preferences = serde_json::from_reader(reader)?; - Ok(person) + Ok(preferences) +} + +impl Default for Preferences { + fn default() -> Self { + Preferences { + is_task_list_visible: true, + active_task: String::new(), + } + } } impl Preferences { + // pub fn new(is_task_list_visible: bool, active_task: String) -> Self { + // Preferences { + // is_task_list_visible, + // active_task, + // } + // } + pub fn write_preferences( - repo_root: &AbsoluteSystemPath, + &self, + repo_root: &AbsoluteSystemPathBuf, ) -> Result<(), Box> { let preferences_dir = repo_root.join_components(&[".turbo", "preferences"]); let preferences_file = preferences_dir.join_component("tui.json"); - // Create the directory structure if it doesn't exist fs::create_dir_all(preferences_dir.as_std_path())?; - save_to_json( - &Preferences { - is_task_list_visible: true, - }, - preferences_file, - ) - .unwrap(); + let json = serde_json::to_string_pretty(self)?; + let mut file = File::create(preferences_file.as_std_path())?; + file.write_all(json.as_bytes())?; Ok(()) } + + // pub fn read_preferences( + // repo_root: &AbsoluteSystemPathBuf, + // ) -> Result> { + // let preferences_file = repo_root.join_components(&[".turbo", + // "preferences", "tui.json"]); read_from_json(preferences_file. + // as_std_path().to_str().unwrap()) } +} + +fn update_json_field( + file_path: &str, + field: &str, + new_value: Value, +) -> Result<(), Box> { + let json_string = fs::read_to_string(file_path)?; + let mut json: Value = serde_json::from_str(&json_string)?; + + json[field] = new_value; + let updated_json_string = serde_json::to_string_pretty(&json)?; + + let mut file = fs::File::create(file_path)?; + file.write_all(updated_json_string.as_bytes())?; + + Ok(()) }