From ba8cc9ae06ef62cabca7ad2e7ba82439d5a1c009 Mon Sep 17 00:00:00 2001 From: Anthony Shew Date: Tue, 26 Nov 2024 22:29:49 -0700 Subject: [PATCH] WIP --- crates/turborepo-ui/src/tui/preferences.rs | 72 ++++++++++++++++++++-- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/crates/turborepo-ui/src/tui/preferences.rs b/crates/turborepo-ui/src/tui/preferences.rs index a75faeb2c0727..c34cac1e195f2 100644 --- a/crates/turborepo-ui/src/tui/preferences.rs +++ b/crates/turborepo-ui/src/tui/preferences.rs @@ -1,10 +1,72 @@ -use turbopath::AbsoluteSystemPath; +use std::{ + fs::{self, File}, + io::{BufReader, Write}, +}; -pub struct Preferences {} +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use turbopath::{AbsoluteSystemPath, 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(()) +} + +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)?; + + Ok(person) +} impl Preferences { - pub fn write_preferences(repo_root: &AbsoluteSystemPath) { - let preferences_file = repo_root.join_components(&[".turbo", "preferences", "tui.json"]); - println!("TODO: save preferences to {:?}", preferences_file); + pub fn write_preferences( + repo_root: &AbsoluteSystemPath, + ) -> 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(); + + Ok(()) } }