-
Notifications
You must be signed in to change notification settings - Fork 0
🎨 Palette: Hide terminal cursor during CLI spinner animation #8
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ## $(date +%Y-%m-%d) - [CLI Cursor UX Fix] | ||
| **Learning:** This app is a Rust CLI, not a web frontend. Standard web accessibility rules (ARIA labels, DOM structure) do not apply. UX improvements here involve terminal manipulation (using `crossterm` for ANSI output, cursor hiding, colors, text alignment). Hiding the terminal cursor during a `Spinner` animation prevents the cursor from awkwardly jumping or rendering alongside spinner frames. | ||
| **Action:** When implementing CLI UX changes that modify terminal state (like hiding a cursor), ALWAYS implement a `Drop` trait to ensure the state (like cursor visibility) is predictably restored if the process is interrupted or panics. | ||
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| use std::fmt::Write as FmtWrite; | ||
| use std::io::{self, Write}; | ||
|
badMade marked this conversation as resolved.
|
||
|
|
||
| use crossterm::cursor::{MoveToColumn, RestorePosition, SavePosition}; | ||
| use crossterm::cursor::{Hide, MoveToColumn, RestorePosition, SavePosition, Show}; | ||
| use crossterm::style::{Color, Print, ResetColor, SetForegroundColor, Stylize}; | ||
| use crossterm::terminal::{Clear, ClearType}; | ||
| use crossterm::{execute, queue}; | ||
|
|
@@ -47,6 +47,16 @@ impl Default for ColorTheme { | |
| #[derive(Debug, Default, Clone, PartialEq, Eq)] | ||
| pub struct Spinner { | ||
| frame_index: usize, | ||
| cursor_hidden: bool, | ||
| } | ||
|
|
||
| impl Drop for Spinner { | ||
| fn drop(&mut self) { | ||
| if self.cursor_hidden { | ||
| let mut out = io::stdout(); | ||
| let _ = execute!(out, Show); | ||
|
badMade marked this conversation as resolved.
Comment on lines
+56
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot, make changes based on the above suggestion. |
||
| } | ||
| } | ||
| } | ||
|
badMade marked this conversation as resolved.
|
||
|
|
||
|
badMade marked this conversation as resolved.
|
||
| impl Spinner { | ||
|
|
@@ -63,6 +73,10 @@ impl Spinner { | |
| theme: &ColorTheme, | ||
| out: &mut impl Write, | ||
| ) -> io::Result<()> { | ||
| if !self.cursor_hidden { | ||
| queue!(out, Hide)?; | ||
| self.cursor_hidden = true; | ||
| } | ||
| let frame = Self::FRAMES[self.frame_index % Self::FRAMES.len()]; | ||
| self.frame_index += 1; | ||
| queue!( | ||
|
|
@@ -85,14 +99,19 @@ impl Spinner { | |
| out: &mut impl Write, | ||
| ) -> io::Result<()> { | ||
| self.frame_index = 0; | ||
| execute!( | ||
| let show_result = execute!( | ||
| out, | ||
| MoveToColumn(0), | ||
| Clear(ClearType::CurrentLine), | ||
| SetForegroundColor(theme.spinner_done), | ||
| Print(format!("✔ {label}\n")), | ||
| ResetColor | ||
| )?; | ||
| ResetColor, | ||
| Show | ||
| ); | ||
| if show_result.is_ok() { | ||
| self.cursor_hidden = false; | ||
| } | ||
| show_result?; | ||
| out.flush() | ||
|
badMade marked this conversation as resolved.
|
||
| } | ||
|
|
||
|
|
@@ -103,14 +122,19 @@ impl Spinner { | |
| out: &mut impl Write, | ||
| ) -> io::Result<()> { | ||
| self.frame_index = 0; | ||
| execute!( | ||
| let show_result = execute!( | ||
| out, | ||
| MoveToColumn(0), | ||
| Clear(ClearType::CurrentLine), | ||
| SetForegroundColor(theme.spinner_failed), | ||
| Print(format!("✘ {label}\n")), | ||
| ResetColor | ||
| )?; | ||
| ResetColor, | ||
| Show | ||
| ); | ||
| if show_result.is_ok() { | ||
| self.cursor_hidden = false; | ||
| } | ||
| show_result?; | ||
| out.flush() | ||
| } | ||
|
badMade marked this conversation as resolved.
|
||
| } | ||
|
|
@@ -909,6 +933,7 @@ fn strip_ansi(input: &str) -> String { | |
| #[cfg(test)] | ||
| mod tests { | ||
| use super::{strip_ansi, MarkdownStreamState, Spinner, TerminalRenderer}; | ||
| use std::io::{self, Write}; | ||
|
|
||
| #[test] | ||
| fn renders_markdown_with_styling_and_lists() { | ||
|
|
@@ -1063,4 +1088,48 @@ mod tests { | |
| let output = String::from_utf8_lossy(&out); | ||
| assert!(output.contains("Working")); | ||
| } | ||
|
|
||
| struct AlwaysFailWriter; | ||
|
|
||
| impl Write for AlwaysFailWriter { | ||
| fn write(&mut self, _buf: &[u8]) -> io::Result<usize> { | ||
| Err(io::Error::other("write failed")) | ||
| } | ||
|
|
||
| fn flush(&mut self) -> io::Result<()> { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn spinner_finish_error_keeps_cursor_hidden_for_drop_recovery() { | ||
| let terminal_renderer = TerminalRenderer::new(); | ||
| let mut spinner = Spinner::new(); | ||
| let mut out = Vec::new(); | ||
| spinner | ||
| .tick("Working", terminal_renderer.color_theme(), &mut out) | ||
| .expect("tick succeeds"); | ||
|
|
||
| let mut failing_out = AlwaysFailWriter; | ||
| let result = spinner.finish("Done", terminal_renderer.color_theme(), &mut failing_out); | ||
|
|
||
| assert!(result.is_err()); | ||
| assert!(spinner.cursor_hidden); | ||
| } | ||
|
|
||
| #[test] | ||
| fn spinner_fail_error_keeps_cursor_hidden_for_drop_recovery() { | ||
| let terminal_renderer = TerminalRenderer::new(); | ||
| let mut spinner = Spinner::new(); | ||
| let mut out = Vec::new(); | ||
| spinner | ||
| .tick("Working", terminal_renderer.color_theme(), &mut out) | ||
| .expect("tick succeeds"); | ||
|
|
||
| let mut failing_out = AlwaysFailWriter; | ||
| let result = spinner.fail("Done", terminal_renderer.color_theme(), &mut failing_out); | ||
|
|
||
| assert!(result.is_err()); | ||
| assert!(spinner.cursor_hidden); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.