Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tui): persistent preferences #9512

Merged
merged 27 commits into from
Dec 18, 2024
Merged

feat(tui): persistent preferences #9512

merged 27 commits into from
Dec 18, 2024

Conversation

anthonyshew
Copy link
Contributor

@anthonyshew anthonyshew commented Nov 25, 2024

Description

Users have expressed that they find themselves repeatedly having to select out the task that they want to use. This ends up being annoying when invoking turbo, shutting down, invoking turbo, shutting down...You end up having to reselect the task you care about every re-invoke.

To improve on this, we're going to hold some state in .turbo/preferences/tui.json so that users pick up where they left off from their previous TUI invocation. State we'll keep around is:

  • The selected task name (Responsible for the "pinned-ness" portion of state)
  • Toggled state of task list visibility

Testing Instructions

Wrote tests but you should also try it out by hand. You should be able to run turbo invocations, pick out tasks, and see the state persist across invocations. You also should be able to delete the .turbo/preferences/tui.json file and it'll feel like the first invocation (no preferences).

Small note

One thing that I noticed while hand-testing is that this implementation tolerates extra fields fine, but doesn't tolerate the wrong types for a field. It will crash for in the latter scenario.

Personally, I think this is fine. You'd have to purposefully go and edit the file to get the wrong types into there.

Copy link

vercel bot commented Nov 25, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
examples-basic-web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 17, 2024 5:33am
examples-designsystem-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 17, 2024 5:33am
examples-gatsby-web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 17, 2024 5:33am
examples-kitchensink-blog ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 17, 2024 5:33am
examples-native-web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 17, 2024 5:33am
examples-nonmonorepo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 17, 2024 5:33am
examples-svelte-web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 17, 2024 5:33am
examples-tailwind-web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 17, 2024 5:33am
examples-vite-web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 17, 2024 5:33am

@anthonyshew anthonyshew changed the title [WIP] feat(tui): Persistent preferences. [WIP] feat(tui): persistent preferences Nov 25, 2024
@@ -730,6 +761,7 @@ fn cleanup<B: Backend + io::Write>(
)?;
let tasks_started = app.tasks_by_status.tasks_started();
app.persist_tasks(tasks_started)?;
app.preferences.flush_to_disk().ok();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chris-olszewski, this was smart. Kudos.

@@ -36,4 +37,6 @@ pub enum Error {
Stdin { name: String, e: std::io::Error },
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("Unable to persist preferences")]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to do:

Suggested change
#[error("Unable to persist preferences")]
#[error("Unable to persist preferences. Please file a bug report.")]

Copy link
Member

@chris-olszewski chris-olszewski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LFG

@@ -262,8 +262,9 @@ impl Run {

let (sender, receiver) = TuiSender::new();
let color_config = self.color_config;
let repo_root = self.repo_root.clone();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just borrow here instead of cloning

Suggested change
let repo_root = self.repo_root.clone();
let repo_root = &self.repo_root;

@@ -126,6 +131,18 @@ impl<W> App<W> {
})
}

fn update_sidebar_toggle(&mut self) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like toggle implies an update, up to you if this is better name 🤷

Suggested change
fn update_sidebar_toggle(&mut self) {
fn toggle_sidebar(&mut self) {

Comment on lines +44 to +47
pub fn set_active_task(&mut self, value: Option<String>) -> Result<(), Error> {
self.config.active_task = value;
Ok(())
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to lie to the caller that this could fail. We can propagate this change up to callers that now no longer need to handle the error case.

Suggested change
pub fn set_active_task(&mut self, value: Option<String>) -> Result<(), Error> {
self.config.active_task = value;
Ok(())
}
pub fn set_active_task(&mut self, value: Option<String>) {
self.config.active_task = value;
}

Comment on lines +121 to +137
loader
.file_path
.ensure_dir()
.expect("Failed to create directory");

let preferences = Preferences {
active_task: Some("web#dev".to_owned()),
is_task_list_visible: Some(false),
};

loader
.file_path
.create_with_contents(
serde_json::to_string_pretty(&preferences)
.expect("Failed to serialize preferences"),
)
.expect("Failed to create file");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be fine to use the methods instead of creating the file manually

Suggested change
loader
.file_path
.ensure_dir()
.expect("Failed to create directory");
let preferences = Preferences {
active_task: Some("web#dev".to_owned()),
is_task_list_visible: Some(false),
};
loader
.file_path
.create_with_contents(
serde_json::to_string_pretty(&preferences)
.expect("Failed to serialize preferences"),
)
.expect("Failed to create file");
loader.set_is_task_list_visible(Some(false));
loader.set_active_task(Some("web#dev".to_owned()));
loader.flush_to_disk().expect("failed to create file);

Comment on lines +151 to +167
loader
.file_path
.ensure_dir()
.expect("Failed to create directory");

let preferences = Preferences {
active_task: Some("web#dev".to_owned()),
is_task_list_visible: Some(false),
};

loader
.file_path
.create_with_contents(
serde_json::to_string_pretty(&preferences)
.expect("Failed to serialize preferences"),
)
.expect("Failed to create file");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same feedback as above

self.preferences.set_active_task(
self.is_task_selection_pinned
.then(|| active_task.to_owned()),
)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't doing FS anymore so it shouldn't have a failure

Suggested change
)?;
);

Comment on lines +139 to +144
fn update_task_selection_pinned_state(&mut self) -> Result<(), Error> {
// Preferences assume a pinned state when there is an active task.
// This `None` creates "un-pinned-ness" on the next TUI startup.
self.preferences.set_active_task(None)?;
Ok(())
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this name a little confusing. Does clear_pinned_task express the behavior better?

Suggested change
fn update_task_selection_pinned_state(&mut self) -> Result<(), Error> {
// Preferences assume a pinned state when there is an active task.
// This `None` creates "un-pinned-ness" on the next TUI startup.
self.preferences.set_active_task(None)?;
Ok(())
}
fn update_task_selection_pinned_state(&mut self) {
// Preferences assume a pinned state when there is an active task.
// This `None` creates "un-pinned-ness" on the next TUI startup.
self.preferences.set_active_task(None);
}

@anthonyshew anthonyshew merged commit 46398a9 into main Dec 18, 2024
37 checks passed
@anthonyshew anthonyshew deleted the shew-244ce branch December 18, 2024 02:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants