Skip to content

feat(tui): persistent preferences - #9512

Merged
anthonyshew merged 27 commits into
mainfrom
shew-244ce
Dec 18, 2024
Merged

feat(tui): persistent preferences#9512
anthonyshew merged 27 commits into
mainfrom
shew-244ce

Conversation

@anthonyshew

@anthonyshew anthonyshew commented Nov 25, 2024

Copy link
Copy Markdown
Contributor

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.

@vercel

vercel Bot commented Nov 25, 2024

Copy link
Copy Markdown
Contributor

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
Comment thread CONTRIBUTING.md
- [Repositories to test with](#repositories-to-test-with)
- [Debugging tips](#debugging-tips)
- [Verbose logging](#verbose-logging)
- [Verbose logging](#verbose-logging)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure.

Comment thread crates/turborepo-ui/src/tui/app.rs Outdated
)?;
let tasks_started = app.tasks_by_status.tasks_started();
app.persist_tasks(tasks_started)?;
app.preferences.flush_to_disk().ok();

Copy link
Copy Markdown
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.

Comment thread crates/turborepo-ui/src/tui/mod.rs Outdated
Stdin { name: String, e: std::io::Error },
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("Unable to persist preferences")]

Copy link
Copy Markdown
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.")]

@chris-olszewski chris-olszewski left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LFG


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

Copy link
Copy Markdown
Contributor

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;

})
}

fn update_sidebar_toggle(&mut self) {

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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);
}

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