Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyshew committed Nov 27, 2024
1 parent 6d1a886 commit ba8cc9a
Showing 1 changed file with 67 additions and 5 deletions.
72 changes: 67 additions & 5 deletions crates/turborepo-ui/src/tui/preferences.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::error::Error>> {
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<dyn std::error::Error>> {
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<Preferences, Box<dyn std::error::Error>> {
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<dyn std::error::Error>> {
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(())
}
}

0 comments on commit ba8cc9a

Please sign in to comment.