The probe measured the distance from a cell nobody moved

`symbol_cells` asked the console one question — what does a box-drawing bar
cost — and spent the answer on sixty characters. That was already the wrong
shape of answer: what decides a price is the code page and the font the console
was left on, and nothing promises the box-drawing set and the block-shading set
land on the same side of it. But the measurement was worse than the shape, and
this is the part worth writing down.

To price a character the probe asked the console to move the cursor to an
anchor, wrote the character, read the cursor back, and computed `after.X -
at.X`: the distance from the anchor it had *requested*. On the console this was
chased on, `SetConsoleCursorPosition` returns success without moving anything.
Every read therefore returned the same cursor, and the "cost" of the nth
character was n. The temporary trace said it plainly: 1, 2, 3, and on up to 60.
`match cost { 0 => None, 1 => Single, _ => Double }` then read every character
from the third onwards as two cells wide, which is how a UTF-8 console that
charges one cell for the lot ended up with its rounded card downgraded to
ASCII, and how the earlier "this console prices bars one cell and blocks two"
found its evidence. That split was never measured. It was a counter.

The distance between two reads is the only number here that means what it looks
like, so that is what is read now: ask where the cursor is, write, ask again,
and the step is the price. A step that cannot be a price is no answer at all —
`price_from_step` accepts one cell and two cells and refuses zero, negatives,
three, and sixty, so a cursor that never moved produces a code page's guess
rather than a layout believing a number with no cells behind it. A read that
changed rows is no answer either, and neither is a row with fewer than two
columns left to answer in, and `SetConsoleCursorPosition` is still called but
its return value is deliberately ignored: a ruler that is obeyed asks all sixty
questions of the same cell, and a ruler that is ignored walks one row, which the
characters get wiped out of either way.

Because a ruler can lie in ways that still produce plausible numbers, `prices`
asks for `A` first. `A` costs one cell in every table that measures characters,
so an answer of anything else means the probe is not reading cells, and it says
"symbols unreadable: A does not cost one cell" and takes the code page's answer
instead of laying a screen out against the lie.

The answers are kept as a price list, not a verdict, and `squid-tui` asks it
only about the characters it means to draw (`witnesses`), so a console that
overcharges the shading but not the rules keeps its rounded card and gives up
only the gauges inside it. The module documentation no longer claims a split
price was measured on cp936: what the ConPTY measured there, as on 65001, is one
cell for all sixty. What remains from the real window is the symptom — a card
that fit and a gauge inside it that did not — which is the reason for the list,
not evidence about that box.

Tests: the fair-priced-and-dishonestly-priced console still downgrades only the
disputed set, and `a_step_that_cannot_be_a_price_is_not_an_answer` keeps sixty
from ever being a price again.
main
mula.liu 2026-10-15 22:28:35 +08:00
parent d2e28d8ab2
commit 345f8391ed
2 changed files with 649 additions and 107 deletions

View File

@ -23,9 +23,20 @@
//! lying any more: it is simply too small for what was written into it, every
//! row overflows by the characters it paid one cell for, and the overflow lands
//! at the start of the row below, which is the same pile of previous frames a
//! wrong size produces. [`symbol_cells`] asks the cells what they cost instead
//! of guessing from the locale, so the interface can afford its glyphs where it
//! can and pay for them in ASCII where it cannot.
//! wrong size produces.
//!
//! [`prices`] asks the cells what they cost instead of guessing from the
//! locale, and it asks *character by character*. The rules, the shading and the
//! punctuation an interface is drawn from are priced one at a time by whatever
//! code page and font the console was left on, and nothing promises they will
//! all land on the same answer: the first Chinese window this was chased through
//! had room for the rounded cards and no room for the gauges inside them, which
//! is one console pricing the two sets differently. One question for the whole
//! screen is one answer for the wrong character, and the frame overflows by
//! whichever set the answer was taken from. So the answer is a price list, and
//! the interface pays ASCII only for the characters this console actually
//! overcharges, keeping the rounded card it can afford and losing only the
//! block that it cannot.
//!
//! A second question has to be answered before the first one means anything:
//! *which* buffer is being measured. The alternate screen on a legacy console is
@ -228,31 +239,180 @@ pub enum SymbolCells {
Double,
}
/// Ask the console, once, what it charges for a one-column character.
/// What a console charges for the characters whose price is not written down.
///
/// The answer is measured rather than looked up, because the two things that
/// decide it are both set outside this program: the code page the console was
/// Held as a list rather than as one verdict, because the characters an
/// interface is drawn from do not all belong to one class: the box-drawing set
/// and the block-shading set are priced separately, by a code page and a font
/// together, and nothing binds the two to the same answer. A single verdict can
/// only be right for one of them, and whichever it gets wrong is the row that
/// overflows — which is also why the list is measured again for every console
/// rather than remembered from the last one, since the same box answers one cell
/// for the lot once it is moved to UTF-8.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Prices {
/// Where the answers came from, so a log says so.
source: &'static str,
/// The price of every character that was asked, and no others.
measured: Vec<(char, SymbolCells)>,
/// The price of a character nobody asked about. This is the pessimistic
/// answer on a code page that reprices things, which is the only safe one:
/// a character this program forgot to ask about is a character it should
/// not have drawn without asking.
assumed: SymbolCells,
}
impl Prices {
/// The answer a person named, or a test asked for: one price for every
/// character, asked or not.
pub fn flat(cells: SymbolCells) -> Prices {
Prices {
source: "asked",
measured: Vec::new(),
assumed: cells,
}
}
/// The answer for a screen that cannot be asked, which is the one a code
/// page implies. See [`charged_by_code_page`] for what that answer is worth.
pub fn implied(code_page: u32) -> Prices {
Prices {
source: "implied",
measured: Vec::new(),
assumed: charged_by_code_page(code_page),
}
}
/// What the cells said, character by character, with the price a code page
/// implies standing behind the ones they could not read.
pub fn measured(measured: Vec<(char, SymbolCells)>, assumed: SymbolCells) -> Prices {
let source = if measured.is_empty() {
// Not one character landed where it could be read back, so there is
// nothing here but the guess, and the log should say so.
"implied"
} else {
"measured"
};
Prices {
source,
measured,
assumed,
}
}
/// What this console charges for one character.
pub fn price(&self, ch: char) -> SymbolCells {
self.measured
.iter()
.find(|(asked, _)| *asked == ch)
.map(|(_, cells)| *cells)
.unwrap_or(self.assumed)
}
/// Whether anything on this screen costs more than it measures, which is the
/// one question a frame can ask before it walks itself.
pub fn overcharged(&self) -> bool {
self.assumed == SymbolCells::Double
|| self
.measured
.iter()
.any(|(_, cells)| *cells == SymbolCells::Double)
}
/// One line for the trace: where the answer came from, what an unasked
/// character is assumed to cost, and which characters cost two. The last of
/// those is the whole reason the line exists — a person working out why a
/// gauge came out as `#` needs to be told what the glass charged for `░`.
pub fn text(&self) -> String {
let double: String = self
.measured
.iter()
.filter(|(_, cells)| *cells == SymbolCells::Double)
.map(|(ch, _)| *ch)
.collect();
format!(
"{} of {} characters, unasked={}, two cells={}",
self.source,
self.measured.len(),
word(self.assumed),
if double.is_empty() {
"none".to_string()
} else {
double
}
)
}
}
/// The word for a price, because `{:?}` of an enum variant is a Rust name and a
/// log is read by a person.
fn word(cells: SymbolCells) -> &'static str {
match cells {
SymbolCells::Single => "one",
SymbolCells::Double => "two",
}
}
/// The price a cursor's step describes, or no price at all.
///
/// One cell and two cells are the only answers a character can give. Anything
/// else is the console saying something different: nothing at all is a write
/// that did not land, a step backwards is a cursor that was moved by somebody
/// else, and a step past two is a reading that has lost the character it was
/// measuring. Sixty looked like a price once, and was not one — it was the
/// distance from a cell the console had never agreed to move the cursor to, read
/// off the sixtieth question in a row, and it is the reason this function
/// exists: an answer that cannot be a price has to become no answer, so the code
/// page answers instead of the layout believing a number with no cells behind it.
// Read by the probe on Windows and by a test anywhere; a non-Windows library
// build has no console to read a step off, so nothing here calls it.
#[cfg_attr(not(windows), allow(dead_code))]
fn price_from_step(step: i32) -> Option<SymbolCells> {
match step {
1 => Some(SymbolCells::Single),
2 => Some(SymbolCells::Double),
_ => None,
}
}
/// Ask the console, once per character, what it charges for a one-column
/// character.
///
/// The answers are measured rather than looked up, because the two things that
/// decide them are both set outside this program: the code page the console was
/// left on and the font it was left with. They are not even the same question,
/// since `chcp 65001` moves a box-drawing bar back to one cell on the box that
/// charges two, and the same console shows that bar as a blank when the font
/// carries no glyph for it. The only answer worth laying a screen out against
/// is the one taken from the cells themselves.
/// carries no glyph for it. The only answers worth laying a screen out against
/// are the ones taken from the cells themselves.
///
/// Measuring spends one character on whatever screen the console is showing, so
/// The caller names the characters worth asking about, which are the ones it
/// means to draw. The price of one of them is not evidence about another: the
/// list is as long as the question because a frame laid out against a character
/// it never asked about is laid out against a guess, and it is the guess that the
/// overflow costs.
///
/// Measuring spends characters on whatever screen the console is showing, so
/// call this when the interface owns the screen rather than while somebody's
/// shell is still on it.
pub fn symbol_cells() -> SymbolCells {
/// shell is still on it. Each character is written, read, and taken back out
/// again, and the first frame covers the row that is left.
pub fn prices(witnesses: &[char]) -> Prices {
if let Some(asked) = asked_for() {
note(&format!("symbols={asked:?} asked"));
return asked;
note(&format!("symbols={} asked", word(asked)));
return Prices::flat(asked);
}
#[cfg(windows)]
{
windows::symbol_cells()
windows::prices(witnesses)
}
#[cfg(not(windows))]
{
SymbolCells::Single
// No console to ask. A terminal on a writing system that charges two
// for these characters also charges two to the layout that measures
// them, because both read the same tables, so there is nothing to buy
// back and the frame keeps the glyphs it was drawn with.
let _ = witnesses;
Prices::flat(SymbolCells::Single)
}
}
@ -367,10 +527,11 @@ fn append(path: &Path, line: &str) {
#[cfg(windows)]
mod windows {
use super::{charged_by_code_page, Geometry, SymbolCells};
use super::{charged_by_code_page, price_from_step, Geometry, Prices, SymbolCells};
use std::collections::HashMap;
use std::ffi::c_void;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::OnceLock;
use std::sync::{Mutex, OnceLock};
use windows_sys::Win32::Foundation::{
CloseHandle, GENERIC_READ, GENERIC_WRITE, INVALID_HANDLE_VALUE,
};
@ -378,9 +539,10 @@ mod windows {
CreateFileW, GetFileType, FILE_SHARE_READ, FILE_SHARE_WRITE, OPEN_EXISTING,
};
use windows_sys::Win32::System::Console::{
GetConsoleMode, GetConsoleOutputCP, GetConsoleScreenBufferInfo, GetStdHandle,
SetConsoleCursorPosition, SetConsoleScreenBufferSize, WriteConsoleW,
CONSOLE_SCREEN_BUFFER_INFO, COORD, ENABLE_VIRTUAL_TERMINAL_PROCESSING, STD_OUTPUT_HANDLE,
FillConsoleOutputCharacterW, GetConsoleMode, GetConsoleOutputCP,
GetConsoleScreenBufferInfo, GetStdHandle, SetConsoleCursorPosition,
SetConsoleScreenBufferSize, WriteConsoleW, CONSOLE_SCREEN_BUFFER_INFO, COORD,
ENABLE_VIRTUAL_TERMINAL_PROCESSING, STD_OUTPUT_HANDLE,
};
type Handle = *mut c_void;
@ -547,48 +709,123 @@ mod windows {
)
}
/// The character the price is asked with: a box-drawing bar, which measures
/// one column in every table that measures characters and is the glyph this
/// interface draws more of than any other.
const PROBE: char = '│';
/// The price, measured once. See [`super::symbol_cells`] for why it is
/// measured, and why the answer is worth keeping for the life of a run.
static SYMBOLS: OnceLock<SymbolCells> = OnceLock::new();
pub fn symbol_cells() -> SymbolCells {
*SYMBOLS.get_or_init(|| {
measure_symbol_cells()
.unwrap_or_else(|| charged_by_code_page(unsafe { GetConsoleOutputCP() }))
})
/// The price list, read off the cells of the console this process draws on.
///
/// See [`super::prices`] for why the question is asked of every character
/// rather than of one, and why the ones that cannot be answered fall back on
/// the code page rather than on the average of the others.
pub fn prices(witnesses: &[char]) -> Prices {
let code_page = unsafe { GetConsoleOutputCP() };
// Measure the ruler before measuring the screen. `A` is one cell in
// every code page ever written, so a console that charges anything else
// for it is not charging anything: it is refusing to be read, and an
// answer taken from it is a number with no cells behind it. The code
// page answers for the whole screen in that case, which is the guess the
// log admits to rather than a measurement the layout trusts by mistake.
if price_of('A') != Some(SymbolCells::Single) {
super::note("symbols unreadable: A does not cost one cell");
return Prices::implied(code_page);
}
Prices::measured(
witnesses
.iter()
.filter_map(|&ch| Some((ch, price_of(ch)?)))
.collect(),
charged_by_code_page(code_page),
)
}
/// Write one character where the console is looking, and read back the cost.
/// Paint two spaces over a cell and whatever was standing behind it.
///
/// `None` when there is no screen to ask, or no answer to read: the
/// character did not land, or it landed where a second cell would have had
/// to wrap — a place the cursor can be and deliberately not a place this
/// function writes to. A cursor that did not move at all is not an answer
/// either; a console that writes nothing where it was told to write is not
/// one whose prices can be read off it. The caller falls back on the code
/// page.
fn measure_symbol_cells() -> Option<SymbolCells> {
/// Two cells, because the character being taken back out may have cost two,
/// and clearing only the first is the half-clear that leaves a spacer on the
/// glass for the next frame to inherit.
fn blank(owned: Handle, at: COORD) {
let mut written = 0u32;
unsafe {
FillConsoleOutputCharacterW(owned, b' ' as u16, 2, at, &mut written);
}
}
/// What one character costs: measured the first time it is asked about, and
/// remembered afterwards — including the cases where no answer could be
/// read, because a character the console refuses to price is not worth
/// writing to the screen again every beat.
fn price_of(ch: char) -> Option<SymbolCells> {
static PRICES: OnceLock<Mutex<HashMap<char, Option<SymbolCells>>>> = OnceLock::new();
let answers = PRICES.get_or_init(|| Mutex::new(HashMap::new()));
if let Some(known) = answers
.lock()
.ok()
.and_then(|known| known.get(&ch).copied())
{
return known;
}
let measured = measure_symbol_cells(ch);
if let Ok(mut known) = answers.lock() {
known.insert(ch, measured);
}
measured
}
/// Write one character and read its price off the step the cursor took.
///
/// The cursor is asked to sit in the top-left cell of the window first,
/// because that is the safest cell to spend a character in: a wide one in the
/// last cell of the last row has nowhere to put its second cell, and a wrap
/// there scrolls the screen this program is about to draw on. Asking is not
/// getting. Measured on the 18363 box, through the buffer the console is
/// showing, while the alternate screen is up: `SetConsoleCursorPosition`
/// answers that it succeeded and leaves the cursor exactly where it was. The
/// first version of this function anchored its arithmetic to the cell it had
/// *asked* for rather than to the cell the cursor was actually standing on,
/// so what it read off sixty characters was not sixty prices but sixty
/// distances from an anchor nobody moved: one cell for the first character,
/// two for the second, sixty for the sixtieth. A console that was charging
/// one cell for every ambiguous character on code page 65001 therefore
/// reported itself a screenful of two-cell ones, the layout quietly gave up
/// the rounded frames it could afford, and the probe built to catch one
/// overflow produced a different one.
///
/// So the anchor is the cursor *found*, read back after the request, and the
/// price is the *step* between two readings of it — a price that does not
/// care whether the reset was obeyed. Where it was, every character is asked
/// in the same cell; where it was not, the questions walk the top row one
/// step at a time, and [`blank`] takes each one back out the moment it has
/// been answered, so what is left behind is a row of empty cells that the
/// first frame paints over regardless.
///
/// `None` — no answer, fall back on the code page — when there is no screen
/// to ask, when the character did not land, when the cursor did not move,
/// when it ended up anywhere other than one or two columns further along the
/// same row, and when the row has no room left to answer in. A price of zero
/// is deliberately not an answer: a console that writes nothing where it was
/// told to write is not one whose prices can be read off it.
fn measure_symbol_cells(ch: char) -> Option<SymbolCells> {
let owned = shown_handle()?;
let before = info(owned.0)?;
// The top-left cell of what is on the glass, not where the cursor
// happens to be: on the last column of the last row a second cell
// wraps, and a wrap scrolls the very screen this program is about to
// draw on. The cell is painted over by the first frame anyway, which is
// what makes it worth spending one character of somebody's screen to
// keep the rest of the frame from being wrong.
let at = COORD {
X: before.srWindow.Left,
Y: before.srWindow.Top,
};
if unsafe { SetConsoleCursorPosition(owned.0, at) } == 0 {
// Ask, and then find out where the cursor really is. The request is
// worth making even on a console that ignores it, because on the ones
// that obey all sixty questions are asked in one cell instead of in
// sixty of them.
unsafe {
SetConsoleCursorPosition(owned.0, at);
}
let room = info(owned.0)?;
let here = room.dwCursorPosition;
// Leave room for the widest answer this function can read. Walking into
// the last column of the window would spend a character on a guess the
// console cannot confirm, and the cell after the window's edge is where
// a wrap begins.
if i32::from(here.X) > i32::from(room.srWindow.Right) - 2 {
return None;
}
let glyph: Vec<u16> = PROBE.to_string().encode_utf16().collect();
blank(owned.0, here);
let glyph: Vec<u16> = ch.to_string().encode_utf16().collect();
let mut written = 0u32;
let ok = unsafe {
WriteConsoleW(
@ -600,19 +837,22 @@ mod windows {
)
};
let after = info(owned.0);
// Take the character back out of the screen, so the next character is
// asked in an empty cell for the same reason this one was, and so a
// program that is only borrowing somebody's console leaves no debt.
blank(owned.0, here);
unsafe { SetConsoleCursorPosition(owned.0, before.dwCursorPosition) };
if ok == 0 || written != 1 {
return None;
}
// Whatever the cursor moved past is what the character cost. Reading it
// Whatever the cursor stepped past is what the character cost. Reading it
// back from the console instead of counting bytes is the point: the
// console is the thing this program has to agree with.
let cost = after?.dwCursorPosition.X.checked_sub(at.X)?;
match cost {
0 => None,
1 => Some(SymbolCells::Single),
_ => Some(SymbolCells::Double),
let after = after?;
if after.dwCursorPosition.Y != here.Y {
return None;
}
price_from_step(i32::from(after.dwCursorPosition.X) - i32::from(here.X))
}
/// Make the buffer agree with the window, and report whether it did.
@ -664,7 +904,9 @@ mod windows {
#[cfg(test)]
mod tests {
use super::{charged_by_code_page, measurement, spend, Geometry, SymbolCells};
use super::{
charged_by_code_page, measurement, price_from_step, spend, Geometry, Prices, SymbolCells,
};
/// A console with room above the window, so the limit is not what holds
/// this one back.
@ -689,6 +931,28 @@ mod tests {
}
}
#[test]
fn a_step_that_cannot_be_a_price_is_not_an_answer() {
// The two real prices, and the readings that are not prices at all: a
// write that never landed, a cursor that went the wrong way, and a step
// so large it can only be the probe losing the character it was asked
// about. That last one is not hypothetical — this probe once reported
// `cost=60` for a middot, because the cursor had been asked rather than
// told to go home and the sixty questions were therefore measured from
// wherever the fifty-ninth had left it. Every character past the second
// came back "two cells", and a console that charged one for all of them
// was drawn as if it charged two.
assert_eq!(price_from_step(1), Some(SymbolCells::Single));
assert_eq!(price_from_step(2), Some(SymbolCells::Double));
for not_a_price in [0, -1, 3, 60] {
assert_eq!(
price_from_step(not_a_price),
None,
"{not_a_price} is a cursor moving, not a character costing"
);
}
}
#[test]
fn a_far_eastern_code_page_charges_two_cells_for_a_one_column_character() {
// Shift-JIS, GBK, Unified Hangul and Big5: the four consoles this
@ -703,6 +967,58 @@ mod tests {
}
}
#[test]
fn a_console_can_price_the_rules_fairly_and_the_shading_dishonestly() {
// The reason a price is a list and not a verdict: the two sets a frame is
// drawn from can land on different answers, and a frame that treats them
// as one question overflows by whichever set its single answer was taken
// from. This is the unlucky shape of it — rules affordable, shading not —
// which is what the first Chinese window looked like, rounded cards that
// fitted and gauges that did not. Whether any particular console charges
// this way is measured rather than assumed here; the list exists so the
// layout never has to decide which half of the screen it is spending.
let prices = Prices::measured(
vec![
('│', SymbolCells::Single),
('─', SymbolCells::Single),
('█', SymbolCells::Double),
('░', SymbolCells::Double),
],
SymbolCells::Single,
);
assert_eq!(prices.price('│'), SymbolCells::Single);
assert_eq!(prices.price('█'), SymbolCells::Double);
assert!(prices.overcharged());
let line = prices.text();
assert!(
line.contains("█░"),
"{line} should name the glyphs at fault"
);
assert!(
line.contains("measured"),
"{line} should say where it came from"
);
}
#[test]
fn a_character_nobody_asked_about_costs_what_the_code_page_charges() {
// Forgetting to ask is not forgetting to pay. The fallback is the
// pessimistic answer, so a glyph added to a card without its price being
// asked comes out as ASCII rather than as a row off the edge of a
// Chinese console.
let chinese = Prices::implied(936);
assert_eq!(chinese.price('█'), SymbolCells::Double);
assert!(chinese.overcharged());
assert!(chinese.text().starts_with("implied"));
let utf8 = Prices::implied(65001);
assert_eq!(utf8.price('█'), SymbolCells::Single);
assert!(!utf8.overcharged());
let asked = Prices::flat(SymbolCells::Double);
assert_eq!(asked.price('a'), SymbolCells::Double);
}
#[test]
fn a_named_price_is_obeyed_and_a_rubbish_one_is_ignored() {
// The override is the only way to lay out for a screen this program is

View File

@ -24,6 +24,19 @@
//! gauges `#`, the arrows `<^>v`. Nothing about the layout moves, because the
//! exchange is one column for one column: what changes is that the rows fit.
//!
//! The exchange is decided character by character, which is the part that took
//! the longest to get right. Rules, shading and punctuation are priced one at a
//! time by the code page and font a console was left on, nothing binds the three
//! to the same answer, and a layout that asks about one and pays for all three is
//! one character deep — the first Chinese window this was chased through had room
//! for the rounded cards and none for the `░` inside them. So the price of every
//! glyph this interface draws is asked of the console
//! — [`squid_platform::console::prices`] takes the whole list — and the
//! rounded card a Chinese console can afford stays rounded while only the
//! blocks that it cannot are paid for in ASCII. A character that was never
//! asked about is charged the pessimistic price, because a glyph that escaped
//! the list is a glyph nobody checked.
//!
//! Chinese text is left alone, and so are the letters of most alphabets —
//! neither is ambiguous, so both cost the console exactly what they cost the
//! layout. That is deliberate. A person's paths and process names are not what
@ -31,39 +44,69 @@
//! it can prove the console is overcharging.
use ratatui::buffer::Buffer;
use squid_platform::console::SymbolCells;
use ratatui::symbols;
use squid_platform::console::{Prices, SymbolCells};
use unicode_width::UnicodeWidthChar;
/// The console's price list, taken once, from the console itself.
/// What the console charges for the characters this interface is drawn from.
pub struct Fit {
/// True when a character the layout bought for one cell costs two here.
overcharging: bool,
/// The price list, kept so a beat can say what the glass charged and so a
/// test can ask what one glyph cost.
prices: Prices,
/// What to draw in place of each character, indexed by code point, with
/// `None` for the ones this console can afford. A frame is tens of
/// thousands of cells and a beat is a quarter of a second, so the answer is
/// looked up rather than worked out — and the table holds the replacement
/// rather than a bit saying *this one is wrong*, because the character is
/// what a cell needs and asking the price of a glyph is the only way to
/// know that a rounded corner may stay rounded while a block of shading
/// may not.
exchange: Vec<Option<char>>,
}
impl Fit {
/// Ask what the screen being drawn on charges for a one-column character.
/// Ask the console what it charges for every character this interface draws.
///
/// [`squid_platform::console::symbol_cells`] measures the answer by writing
/// one character and reading back what it cost, so the Chinese Windows
/// console that cannot afford a box-drawing bar gets the ASCII chrome it
/// can, while Windows Terminal, a console on UTF-8 and a Mac keep the
/// rounded cards they have always paid for.
/// [`squid_platform::console::prices`] measures the answers by writing each
/// character and reading back what it cost, so the Chinese Windows console
/// that cannot afford a block of shading draws its gauges in `#` while
/// keeping the rounded cards it can afford, and Windows Terminal, a console
/// on UTF-8 and a Mac keep everything they were drawn with.
///
/// Measuring spends characters on whatever the console is showing, which is
/// why this is called once the alternate screen is up and not at startup.
pub fn measure() -> Fit {
let cells = squid_platform::console::symbol_cells();
squid_platform::console::note(&format!("symbols={cells:?}"));
Fit {
overcharging: cells == SymbolCells::Double,
}
Fit::priced(squid_platform::console::prices(&witnesses()))
}
/// A console whose answers are already known: the ones a person named in the
/// environment, and the ones a test wants to pretend.
fn priced(prices: Prices) -> Fit {
let exchange = (0..=PRICED_UPTO)
.map(|code| {
char::from_u32(code)
// Two questions have to be answered before a cell is
// rewritten: is this a character this module owns at all
// (not Chinese, not somebody's path), and does *this*
// console actually charge two cells for it. The first is a
// rule, the second is a measurement, and a glyph that fails
// either one keeps what it was drawn with.
.filter(|&ch| prices.price(ch) == SymbolCells::Double)
.and_then(cheaper)
})
.collect();
squid_platform::console::note(&format!("symbols {}", prices.text()));
Fit { prices, exchange }
}
/// Rewrite a rendered frame so that every cell costs what the layout paid.
///
/// Called once the widgets are done and before the frame reaches the
/// terminal, which is the last moment at which an over-long row can still
/// be made to fit. On a console that charges one cell there is nothing to
/// do, and this returns without touching the frame.
/// be made to fit. On a console that charges one cell for everything there
/// is nothing to do, and this returns without touching the frame.
pub fn carry(&self, buffer: &mut Buffer) {
if !self.overcharging {
if !self.prices.overcharged() {
return;
}
for y in 0..buffer.area.height {
@ -76,7 +119,10 @@ impl Fit {
let Some(first) = cell.symbol().chars().next() else {
continue;
};
if let Some(instead) = cheaper(first) {
// A character above the table is one no console reprices: that
// is where Chinese lives, and both sides already agree that it
// costs two cells, so it has nothing here to buy back.
if let Some(instead) = self.exchange.get(first as usize).copied().flatten() {
cell.set_char(instead);
}
}
@ -84,12 +130,132 @@ impl Fit {
}
}
/// The character to draw instead, or `None` when this one is fairly priced.
fn cheaper(ch: char) -> Option<char> {
overcharged(ch).then(|| one_cell(ch))
/// The highest code point the price of is worth a slot in the table, which is
/// one past the last range a Far-Eastern console was ever seen repricing.
const PRICED_UPTO: u32 = 0x2BFF;
/// The three files every screen of this interface is drawn out of.
const FACES: &[&str] = &[
include_str!("app.rs"),
include_str!("theme.rs"),
include_str!("views.rs"),
];
/// The characters the widgets draw on our behalf, taken from the library that
/// draws them rather than from what this crate happens to mention: the rules of
/// a card, the fill and the empty of a gauge, the bars of a sparkline. A widget
/// is not a literal anybody can grep for, so it is asked for by name.
fn drawn_by_widgets() -> Vec<String> {
use symbols::border::Set;
let mut drawn: Vec<String> = Vec::new();
let shading = [
symbols::shade::EMPTY,
symbols::shade::LIGHT,
symbols::shade::MEDIUM,
symbols::shade::DARK,
symbols::shade::FULL,
symbols::DOT,
];
drawn.extend(shading.map(str::to_string));
for level in [
symbols::half_block::UPPER,
symbols::half_block::LOWER,
symbols::half_block::FULL,
] {
drawn.push(level.to_string());
}
// A gauge is filled with the left blocks and a sparkline drawn with the
// bottom ones, both in eight levels, and either could be the character this
// console charges two cells for.
let bars = [
symbols::bar::FULL,
symbols::bar::SEVEN_EIGHTHS,
symbols::bar::THREE_QUARTERS,
symbols::bar::FIVE_EIGHTHS,
symbols::bar::HALF,
symbols::bar::THREE_EIGHTHS,
symbols::bar::ONE_QUARTER,
symbols::bar::ONE_EIGHTH,
symbols::block::FULL,
symbols::block::SEVEN_EIGHTHS,
symbols::block::THREE_QUARTERS,
symbols::block::FIVE_EIGHTHS,
symbols::block::HALF,
symbols::block::THREE_EIGHTHS,
symbols::block::ONE_QUARTER,
symbols::block::ONE_EIGHTH,
];
drawn.extend(bars.map(str::to_string));
for Set {
top_left,
top_right,
bottom_left,
bottom_right,
vertical_left,
vertical_right,
horizontal_top,
horizontal_bottom,
} in [
symbols::border::PLAIN,
symbols::border::ROUNDED,
symbols::border::DOUBLE,
symbols::border::THICK,
] {
drawn.extend(
[
top_left,
top_right,
bottom_left,
bottom_right,
vertical_left,
vertical_right,
horizontal_top,
horizontal_bottom,
]
.map(str::to_string),
);
}
drawn
}
/// True when the console charges two cells for what the layout measured as one.
/// Every character whose price has to be asked rather than looked up: the ones
/// this interface draws itself, and the ones its widgets draw for it.
///
/// This is the list that used to be one character long. It is derived from the
/// program — the source of the screens, plus the widgets — so that a glyph put
/// into a card is a glyph whose price gets asked without anybody remembering to
/// update a table, which is exactly the thing that went wrong and looked
/// perfectly fine on every terminal but one.
pub fn witnesses() -> Vec<char> {
let mut asked: Vec<char> = Vec::new();
let widgets = drawn_by_widgets();
let drawn = FACES.iter().map(|face| face.to_string());
for face in drawn.chain(widgets) {
for ch in face.chars() {
if in_dispute(ch) && !asked.contains(&ch) {
asked.push(ch);
}
}
}
asked
}
/// The character to draw instead, or `None` when this one is fairly priced.
fn cheaper(ch: char) -> Option<char> {
in_dispute(ch).then(|| one_cell(ch))
}
/// True when this is a character a console is free to charge two cells for.
///
/// In dispute is not the same as overcharged: whether *this* console does it is
/// measured, per character, by [`Fit::measure`]. What this answers is which
/// characters are worth asking about, and which ones the frame is allowed to
/// rewrite once the console has said.
///
/// The first of the two tests is the honest one: `width` is what the layout paid
/// and `width_cjk` is what the Ambiguous class says a Far-Eastern terminal
@ -97,15 +263,18 @@ fn cheaper(ch: char) -> Option<char> {
/// whatever else the console may be getting wrong.
///
/// The second test exists because that class is a description of what terminals
/// have been seen to do rather than a rule, and a console on code page 936 has
/// been seen doing it to characters the class leaves out. `░` is not Ambiguous at
/// all, and it is what this interface fills its gauges with: a row of ten of them
/// is twenty cells on that glass. Being too generous here buys a plainer
/// character on the console that cannot afford the drawn one; being too strict
/// buys the shifted frame this module exists to prevent. So the blocks a
/// Far-Eastern writing system was laid out over are asked as well as the class,
/// and the letters of other alphabets keep their one cell.
fn overcharged(ch: char) -> bool {
/// have been seen to do rather than a rule, and a Chinese console charges it to
/// characters the class leaves out. `░` is not Ambiguous at all, and it is what
/// this interface fills its gauges with — and it is the character the single probe
/// never asked about, which is how a frame whose rules were measured fair still
/// overflowed. Anything this list declines to ask about is a character the console
/// prices without being asked, so it errs towards the questions.
/// Being too generous here buys a plainer character on the console that cannot
/// afford the drawn one; being too strict buys the shifted frame this module
/// exists to prevent. So the blocks a Far-Eastern writing system was laid out
/// over are asked as well as the class, and the letters of other alphabets keep
/// their one cell.
fn in_dispute(ch: char) -> bool {
ch.width() == Some(1) && (ch.width_cjk() == Some(2) || laid_out_by_far_east(ch))
}
@ -213,18 +382,42 @@ fn one_cell(ch: char) -> char {
#[cfg(test)]
mod tests {
use super::{cheaper, one_cell, overcharged, Fit};
use super::{cheaper, in_dispute, one_cell, witnesses, Fit, FACES};
use ratatui::{
buffer::Buffer,
layout::Rect,
style::Style,
widgets::{Block, BorderType, Borders, Widget},
};
use squid_platform::console::{Prices, SymbolCells};
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
/// A narrow frame, spelled out so the tests read as rows of a screen.
/// A console that charges two cells for everything the layout measured as
/// one, which is the worst answer the probe can bring back.
fn cramped() -> Fit {
Fit { overcharging: true }
Fit::priced(Prices::flat(SymbolCells::Double))
}
/// A console that charges one cell for everything, so nothing here has a
/// reason to touch the frame.
fn fairly_priced() -> Fit {
Fit::priced(Prices::flat(SymbolCells::Single))
}
/// The console this module was written for and still got wrong: the rules
/// cost one cell, the shading costs two. Whether a console really charges this
/// way is measured, never assumed — this is the *shape* of the unlucky answer,
/// so the exchange is tested against the two sets having different prices,
/// which is the one thing a single price can never express.
fn rules_affordable_gauges_not() -> Fit {
let mut asked = Vec::new();
for ch in "░▒▓█".chars() {
asked.push((ch, SymbolCells::Double));
}
for ch in "│─╭·…↑✓".chars() {
asked.push((ch, SymbolCells::Single));
}
Fit::priced(Prices::measured(asked, SymbolCells::Single))
}
/// What a row of the buffer reads as. The cell behind a wide character is
@ -257,7 +450,7 @@ mod tests {
'─', '│', '╭', '╮', '╯', '╰', '·', '…', '—', '↑', '↓', '✓', '×', '█', '▒', '▾', '①',
'●', '“', '░', '►', '⊘', '⇄',
] {
assert!(overcharged(ch), "{ch} should be in dispute");
assert!(in_dispute(ch), "{ch} should be in dispute");
assert!(
one_cell(ch).is_ascii(),
"{ch} is in dispute and must be exchanged, not argued about"
@ -315,10 +508,7 @@ mod tests {
fn a_frame_is_left_alone_on_a_console_that_charges_one() {
let mut buffer = Buffer::empty(Rect::new(0, 0, 9, 1));
buffer.set_string(0, 0, "╭─╮ · ░▒█", Style::default());
Fit {
overcharging: false,
}
.carry(&mut buffer);
fairly_priced().carry(&mut buffer);
assert_eq!(read(&buffer, 0), "╭─╮ · ░▒█");
}
@ -333,14 +523,6 @@ mod tests {
assert_eq!(read(&buffer, 0), "回收站/");
}
/// The source of every screen this crate draws, so the set of glyphs is the
/// program's and not a list someone keeps up to date by hand.
const FACES: &[&str] = &[
include_str!("app.rs"),
include_str!("theme.rs"),
include_str!("views.rs"),
];
#[test]
fn every_glyph_this_interface_draws_has_a_price() {
// What this test stands in for is the mistake that spent a week: a glyph
@ -360,7 +542,7 @@ mod tests {
};
assert!(
instead.is_ascii() && instead != '?',
"`{ch}` is drawn by this interface, so the console must not be left guessing what it was meant to be"
"`{ch}` is drawn here, so the console must not guess what it meant"
);
}
}
@ -370,6 +552,50 @@ mod tests {
);
}
#[test]
fn a_console_that_reprices_the_gauges_keeps_the_rules_it_priced_fairly() {
// The frame this module existed for two rounds and still got wrong. One
// probe asked the price of a box-drawing rule, was answered one cell, and
// that became the price of every glyph on the screen — so the rounded card
// stayed rounded, the gauge stayed made of `█`, and every row of the
// Storage and System panels ran further than the console had cells for.
// The overflow arrived at the beginning of the row below, which is where
// the menu is: a gauge overwriting the second entry of a list is what the
// screen looked like, and it is why a price is now asked of every
// character instead of of whichever one was easiest to write.
let fit = rules_affordable_gauges_not();
let mut buffer = Buffer::empty(Rect::new(0, 0, 14, 1));
buffer.set_string(0, 0, "│ C: ░░░░░ ███", Style::default());
fit.carry(&mut buffer);
// The rule is exactly as it was drawn, because this console was measured
// charging one cell for it; only the blocks changed hands.
assert_eq!(read(&buffer, 0), "│ C: ..... ###");
}
#[test]
fn the_probe_asks_the_price_of_everything_that_ends_up_on_the_screen() {
// The invariant that would have caught it: the list the console is asked
// about and the list the interface draws are one list. A glyph added to
// a card, or drawn by a widget nobody thought about, has to be priced —
// and no test that renders a frame can tell, because on a terminal that
// charges one cell for everything an unpriced glyph is invisible.
let asked = witnesses();
for face in FACES {
for ch in face.chars() {
if in_dispute(ch) {
assert!(asked.contains(&ch), "`{ch}` is drawn but never priced");
}
}
}
// The shading and the bars come out of ratatui, not out of this crate.
for ch in ['░', '▒', '▓', '█', '▄', '▉', '▏', '│', '─', '╭', '╰'] {
assert!(asked.contains(&ch), "the widgets draw `{ch}`");
}
// Chinese is priced the same by both sides and is not worth a question.
assert!(!asked.contains(&'回'), "a person's text is not in dispute");
assert!(asked.len() > 20, "{asked:?} is not this interface");
}
#[test]
fn a_card_is_still_a_card_after_the_exchange() {
// ratatui's own border widget rather than a hand-written row, so this