Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .Jules/palette.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-04-15 - [Cursor Visibility during Animations]
**Learning:** In terminal CLI applications using `crossterm`, a spinning indicator (`Spinner`) without hiding the cursor results in a flashing block jumping across the terminal as the frame updates, reducing the perceived polish of the application.
**Action:** Always hide the cursor when initiating a long-running animated terminal indicator, and explicitly restore cursor visibility in the normal completion and failure paths.
61 changes: 59 additions & 2 deletions rust/crates/rusty-claude-cli/src/render.rs
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};

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};
Expand Down Expand Up @@ -44,9 +44,10 @@ impl Default for ColorTheme {
}
}

#[derive(Debug, Default, Clone, PartialEq, Eq)]
#[derive(Debug, Default)]
pub struct Spinner {
frame_index: usize,
cursor_hidden: bool,
}

impl Spinner {
Expand All @@ -63,6 +64,10 @@ impl Spinner {
theme: &ColorTheme,
out: &mut impl Write,
) -> io::Result<()> {
if !self.cursor_hidden {
queue!(out, Hide)?;
self.cursor_hidden = true;
}
Comment thread
badMade marked this conversation as resolved.
let frame = Self::FRAMES[self.frame_index % Self::FRAMES.len()];
self.frame_index += 1;
queue!(
Expand All @@ -85,6 +90,10 @@ impl Spinner {
out: &mut impl Write,
) -> io::Result<()> {
self.frame_index = 0;
if self.cursor_hidden {
execute!(out, Show)?;
self.cursor_hidden = false;
}
execute!(
out,
MoveToColumn(0),
Expand All @@ -103,6 +112,10 @@ impl Spinner {
out: &mut impl Write,
) -> io::Result<()> {
self.frame_index = 0;
if self.cursor_hidden {
execute!(out, Show)?;
self.cursor_hidden = false;
}
Comment thread
badMade marked this conversation as resolved.
execute!(
out,
MoveToColumn(0),
Expand Down Expand Up @@ -1063,4 +1076,48 @@ mod tests {
let output = String::from_utf8_lossy(&out);
assert!(output.contains("Working"));
}

#[test]
fn spinner_hides_cursor_once_and_restores_on_finish() {
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("first tick succeeds");
spinner
.tick("Working", terminal_renderer.color_theme(), &mut out)
.expect("second tick succeeds");
spinner
.finish("Done", terminal_renderer.color_theme(), &mut out)
.expect("finish succeeds");
spinner
.finish("Done", terminal_renderer.color_theme(), &mut out)
.expect("second finish succeeds");

let output = String::from_utf8_lossy(&out);
assert_eq!(output.matches("\u{1b}[?25l").count(), 1);
assert_eq!(output.matches("\u{1b}[?25h").count(), 1);
}

#[test]
fn spinner_restores_cursor_on_fail() {
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");
spinner
.fail("Failed", terminal_renderer.color_theme(), &mut out)
.expect("fail succeeds");
spinner
.fail("Failed", terminal_renderer.color_theme(), &mut out)
.expect("second fail succeeds");

let output = String::from_utf8_lossy(&out);
assert_eq!(output.matches("\u{1b}[?25h").count(), 1);
}
}