Initial public release
Luxe 0.1.0: a coding agent harness for the terminal. See README.md to get started and CHANGELOG.md for releases.
This commit is contained in:
130
crates/luxe-tui/src/glyphs/detect.rs
Normal file
130
crates/luxe-tui/src/glyphs/detect.rs
Normal file
@@ -0,0 +1,130 @@
|
||||
//! Glyph detection — what the terminal and its fonts can draw: braille, Nerd Font icons, kitty.
|
||||
|
||||
use super::spinner::SPINNER_BRAILLE;
|
||||
use super::{GlyphTier, EVERY_ICON};
|
||||
|
||||
/// Whether the terminal in front of the reader can draw the braille sweep.
|
||||
///
|
||||
/// Braille is not in the terminal's face and never needed to be — it is one cell, nothing is
|
||||
/// aligned to it, and every desktop platform ships a font that covers the block in its fallback
|
||||
/// chain. See [`braille_drawable`] for how the question is answered.
|
||||
pub(super) fn braille_available() -> bool {
|
||||
braille_drawable(&|name| std::env::var(name).ok().filter(|value| !value.is_empty()), || {
|
||||
fontconfig_has_all(SPINNER_BRAILLE.iter().filter_map(|frame| frame.chars().next()))
|
||||
})
|
||||
}
|
||||
|
||||
/// [`braille_available`], with the environment and the font probe passed in.
|
||||
///
|
||||
/// Asked of the terminal that draws, which is not always this machine:
|
||||
///
|
||||
/// - The kernel console (`TERM=linux`) cannot: its font is a few hundred glyphs and braille is not
|
||||
/// among them, whatever is installed.
|
||||
/// - macOS and Windows can: they ship Apple Symbols and Segoe UI Symbol, both covering
|
||||
/// `U+2800`–`U+28FF` in the documented system fallback chain.
|
||||
/// - Over SSH the terminal is at the other end, so this machine's fonts say nothing about it: a
|
||||
/// headless server with no fonts at all may be shown on a laptop that has braille.
|
||||
/// - kitty, Ghostty and WezTerm draw braille themselves, from the cell box, the way they draw box
|
||||
/// lines, so no font is involved.
|
||||
///
|
||||
/// Anything else is a local terminal leaning on fontconfig, which is asked about the exact ten
|
||||
/// frames — on a box carrying only Liberation and Noto that still resolves, to Noto Sans Symbols 2.
|
||||
pub(super) fn braille_drawable(
|
||||
var: &dyn Fn(&str) -> Option<String>,
|
||||
fonts: impl FnOnce() -> bool,
|
||||
) -> bool {
|
||||
let term = var("TERM").unwrap_or_default();
|
||||
if term == "linux" {
|
||||
return false;
|
||||
}
|
||||
if cfg!(any(target_os = "macos", target_os = "windows")) {
|
||||
return true;
|
||||
}
|
||||
if var("SSH_CONNECTION").is_some() || var("SSH_TTY").is_some() {
|
||||
return true;
|
||||
}
|
||||
let program = var("TERM_PROGRAM").unwrap_or_default().to_lowercase();
|
||||
let self_drawn = ["kitty", "ghostty", "wezterm"]
|
||||
.iter()
|
||||
.any(|terminal| term.contains(terminal) || program == *terminal);
|
||||
self_drawn || fonts()
|
||||
}
|
||||
|
||||
/// Whether one installed font carries every character in `wanted`.
|
||||
///
|
||||
/// fontconfig ANDs a charset list, so the question costs a single process however many characters
|
||||
/// it is asked about. One owner, because two callers ask it and the query is easy to get subtly
|
||||
/// wrong — a list that is ORed would answer a different and much easier question.
|
||||
fn fontconfig_has_all(wanted: impl IntoIterator<Item = char>) -> bool {
|
||||
let mut codepoints: Vec<u32> = wanted.into_iter().map(u32::from).collect();
|
||||
codepoints.sort_unstable();
|
||||
codepoints.dedup();
|
||||
if codepoints.is_empty() {
|
||||
return false;
|
||||
}
|
||||
let mut charset = String::from(":charset=");
|
||||
for (index, cp) in codepoints.iter().enumerate() {
|
||||
if index > 0 {
|
||||
charset.push(' ');
|
||||
}
|
||||
charset.push_str(&format!("{cp:x}"));
|
||||
}
|
||||
std::process::Command::new("fc-list")
|
||||
.args([charset.as_str(), "family"])
|
||||
.stderr(std::process::Stdio::null())
|
||||
.output()
|
||||
.is_ok_and(|probe| probe.status.success() && !probe.stdout.is_empty())
|
||||
}
|
||||
|
||||
/// Whether the Material icons can actually be drawn on this machine.
|
||||
///
|
||||
/// A terminal cannot be asked what its font contains, so this asks the two questions that do have
|
||||
/// answers:
|
||||
///
|
||||
/// - **kitty ships its own symbol font** (`SymbolsNerdFontMono-Regular.ttf`), so its icons render
|
||||
/// whatever the user configured. Measured rather than read: with `font_family=Liberation Mono`,
|
||||
/// which contains none of these glyphs, kitty draws them anyway.
|
||||
/// - **otherwise, ask fontconfig** whether one installed font provides **every** icon in the
|
||||
/// vocabulary. On Linux that is a fair proxy for renderable, because terminals fall back through
|
||||
/// fontconfig for a glyph their configured font lacks — ghostty draws the Material icons under
|
||||
/// Liberation Mono for exactly that reason, and it bundles no font of its own.
|
||||
///
|
||||
/// Where fontconfig isn't the answer — macOS, Windows, a container without `fc-list` — this is false
|
||||
/// and the fallback vocabulary is used. Conservative on purpose: a reader who *has* a patched font sees
|
||||
/// plain shapes until they set `glyphs`, which is a smaller disappointment than a screen of boxes.
|
||||
///
|
||||
/// # Why the whole vocabulary and not a sample
|
||||
///
|
||||
/// One codepoint is no answer about ninety-odd: a partial font has most icons and misses a few,
|
||||
/// which a one-glyph probe cannot see. Fifty-six of these live in the Material Design block that
|
||||
/// Nerd Fonts **moved** in v3, so a v2 font passes a probe for, say, the cog and then draws boxes
|
||||
/// for most of the dashboard.
|
||||
///
|
||||
/// fontconfig ANDs a charset list, so the exact question costs one process and about six
|
||||
/// milliseconds. Requiring a *single* font to carry all of them is stricter than per-glyph
|
||||
/// fallback strictly needs, and deliberately so: coverage scattered across three fonts renders at
|
||||
/// three weights, which is the same disappointment arriving more slowly.
|
||||
pub(super) fn nerd_icons_available() -> bool {
|
||||
if is_kitty() {
|
||||
return true;
|
||||
}
|
||||
// Derived from the table, so an icon added in a range nothing else uses extends the probe
|
||||
// rather than slipping past it.
|
||||
fontconfig_has_all(EVERY_ICON.iter().filter_map(|icon| icon.at(GlyphTier::Nerd).chars().next()))
|
||||
}
|
||||
|
||||
/// Whether this is kitty — the one terminal that answers the question above by itself.
|
||||
///
|
||||
/// `TERM` is the only authoritative signal: the innermost pty sets it, so a `TERM` naming a terminal
|
||||
/// names *this* terminal. `KITTY_WINDOW_ID` is inherited — a shell started from kitty carries it into
|
||||
/// whatever it launches, so an alacritty opened from kitty would claim to be one. It is therefore
|
||||
/// trusted only under a multiplexer's `TERM`, which describes the multiplexer and leaves nothing
|
||||
/// better to go on.
|
||||
fn is_kitty() -> bool {
|
||||
let term = std::env::var("TERM").unwrap_or_default();
|
||||
if term.contains("kitty") {
|
||||
return true;
|
||||
}
|
||||
let multiplexed = term.starts_with("tmux") || term.starts_with("screen");
|
||||
multiplexed && std::env::var_os("KITTY_WINDOW_ID").is_some()
|
||||
}
|
||||
93
crates/luxe-tui/src/glyphs/format.rs
Normal file
93
crates/luxe-tui/src/glyphs/format.rs
Normal file
@@ -0,0 +1,93 @@
|
||||
//! Glyph formatting — how a tier spells its icons, and the helpers that set an icon beside words.
|
||||
|
||||
use super::detect::nerd_icons_available;
|
||||
use super::state::tier;
|
||||
use super::{GlyphTier, Icon};
|
||||
use luxe_core::config::GlyphStyle;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
impl GlyphTier {
|
||||
/// The tier a preference resolves to, asking the system only when it says `auto`.
|
||||
///
|
||||
/// `auto` never picks [`Self::NerdWide`], because the question it would have to answer — which
|
||||
/// cut of the face the terminal loaded — has no answer: both report a one-cell advance.
|
||||
///
|
||||
/// Knowing the terminal does not answer it either. Ghostty and kitty spread an icon into a
|
||||
/// following space only when the face drew it wide — the `Mono` cut stays in its cell under
|
||||
/// Ghostty, measured — so they inherit the question rather than settle it.
|
||||
pub fn resolve(preference: GlyphStyle) -> Self {
|
||||
match preference {
|
||||
GlyphStyle::Nerd => Self::Nerd,
|
||||
GlyphStyle::NerdWide => Self::NerdWide,
|
||||
GlyphStyle::Unicode => Self::Unicode,
|
||||
GlyphStyle::Ascii => Self::Ascii,
|
||||
GlyphStyle::Auto | GlyphStyle::Unknown => {
|
||||
if nerd_icons_available() {
|
||||
Self::Nerd
|
||||
} else {
|
||||
Self::Unicode
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The clearance between an icon and the words it labels, at this tier.
|
||||
///
|
||||
/// Two cells where the face draws its icons two cells wide, so the first of them is the one the
|
||||
/// icon is already painting on. Everywhere else the usual single space.
|
||||
pub const fn gap(self) -> &'static str {
|
||||
match self {
|
||||
Self::NerdWide => " ",
|
||||
Self::Nerd | Self::Unicode | Self::Ascii => " ",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The clearance between an icon and the words it labels, at the resolved tier.
|
||||
///
|
||||
/// One owner for the gap, because it is spent in about thirty places and a surface that hard-codes
|
||||
/// its own space is a surface that stops moving with the rest of them.
|
||||
pub fn icon_gap() -> &'static str {
|
||||
tier().gap()
|
||||
}
|
||||
|
||||
/// `glyph` and the clearance after it, or nothing where the tier draws no glyph.
|
||||
pub fn icon_prefix(glyph: &str) -> String {
|
||||
if glyph.is_empty() {
|
||||
String::new()
|
||||
} else {
|
||||
format!("{glyph}{}", icon_gap())
|
||||
}
|
||||
}
|
||||
|
||||
/// A glyph the reader configured, where Nerd Font icons are drawn, and `fallback` elsewhere.
|
||||
///
|
||||
/// What a person puts in a config is most likely a Nerd Font mark, which is tofu at the Unicode and
|
||||
/// ASCII tiers; there, and when they named none, the fallback's own rendering stands in.
|
||||
pub fn configured_glyph(configured: Option<&str>, fallback: Icon) -> &str {
|
||||
match (configured, tier()) {
|
||||
(Some(glyph), GlyphTier::Nerd | GlyphTier::NerdWide) => glyph,
|
||||
_ => fallback.glyph(),
|
||||
}
|
||||
}
|
||||
|
||||
/// `{glyph}{gap}{label}`, or bare `label` where the tier draws no glyph.
|
||||
///
|
||||
/// The shape all but a handful of rows in the product are built from. An omitted icon takes its
|
||||
/// clearance with it, so the words land on the same column at every tier rather than one cell in
|
||||
/// from where they belong.
|
||||
pub fn icon_label(glyph: &str, label: &str) -> String {
|
||||
format!("{}{label}", icon_prefix(glyph))
|
||||
}
|
||||
|
||||
/// Cells `glyph` and its clearance occupy — zero where the tier draws none.
|
||||
///
|
||||
/// For the surfaces that have to *measure* a row before drawing it: a panel sizing its box to its own
|
||||
/// title, a dock row working out what is left for a path.
|
||||
pub fn icon_cells(glyph: &str) -> usize {
|
||||
if glyph.is_empty() {
|
||||
0
|
||||
} else {
|
||||
UnicodeWidthStr::width(glyph) + UnicodeWidthStr::width(icon_gap())
|
||||
}
|
||||
}
|
||||
79
crates/luxe-tui/src/glyphs/portable.txt
Normal file
79
crates/luxe-tui/src/glyphs/portable.txt
Normal file
@@ -0,0 +1,79 @@
|
||||
# Characters the fallback tiers are allowed to draw.
|
||||
#
|
||||
# The `unicode` tier exists for a reader whose font has no private-use icons. That only helps if the
|
||||
# substitutes are in the font they fell back to, and for a long time nothing checked: `glyphs.rs`
|
||||
# asserted its fallbacks were free of private-use codepoints and one cell wide by the Unicode width
|
||||
# property, neither of which is a question about the reader's font. Measured, Liberation Mono — the
|
||||
# control font the wordmark probe uses precisely because it is the plainest monospace a distribution
|
||||
# ships — was missing twenty of the thirty-one marks the TUI draws at every tier, `◆ ▸ ◉ ✓ ✗ ❮ ❯`
|
||||
# among them. Substitutes chosen to be safe, absent from the font that defines bare.
|
||||
#
|
||||
# So the tier is held to this list instead, and the list is a measurement. Two ways in:
|
||||
#
|
||||
# synthesized U+2500–U+259F. The terminal draws these against the cell box rather than scaling a
|
||||
# font outline into it — alacritty's `builtin_box_drawing`, wezterm's
|
||||
# `custom_block_glyphs`, kitty's box drawing, ghostty's sprite face, Windows
|
||||
# Terminal's `BuiltinGlyphs`. The font is not in the path, so no survey applies.
|
||||
# Not listed below; the range is allowed wholesale.
|
||||
#
|
||||
# surveyed present in every face in the panel. Listed one per line.
|
||||
#
|
||||
# The panel is the faces a reader plausibly already has, not the ones we would pick:
|
||||
#
|
||||
# Liberation Mono the metric-compatible Arial/Courier stand-in, on every distribution
|
||||
# DejaVu Sans Mono the classic Linux default
|
||||
# Noto Sans Mono the Google/ChromeOS default
|
||||
# Cascadia Mono the Windows Terminal default
|
||||
# Adwaita Mono the GNOME 48 default
|
||||
# Hack common
|
||||
# Fira Code common
|
||||
# JetBrains Mono common
|
||||
# Source Code Pro common
|
||||
#
|
||||
# Regenerate with `scripts/glyph-survey.py`, which reads the installed files with fontTools and
|
||||
# fails rather than guessing when a face is absent. Adding a character here is a claim that every
|
||||
# one of those nine faces has it — make the script say so, do not hand-edit.
|
||||
#
|
||||
# Leaving the list is allowed and sometimes right: the `nerd` tier draws whatever a patched font
|
||||
# carries, and that is what the tier is for. This governs the tiers that promise portability.
|
||||
|
||||
U+00B1 ± PLUS-MINUS SIGN
|
||||
U+00B7 · MIDDLE DOT
|
||||
U+00D7 × MULTIPLICATION SIGN
|
||||
U+00F7 ÷ DIVISION SIGN
|
||||
U+00A4 ¤ CURRENCY SIGN
|
||||
U+00A7 § SECTION SIGN
|
||||
U+00AC ¬ NOT SIGN
|
||||
U+00B0 ° DEGREE SIGN
|
||||
U+00B6 ¶ PILCROW SIGN
|
||||
U+2013 – EN DASH
|
||||
U+2014 — EM DASH
|
||||
U+2020 † DAGGER
|
||||
U+2021 ‡ DOUBLE DAGGER
|
||||
U+2022 • BULLET
|
||||
U+2026 … HORIZONTAL ELLIPSIS
|
||||
U+2039 ‹ SINGLE LEFT-POINTING ANGLE QUOTATION MARK
|
||||
U+203A › SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
|
||||
U+00AB « LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
|
||||
U+00BB » RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
|
||||
U+2190 ← LEFTWARDS ARROW
|
||||
U+2191 ↑ UPWARDS ARROW
|
||||
U+2192 → RIGHTWARDS ARROW
|
||||
U+2193 ↓ DOWNWARDS ARROW
|
||||
U+2219 ∙ BULLET OPERATOR
|
||||
U+221A √ SQUARE ROOT
|
||||
U+221E ∞ INFINITY
|
||||
U+2248 ≈ ALMOST EQUAL TO
|
||||
U+2261 ≡ IDENTICAL TO
|
||||
U+2264 ≤ LESS-THAN OR EQUAL TO
|
||||
U+2265 ≥ GREATER-THAN OR EQUAL TO
|
||||
U+25A0 ■ BLACK SQUARE
|
||||
U+25A1 □ WHITE SQUARE
|
||||
U+25AA ▪ BLACK SMALL SQUARE
|
||||
U+25AB ▫ WHITE SMALL SQUARE
|
||||
U+25B2 ▲ BLACK UP-POINTING TRIANGLE
|
||||
U+25BA ► BLACK RIGHT-POINTING POINTER
|
||||
U+25BC ▼ BLACK DOWN-POINTING TRIANGLE
|
||||
U+25C4 ◄ BLACK LEFT-POINTING POINTER
|
||||
U+25CB ○ WHITE CIRCLE
|
||||
U+25CF ● BLACK CIRCLE
|
||||
83
crates/luxe-tui/src/glyphs/spinner.rs
Normal file
83
crates/luxe-tui/src/glyphs/spinner.rs
Normal file
@@ -0,0 +1,83 @@
|
||||
//! The spinner — its frames in each vocabulary, and how many ticks each frame holds.
|
||||
|
||||
use super::state::{braille, tier};
|
||||
use super::GlyphTier;
|
||||
|
||||
/// The ten-frame braille sweep, when something on the machine can draw it.
|
||||
///
|
||||
/// Not in the terminal's font, and it does not have to be: this is the one glyph class that is
|
||||
/// allowed to fall back, because it occupies one cell by `wcwidth` and nothing is aligned to it.
|
||||
/// The survey that produced `glyphs/portable.txt` asked the wrong question of it — *does the
|
||||
/// terminal face contain this* — and JetBrainsMono Nerd Font contains none of the 256 patterns
|
||||
/// while rendering them perfectly well, out of Noto Sans Symbols 2.
|
||||
///
|
||||
/// Ten frames rather than four is the whole point: the sweep reads as motion instead of as four
|
||||
/// positions. It is pi's set, which is the de-facto one.
|
||||
pub(super) const SPINNER_BRAILLE: [&str; 10] = [
|
||||
"\u{280B}", "\u{2819}", "\u{2839}", "\u{2838}", "\u{283C}", "\u{2834}", "\u{2826}", "\u{2827}",
|
||||
"\u{2807}", "\u{280F}",
|
||||
];
|
||||
|
||||
/// One quarter block orbiting the corners of its cell.
|
||||
///
|
||||
/// Block Elements, which terminals synthesize against the cell box rather than scaling a font
|
||||
/// outline into it, so the animation lands the same whether the reader has a patched font or the
|
||||
/// plainest monospace their distribution shipped — the wordmark's guarantee, for the same reason
|
||||
/// and out of the same range.
|
||||
///
|
||||
/// **The mass has to stay constant**, which is what rules out the obvious eight-frame version
|
||||
/// alternating quarters with halves. Rotation is read from a shape that moves; one that also grows
|
||||
/// and shrinks reads as a flicker between unrelated glyphs, and was described by the first reader
|
||||
/// to meet it as "different sized square and rectangle glyphs" — which is exactly what it was.
|
||||
/// Four corners at one size is a coarser animation and a better spinner.
|
||||
pub(super) const SPINNER_BLOCKS: [&str; 4] = ["\u{2598}", "\u{259D}", "\u{2597}", "\u{2596}"];
|
||||
|
||||
/// The same sweep for a terminal that is not speaking UTF-8.
|
||||
pub(super) const SPINNER_ASCII: [&str; 4] = ["|", "/", "-", "\\"];
|
||||
|
||||
/// Render ticks each frame of the **block** sweep is held for.
|
||||
///
|
||||
/// The loop ticks at 30fps because that is what streamed text and the activity comet were drawn
|
||||
/// for, and a spinner read straight off that counter turns near four times a second — faster than
|
||||
/// an eye resolves as rotation. Four quarter blocks at that rate are not motion, they are a
|
||||
/// flicker, so they step on their own clock: three ticks a frame puts one revolution just under
|
||||
/// 400ms.
|
||||
pub(super) const SPINNER_BLOCK_TICKS: usize = 3;
|
||||
|
||||
/// Render ticks each frame of the **braille** sweep is held for.
|
||||
///
|
||||
/// One: the sweep steps on every frame the loop draws, which is the smoothest a 30fps loop can be
|
||||
/// and is what the braille did before it inherited the blocks' clock. Ten frames of one or two
|
||||
/// dots is a shimmer at that rate rather than the flicker four quarter blocks would be — the whole
|
||||
/// reason the two vocabularies do not share a number.
|
||||
///
|
||||
/// A held frame can only last a multiple of 33ms, so the options were 33, 67 and 100. It sat at
|
||||
/// 100 for a release by accident; 67 was not tellable from it by eye, which leaves the one that is.
|
||||
const SPINNER_BRAILLE_TICKS: usize = 1;
|
||||
|
||||
/// Render ticks each frame of the sweep the process will actually draw is held for.
|
||||
///
|
||||
/// For the callers that have to advance the spinner by a whole frame rather than by a tick.
|
||||
pub(crate) fn spinner_ticks_per_frame() -> usize {
|
||||
match tier() {
|
||||
GlyphTier::Ascii => SPINNER_BLOCK_TICKS,
|
||||
_ if braille() => SPINNER_BRAILLE_TICKS,
|
||||
_ => SPINNER_BLOCK_TICKS,
|
||||
}
|
||||
}
|
||||
|
||||
/// The turn spinner at render tick `frame`, wrapping.
|
||||
///
|
||||
/// One owner, because there were two: an eight-frame array in the status model and a ten-frame one
|
||||
/// in the bar, spinning at different rates in the same window.
|
||||
pub fn spinner(frame: usize) -> &'static str {
|
||||
// Each vocabulary keeps its own cadence: ten braille frames and four quarter blocks are not the
|
||||
// same animation, and one number for both slows whichever it was not chosen for.
|
||||
let step = frame / spinner_ticks_per_frame();
|
||||
match tier() {
|
||||
// `ascii` means assume nothing beyond ASCII, which a fallback font cannot rescue.
|
||||
GlyphTier::Ascii => SPINNER_ASCII[step % SPINNER_ASCII.len()],
|
||||
_ if braille() => SPINNER_BRAILLE[step % SPINNER_BRAILLE.len()],
|
||||
_ => SPINNER_BLOCKS[step % SPINNER_BLOCKS.len()],
|
||||
}
|
||||
}
|
||||
59
crates/luxe-tui/src/glyphs/state.rs
Normal file
59
crates/luxe-tui/src/glyphs/state.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
//! Glyph state — the vocabulary and braille switch every glyph call reads, set once at startup.
|
||||
|
||||
use super::detect::braille_available;
|
||||
use super::GlyphTier;
|
||||
use luxe_core::config::GlyphStyle;
|
||||
use std::sync::atomic::{AtomicU8, Ordering};
|
||||
|
||||
/// The process-wide tier, as a discriminant so it can be swapped atomically.
|
||||
static TIER: AtomicU8 = AtomicU8::new(0);
|
||||
|
||||
/// Whether the spinner may use its braille sweep.
|
||||
///
|
||||
/// Off by default, which is what keeps a test, a fixture and a generated site asset drawing the
|
||||
/// same thing on any machine: the block orbit asks nothing of any font. Only the startup path
|
||||
/// turns it on, and only after measuring.
|
||||
static BRAILLE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
|
||||
|
||||
/// Resolves the whole vocabulary from `preference` and installs it process-wide.
|
||||
///
|
||||
/// One call, because there are two questions and they are asked at the same two moments — startup
|
||||
/// and `/reload`. Setting the tier and forgetting the sweep is the mistake this exists to make
|
||||
/// impossible.
|
||||
pub fn resolve_vocabulary(preference: GlyphStyle) {
|
||||
set_tier(GlyphTier::resolve(preference));
|
||||
set_braille(braille_available());
|
||||
}
|
||||
|
||||
/// Whether the spinner is currently drawing its braille sweep.
|
||||
pub fn braille() -> bool {
|
||||
BRAILLE.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Chooses the spinner's vocabulary. Exposed for tests and for the startup probe.
|
||||
pub fn set_braille(available: bool) {
|
||||
BRAILLE.store(available, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// Sets the vocabulary every [`Icon::glyph`](super::Icon::glyph) call renders with.
|
||||
pub fn set_tier(tier: GlyphTier) {
|
||||
TIER.store(
|
||||
match tier {
|
||||
GlyphTier::Nerd => 0,
|
||||
GlyphTier::Unicode => 1,
|
||||
GlyphTier::Ascii => 2,
|
||||
GlyphTier::NerdWide => 3,
|
||||
},
|
||||
Ordering::Relaxed,
|
||||
);
|
||||
}
|
||||
|
||||
/// The tier [`Icon::glyph`](super::Icon::glyph) is currently rendering with.
|
||||
pub fn tier() -> GlyphTier {
|
||||
match TIER.load(Ordering::Relaxed) {
|
||||
1 => GlyphTier::Unicode,
|
||||
2 => GlyphTier::Ascii,
|
||||
3 => GlyphTier::NerdWide,
|
||||
_ => GlyphTier::Nerd,
|
||||
}
|
||||
}
|
||||
513
crates/luxe-tui/src/glyphs/tests.rs
Normal file
513
crates/luxe-tui/src/glyphs/tests.rs
Normal file
@@ -0,0 +1,513 @@
|
||||
use super::*;
|
||||
|
||||
/// The terminal that draws is the one asked: SSH and the terminals that draw braille themselves
|
||||
/// get it without a local font, and the kernel console never does.
|
||||
#[test]
|
||||
fn braille_is_asked_of_the_terminal_that_draws() {
|
||||
let env = |pairs: &'static [(&'static str, &'static str)]| {
|
||||
move |name: &str| {
|
||||
pairs.iter().find(|(key, _)| *key == name).map(|(_, value)| (*value).to_owned())
|
||||
}
|
||||
};
|
||||
let no_fonts = || false;
|
||||
let desktop = cfg!(any(target_os = "macos", target_os = "windows"));
|
||||
|
||||
assert!(!braille_drawable(&env(&[("TERM", "linux"), ("SSH_TTY", "/dev/pts/1")]), || true));
|
||||
assert!(braille_drawable(
|
||||
&env(&[("TERM", "xterm-256color"), ("SSH_TTY", "/dev/pts/1")]),
|
||||
no_fonts
|
||||
));
|
||||
assert!(braille_drawable(&env(&[("SSH_CONNECTION", "10.0.0.2 5 10.0.0.1 22")]), no_fonts));
|
||||
assert!(braille_drawable(&env(&[("TERM", "xterm-kitty")]), no_fonts));
|
||||
assert!(braille_drawable(&env(&[("TERM", "xterm-ghostty")]), no_fonts));
|
||||
assert!(braille_drawable(&env(&[("TERM_PROGRAM", "WezTerm")]), no_fonts));
|
||||
assert_eq!(braille_drawable(&env(&[("TERM", "xterm-256color")]), no_fonts), desktop);
|
||||
assert!(braille_drawable(&env(&[("TERM", "xterm-256color")]), || true), "a font draws it");
|
||||
}
|
||||
use std::collections::HashMap;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
const TIERS: [GlyphTier; 4] =
|
||||
[GlyphTier::Nerd, GlyphTier::NerdWide, GlyphTier::Unicode, GlyphTier::Ascii];
|
||||
|
||||
/// The whole point of the slice: below the nerd tier, nothing on screen may need a patched font.
|
||||
/// Asserted over the table rather than over a screenshot, because a table is exhaustive and an
|
||||
/// eyeball is not.
|
||||
#[test]
|
||||
fn no_private_use_codepoint_escapes_the_fallback_tiers() {
|
||||
for tier in [GlyphTier::Unicode, GlyphTier::Ascii] {
|
||||
for icon in EVERY_ICON {
|
||||
for glyph in icon.at(tier).chars() {
|
||||
let cp = u32::from(glyph);
|
||||
assert!(
|
||||
!(0xE000..=0xF8FF).contains(&cp) && !(0xF0000..=0xFFFFD).contains(&cp),
|
||||
"{icon:?} at {tier:?} draws U+{cp:04X}, which needs a Nerd Font"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `ascii` has to survive a terminal that is not speaking UTF-8 at all.
|
||||
#[test]
|
||||
fn the_ascii_tier_is_actually_ascii() {
|
||||
for icon in EVERY_ICON {
|
||||
let glyph = icon.at(GlyphTier::Ascii);
|
||||
assert!(glyph.is_ascii(), "{icon:?} at ascii draws {glyph:?}");
|
||||
}
|
||||
for mark in EVERY_MARK {
|
||||
let glyph = mark.at(GlyphTier::Ascii);
|
||||
assert!(glyph.is_ascii(), "{mark:?} at ascii draws {glyph:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Characters a bare font has been *measured* to contain, plus the range terminals draw
|
||||
/// themselves. Parsed from the survey's own output so the two cannot drift.
|
||||
fn portable_characters() -> std::collections::HashSet<char> {
|
||||
include_str!("../glyphs/portable.txt")
|
||||
.lines()
|
||||
.filter_map(|line| line.strip_prefix("U+"))
|
||||
.filter_map(|line| line.split('\t').next())
|
||||
.filter_map(|hex| u32::from_str_radix(hex, 16).ok())
|
||||
.filter_map(char::from_u32)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The point of the fallback tiers, and the thing that was never checked: a substitute is only
|
||||
/// a substitute if the font the reader fell back to actually has it.
|
||||
///
|
||||
/// `no_private_use_codepoint_escapes_the_fallback_tiers` proves these are not Nerd Font glyphs
|
||||
/// and `every_rendering_is_one_cell_or_omitted` proves the Unicode width property says one
|
||||
/// cell. Neither is a question about the reader's font, and for a long time nothing was:
|
||||
/// Liberation Mono, the plainest monospace a distribution ships, was missing `◆ ▸ ◉ ✓ ✗ ❯`
|
||||
/// and fourteen more — every one of them chosen *because* it was supposed to be safe.
|
||||
///
|
||||
/// `scripts/glyph-survey.py --check` is the other half: this asserts the vocabulary stays
|
||||
/// inside the list, that asserts the list stays true of the nine faces it was measured from.
|
||||
#[test]
|
||||
fn the_fallback_tiers_draw_only_what_a_bare_font_has() {
|
||||
let portable = portable_characters();
|
||||
// U+2500-U+259F is synthesized by the terminal against the cell box, so the font is not in
|
||||
// the path and no survey applies.
|
||||
let synthesized = |c: char| ('\u{2500}'..='\u{259f}').contains(&c);
|
||||
let mut checked = 0;
|
||||
for tier in [GlyphTier::Unicode, GlyphTier::Ascii] {
|
||||
let renderings = EVERY_ICON
|
||||
.iter()
|
||||
.map(|icon| (format!("{icon:?}"), icon.at(tier)))
|
||||
.chain(EVERY_MARK.iter().map(|mark| (format!("{mark:?}"), mark.at(tier))));
|
||||
for (what, glyph) in renderings {
|
||||
for ch in glyph.chars() {
|
||||
checked += 1;
|
||||
assert!(
|
||||
ch.is_ascii() || synthesized(ch) || portable.contains(&ch),
|
||||
"{what} at {tier:?} draws {ch:?} (U+{:04X}), which is not in \
|
||||
glyphs/portable.txt — add it there with a survey run that proves every \
|
||||
face on the panel has it, or pick a character that is already listed",
|
||||
u32::from(ch)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(checked > 100, "the survey should be covering the whole vocabulary, saw {checked}");
|
||||
}
|
||||
|
||||
/// A mark is part of a row's grammar, so unlike an icon it may never stand down.
|
||||
#[test]
|
||||
fn no_mark_is_ever_omitted() {
|
||||
for mark in EVERY_MARK {
|
||||
for tier in TIERS {
|
||||
let glyph = mark.at(tier);
|
||||
assert!(!glyph.is_empty(), "{mark:?} has no rendering at {tier:?}");
|
||||
assert_eq!(glyph.chars().count(), 1, "{mark:?} at {tier:?} is not one glyph");
|
||||
assert_eq!(
|
||||
UnicodeWidthStr::width(glyph),
|
||||
1,
|
||||
"{mark:?} at {tier:?} is not one cell wide: {glyph:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A mark drops into `format!` where a literal used to sit, and has to render at the live tier
|
||||
/// rather than the one that was current when the string was built.
|
||||
/// A configured glyph is drawn where Nerd Font icons are, and the fallback's own rendering
|
||||
/// stands in at the tiers where a private-use mark would be tofu, and when none is configured.
|
||||
#[test]
|
||||
fn a_configured_glyph_is_drawn_only_where_nerd_icons_are() {
|
||||
let jira = Some("\u{f0303}");
|
||||
set_tier(GlyphTier::Nerd);
|
||||
assert_eq!(configured_glyph(jira, Icon::MentionSource), "\u{f0303}");
|
||||
assert_eq!(configured_glyph(None, Icon::MentionSource), "\u{f04fc}");
|
||||
set_tier(GlyphTier::NerdWide);
|
||||
assert_eq!(configured_glyph(jira, Icon::MentionSource), "\u{f0303}");
|
||||
set_tier(GlyphTier::Unicode);
|
||||
assert_eq!(configured_glyph(jira, Icon::MentionSource), "▪");
|
||||
set_tier(GlyphTier::Ascii);
|
||||
assert_eq!(configured_glyph(jira, Icon::MentionSource), "-");
|
||||
set_tier(GlyphTier::Nerd);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_mark_formats_at_the_live_tier() {
|
||||
set_tier(GlyphTier::Unicode);
|
||||
assert_eq!(format!("{} open", Mark::FoldOpen), "▼ open");
|
||||
set_tier(GlyphTier::Ascii);
|
||||
assert_eq!(format!("{} open", Mark::FoldOpen), "v open");
|
||||
set_tier(GlyphTier::Nerd);
|
||||
assert_eq!(format!("{} open", Mark::FoldOpen), "▾ open");
|
||||
assert!(Mark::FoldOpen.leads("▼ open"), "leads accepts every tier's rendering");
|
||||
assert!(Mark::FoldOpen.leads("v open"));
|
||||
}
|
||||
|
||||
/// Layout budgets one cell per icon everywhere. A two-cell substitute shifts every column after it,
|
||||
/// which is how a "portable" fallback shears the row it was supposed to rescue. An icon may also
|
||||
/// render as *nothing* below the nerd tier — that is the deliberate omission, and it takes its
|
||||
/// separator with it through [`Icon::prefix`].
|
||||
#[test]
|
||||
fn every_rendering_is_one_cell_or_omitted() {
|
||||
for tier in TIERS {
|
||||
for icon in EVERY_ICON {
|
||||
let glyph = icon.at(tier);
|
||||
if glyph.is_empty() {
|
||||
assert!(
|
||||
!matches!(tier, GlyphTier::Nerd | GlyphTier::NerdWide),
|
||||
"{icon:?} has no shipped glyph"
|
||||
);
|
||||
continue;
|
||||
}
|
||||
assert_eq!(
|
||||
glyph.chars().count(),
|
||||
1,
|
||||
"{icon:?} at {tier:?} is not a single glyph: {glyph:?}"
|
||||
);
|
||||
assert_eq!(
|
||||
UnicodeWidthStr::width(glyph),
|
||||
1,
|
||||
"{icon:?} at {tier:?} is not one cell wide: {glyph:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An omitted icon must not leave a hole where it used to be.
|
||||
#[test]
|
||||
fn an_omitted_icon_takes_its_separator_with_it() {
|
||||
set_tier(GlyphTier::Unicode);
|
||||
assert_eq!(Icon::ToolBash.at(GlyphTier::Unicode), "", "tool cards drop the icon");
|
||||
assert_eq!(Icon::ToolBash.prefix(), "", "and the space that followed it");
|
||||
assert_eq!(Icon::Palette.prefix(), format!("{} ", Icon::Palette.at(GlyphTier::Unicode)));
|
||||
assert_eq!(icon_cells(Icon::ToolBash.glyph()), 0, "and the cells it was measured at");
|
||||
set_tier(GlyphTier::Nerd);
|
||||
}
|
||||
|
||||
/// The wide tier is the shipped vocabulary with one more cell of clearance — same shapes, same
|
||||
/// words, everything after an icon moved over by exactly one column. Anything else and it is a
|
||||
/// second look rather than the same one on a different cut of the font.
|
||||
#[test]
|
||||
fn the_wide_tier_draws_the_nerd_shapes_with_a_cell_more_air() {
|
||||
for icon in EVERY_ICON {
|
||||
assert_eq!(
|
||||
icon.at(GlyphTier::NerdWide),
|
||||
icon.at(GlyphTier::Nerd),
|
||||
"{icon:?} is drawn differently"
|
||||
);
|
||||
}
|
||||
assert_eq!(GlyphTier::NerdWide.gap().len(), GlyphTier::Nerd.gap().len() + 1);
|
||||
|
||||
set_tier(GlyphTier::NerdWide);
|
||||
assert_eq!(icon_label(Icon::Model.glyph(), "opus"), format!("{} opus", Icon::Model.glyph()));
|
||||
assert_eq!(icon_cells(Icon::Model.glyph()), 3);
|
||||
// The omission rule outranks the gap: a row with no icon spends nothing, however much room
|
||||
// the tier would have given one.
|
||||
assert_eq!(icon_prefix(""), "");
|
||||
set_tier(GlyphTier::Nerd);
|
||||
assert_eq!(icon_label(Icon::Model.glyph(), "opus"), format!("{} opus", Icon::Model.glyph()));
|
||||
assert_eq!(icon_cells(Icon::Model.glyph()), 2);
|
||||
}
|
||||
|
||||
/// The availability probe has to ask about the icons the product actually draws.
|
||||
///
|
||||
/// It asked about one — the cog — and fifty-six of the ninety-odd live in the Material Design
|
||||
/// block Nerd Fonts moved in v3, so a v2 font answered "yes" and then drew boxes for most of
|
||||
/// the dashboard. Asserted over the table rather than over the query string, because the point
|
||||
/// is coverage of the vocabulary and not the spelling of an `fc-list` argument.
|
||||
#[test]
|
||||
fn the_availability_probe_covers_every_nerd_range_the_icons_use() {
|
||||
let probed: std::collections::HashSet<u32> = EVERY_ICON
|
||||
.iter()
|
||||
.filter_map(|icon| icon.at(GlyphTier::Nerd).chars().next())
|
||||
.map(u32::from)
|
||||
.collect();
|
||||
assert!(probed.len() > 80, "the vocabulary should be most of the table: {}", probed.len());
|
||||
|
||||
// One codepoint from each block the table draws from, named so a future icon in a new
|
||||
// range is a visible addition here rather than a silent gap in the probe.
|
||||
let blocks: [(&str, std::ops::RangeInclusive<u32>); 4] = [
|
||||
("Material Design (moved in Nerd Fonts v3)", 0xF0001..=0xF1AF0),
|
||||
("Devicons", 0xE700..=0xE8EF),
|
||||
("Font Awesome", 0xF000..=0xF2FF),
|
||||
("Octicons", 0xF400..=0xF533),
|
||||
];
|
||||
for (name, range) in blocks {
|
||||
assert!(
|
||||
probed.iter().any(|cp| range.contains(cp)),
|
||||
"nothing from {name} reaches the probe, so a font missing that block passes it"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// `auto` may not choose the wide tier: it exists for a question the machine cannot answer, so
|
||||
/// choosing it would be a guess presented as a measurement.
|
||||
#[test]
|
||||
fn only_an_explicit_preference_reaches_the_wide_tier() {
|
||||
assert_eq!(GlyphTier::resolve(GlyphStyle::NerdWide), GlyphTier::NerdWide);
|
||||
for preference in [GlyphStyle::Auto, GlyphStyle::Unknown] {
|
||||
assert_ne!(GlyphTier::resolve(preference), GlyphTier::NerdWide, "{preference:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Icons that have always drawn the same nerd glyph, and may keep doing so.
|
||||
///
|
||||
/// Most share a *metaphor* and never appear in the same surface: a document for "new session"
|
||||
/// and the `@`-mention file menu, a console line for the shell and for the commands you type at
|
||||
/// one, find-by-name for the session finder and `glob`, a tick for a review that was accepted
|
||||
/// and an answer that was given.
|
||||
///
|
||||
/// `±` earns its place the other way round. The bar's changes module and an `edit` card are on
|
||||
/// screen together and *should* look alike — both are a diff, at two scales, and a reader who
|
||||
/// learns the mark on one has learned it on the other.
|
||||
///
|
||||
/// Declared rather than merged because the meanings stay distinct, and the fallbacks already
|
||||
/// separate them: below the nerd tier a tool card drops its icon altogether while the bar keeps
|
||||
/// its `±`.
|
||||
const SHARED_NERD_SHAPES: &[[Icon; 2]] = &[
|
||||
[Icon::NewSession, Icon::FileMenu],
|
||||
[Icon::ToolBash, Icon::SlashMenu],
|
||||
[Icon::Find, Icon::ToolGlob],
|
||||
[Icon::Finish, Icon::Answer],
|
||||
[Icon::Changes, Icon::ToolEdit],
|
||||
];
|
||||
|
||||
/// At the nerd tier a surface is identified by its silhouette, so two of them sharing one is a bug
|
||||
/// unless it is declared above. The fallbacks deliberately group by family instead — there is no
|
||||
/// honest way to give seventy shapes to a font guaranteed only a couple of dozen — so they are
|
||||
/// exempt, and the label beside the icon tells them apart.
|
||||
#[test]
|
||||
fn the_nerd_tier_gives_every_icon_its_own_shape() {
|
||||
let declared = |a: Icon, b: Icon| {
|
||||
SHARED_NERD_SHAPES.iter().any(|pair| pair.contains(&a) && pair.contains(&b))
|
||||
};
|
||||
let mut seen: HashMap<&str, Icon> = HashMap::new();
|
||||
for icon in EVERY_ICON {
|
||||
if let Some(other) = seen.insert(icon.at(GlyphTier::Nerd), *icon) {
|
||||
assert!(declared(*icon, other), "{icon:?} and {other:?} share a nerd glyph");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Every tool the transcript can card. Inherited from the vocabulary this module absorbed, and
|
||||
/// kept because it is the one place shape-identity is load-bearing: a reader picks a `bash` out of
|
||||
/// a run of `read`s by silhouette, before reading a word.
|
||||
const TOOL_ICONS: &[Icon] = &[
|
||||
Icon::ToolRead,
|
||||
Icon::ToolWrite,
|
||||
Icon::ToolEdit,
|
||||
Icon::ToolBash,
|
||||
Icon::ToolGrep,
|
||||
Icon::ToolGlob,
|
||||
Icon::ToolAstGrep,
|
||||
Icon::ToolGit,
|
||||
Icon::ToolTest,
|
||||
Icon::ToolTodo,
|
||||
Icon::ToolSkill,
|
||||
Icon::ToolWebFetch,
|
||||
Icon::ToolWebSearch,
|
||||
Icon::ToolScreenshot,
|
||||
Icon::ToolLoopControl,
|
||||
Icon::ToolDiagnostics,
|
||||
Icon::ToolNavigation,
|
||||
Icon::ToolSymbolSearch,
|
||||
Icon::ToolModuleReport,
|
||||
Icon::ToolReadSymbol,
|
||||
Icon::ToolImpact,
|
||||
Icon::ToolDebug,
|
||||
Icon::ToolMcp,
|
||||
Icon::ToolActivate,
|
||||
];
|
||||
|
||||
/// Two tools sharing an icon defeats the point of having them: telling calls apart at a glance.
|
||||
#[test]
|
||||
fn no_two_tools_share_a_nerd_icon() {
|
||||
let mut seen: HashMap<&str, Icon> = HashMap::new();
|
||||
for icon in TOOL_ICONS {
|
||||
if let Some(other) = seen.insert(icon.at(GlyphTier::Nerd), *icon) {
|
||||
panic!("{icon:?} and {other:?} share an icon");
|
||||
}
|
||||
}
|
||||
assert_ne!(
|
||||
Icon::ToolGeneric.at(GlyphTier::Nerd),
|
||||
Icon::ToolRead.at(GlyphTier::Nerd),
|
||||
"the generic wrench has to be distinguishable from a real tool's icon"
|
||||
);
|
||||
}
|
||||
|
||||
/// Tool cards carry an icon only where it is a real shape. Approximating twenty-six distinct tools
|
||||
/// with a handful of geometric shapes would say less than the bold name already does, so below the
|
||||
/// nerd tier there is nothing to tell apart — deliberately, and asserted so it stays deliberate.
|
||||
#[test]
|
||||
fn tool_cards_drop_their_icons_rather_than_approximate_them() {
|
||||
for icon in TOOL_ICONS.iter().chain(std::iter::once(&Icon::ToolGeneric)) {
|
||||
assert!(!icon.at(GlyphTier::Nerd).is_empty(), "{icon:?} has no shipped glyph");
|
||||
for tier in [GlyphTier::Unicode, GlyphTier::Ascii] {
|
||||
assert_eq!(icon.at(tier), "", "{icon:?} should be omitted at {tier:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The spinner is the most-redrawn character in the product and the one the braille frames got
|
||||
/// wrong: block elements are synthesized by the terminal, so the animation cannot depend on a
|
||||
/// font having anything. The ascii tier has to leave the range altogether.
|
||||
#[test]
|
||||
fn the_spinner_is_drawn_from_the_range_terminals_synthesize() {
|
||||
for at in [GlyphTier::Nerd, GlyphTier::NerdWide, GlyphTier::Unicode] {
|
||||
set_tier(at);
|
||||
for frame in 0..SPINNER_BLOCKS.len() {
|
||||
let glyph = spinner(frame);
|
||||
let cp = u32::from(glyph.chars().next().expect("a frame is one glyph"));
|
||||
assert!(
|
||||
(0x2580..=0x259F).contains(&cp),
|
||||
"{at:?} frame {frame} draws U+{cp:04X}, outside Block Elements"
|
||||
);
|
||||
assert_eq!(UnicodeWidthStr::width(glyph), 1, "frame {frame} is not one cell");
|
||||
}
|
||||
}
|
||||
set_tier(GlyphTier::Ascii);
|
||||
for frame in 0..SPINNER_ASCII.len() {
|
||||
assert!(spinner(frame).is_ascii(), "the ascii tier has to survive a non-UTF-8 terminal");
|
||||
}
|
||||
set_tier(GlyphTier::Nerd);
|
||||
}
|
||||
|
||||
/// A spinner that stops on the same glyph twice reads as a freeze, and the frame index is a
|
||||
/// free-running counter, so it has to wrap rather than saturate.
|
||||
#[test]
|
||||
fn the_spinner_cycles_without_repeating_a_frame() {
|
||||
let ticks = spinner_ticks_per_frame();
|
||||
let cycle: Vec<&str> = (0..SPINNER_BLOCKS.len()).map(|step| spinner(step * ticks)).collect();
|
||||
assert_eq!(
|
||||
cycle.iter().collect::<std::collections::HashSet<_>>().len(),
|
||||
cycle.len(),
|
||||
"every frame of the sweep is a different glyph: {cycle:?}"
|
||||
);
|
||||
let revolution = SPINNER_BLOCKS.len() * ticks;
|
||||
assert_eq!(spinner(revolution), spinner(0), "the sweep wraps");
|
||||
assert_eq!(spinner(usize::MAX), SPINNER_BLOCKS[(usize::MAX / ticks) % SPINNER_BLOCKS.len()]);
|
||||
}
|
||||
|
||||
/// The braille sweep is the preferred drawing where a font on the machine can draw it, and the
|
||||
/// blocks are the floor. Both are one cell; neither may leak into the `ascii` tier, which means
|
||||
/// "assume nothing beyond ASCII" and cannot be rescued by a fallback font.
|
||||
#[test]
|
||||
fn the_sweep_upgrades_to_braille_and_never_past_the_ascii_tier() {
|
||||
let frame = |step: usize| spinner(step * spinner_ticks_per_frame());
|
||||
|
||||
set_braille(false);
|
||||
assert_eq!(frame(0), SPINNER_BLOCKS[0], "the floor asks nothing of any font");
|
||||
|
||||
set_braille(true);
|
||||
assert_eq!(frame(0), SPINNER_BRAILLE[0]);
|
||||
for ticks in 0..SPINNER_BRAILLE.len() {
|
||||
let glyph = frame(ticks);
|
||||
let cp = u32::from(glyph.chars().next().expect("a frame is one glyph"));
|
||||
assert!((0x2800..=0x28FF).contains(&cp), "{glyph:?} is U+{cp:04X}, not braille");
|
||||
assert_eq!(UnicodeWidthStr::width(glyph), 1, "{glyph:?} is not one cell");
|
||||
}
|
||||
assert_eq!(frame(SPINNER_BRAILLE.len()), SPINNER_BRAILLE[0], "the sweep wraps");
|
||||
|
||||
// The ascii tier outranks the probe: a terminal that is not speaking UTF-8 gets ASCII
|
||||
// however many braille fonts are installed.
|
||||
set_tier(GlyphTier::Ascii);
|
||||
for ticks in 0..SPINNER_ASCII.len() {
|
||||
assert!(frame(ticks).is_ascii(), "{:?} is not ascii", frame(ticks));
|
||||
}
|
||||
set_tier(GlyphTier::Nerd);
|
||||
set_braille(false);
|
||||
}
|
||||
|
||||
/// The default has to be the drawing that needs nothing, or a test, a fixture and a generated
|
||||
/// site asset stop agreeing across machines — and the one that disagrees is whichever box last
|
||||
/// installed a symbols font.
|
||||
#[test]
|
||||
fn the_sweep_defaults_to_the_floor() {
|
||||
assert!(!braille(), "braille is opt-in, and only the startup probe opts in");
|
||||
}
|
||||
|
||||
/// Rotation is read from a shape that moves, not one that changes. Every frame has to ink the
|
||||
/// same fraction of the cell, or the sweep reads as a flicker between unrelated glyphs — which
|
||||
/// is what the eight-frame version alternating quarters with halves actually did.
|
||||
#[test]
|
||||
fn every_frame_of_the_sweep_has_the_same_mass() {
|
||||
for frame in SPINNER_BLOCKS {
|
||||
let cp = u32::from(frame.chars().next().expect("a frame is one glyph"));
|
||||
assert!(
|
||||
(0x2596..=0x259F).contains(&cp),
|
||||
"{frame:?} is U+{cp:04X} — not a quadrant, so it does not weigh what its \
|
||||
neighbors weigh"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The **block** sweep steps on its own clock rather than the render loop's, or a 30fps tick
|
||||
/// spins four quarters about four times a second and the reader sees flicker instead of
|
||||
/// motion. The braille is the case this does not apply to: ten frames of one or two dots read
|
||||
/// as a shimmer at the same rate, which is why the two numbers are two numbers.
|
||||
#[test]
|
||||
fn the_block_sweep_is_slower_than_the_render_tick() {
|
||||
const { assert!(SPINNER_BLOCK_TICKS > 1, "the blocks would step at the render rate") };
|
||||
set_braille(false);
|
||||
let ticks = spinner_ticks_per_frame();
|
||||
for tick in 0..ticks {
|
||||
assert_eq!(spinner(tick), spinner(0), "tick {tick} should hold the first frame");
|
||||
}
|
||||
assert_ne!(spinner(ticks), spinner(0), "and then move on");
|
||||
}
|
||||
|
||||
/// The two sweeps keep their own cadences, and the braille's is the faster of them.
|
||||
///
|
||||
/// One number served both for a release, which is how the ten-frame braille sweep came to be
|
||||
/// held at the four-frame blocks' 100ms: slow enough to read as a stutter rather than a turn.
|
||||
/// The set is drawn for 80ms a frame, and 30fps quantizes that to 67 or 100 — so this is the
|
||||
/// nearer one, and the assertion is the relationship rather than the literals.
|
||||
#[test]
|
||||
fn each_sweep_keeps_the_cadence_it_was_drawn_for() {
|
||||
set_braille(false);
|
||||
let blocks = spinner_ticks_per_frame();
|
||||
set_braille(true);
|
||||
let braille = spinner_ticks_per_frame();
|
||||
set_braille(false);
|
||||
|
||||
assert!(
|
||||
braille < blocks,
|
||||
"ten frames of one or two dots survive a rate four quarter blocks cannot: \
|
||||
{braille} vs {blocks} ticks"
|
||||
);
|
||||
// A revolution a reader waits on is not a spinner. Both stay well inside a second at 30fps.
|
||||
let revolution = |ticks: usize, frames: usize| ticks * frames * 1000 / 30;
|
||||
assert!(revolution(braille, SPINNER_BRAILLE.len()) <= 400, "the braille sweep drags");
|
||||
assert!(revolution(blocks, SPINNER_BLOCKS.len()) <= 500, "the block sweep drags");
|
||||
}
|
||||
|
||||
/// The process tier is what the ~150 render sites read, so moving it has to move them.
|
||||
#[test]
|
||||
fn the_process_tier_selects_the_vocabulary() {
|
||||
assert_eq!(tier(), GlyphTier::Nerd, "the default is the look Luxe ships with");
|
||||
for at in TIERS {
|
||||
set_tier(at);
|
||||
assert_eq!(tier(), at, "{at:?} does not survive the round trip");
|
||||
assert_eq!(Icon::ToolBash.glyph(), Icon::ToolBash.at(at));
|
||||
assert_eq!(icon_gap(), at.gap());
|
||||
}
|
||||
set_tier(GlyphTier::Nerd);
|
||||
assert_eq!(Icon::ToolBash.glyph(), Icon::ToolBash.at(GlyphTier::Nerd));
|
||||
}
|
||||
Reference in New Issue
Block a user