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 ba8cc9a commit a6c1ae1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 41 deletions.
23 changes: 22 additions & 1 deletion crates/turborepo-ui/src/tui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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 => {
Expand All @@ -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 } => {
Expand Down
89 changes: 49 additions & 40 deletions crates/turborepo-ui/src/tui/preferences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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(())
pub active_task: String,
}

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)?;
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<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();
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<Self, Box<dyn std::error::Error>> {
// 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<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(())
}

0 comments on commit a6c1ae1

Please sign in to comment.