| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
use makeover_layout::{Act, Field, FieldKind, Figure, Heading, Meter, Token, Tone}; |
| 48 |
use ratatui::buffer::Buffer; |
| 49 |
use ratatui::layout::Rect; |
| 50 |
use ratatui::style::{Modifier, Style}; |
| 51 |
use ratatui::text::{Line, Span}; |
| 52 |
|
| 53 |
use crate::text; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 70 |
pub struct PieceStyle { |
| 71 |
|
| 72 |
pub content: Style, |
| 73 |
|
| 74 |
pub secondary: Style, |
| 75 |
|
| 76 |
pub muted: Style, |
| 77 |
|
| 78 |
pub info: Style, |
| 79 |
|
| 80 |
pub success: Style, |
| 81 |
|
| 82 |
pub warning: Style, |
| 83 |
|
| 84 |
pub danger: Style, |
| 85 |
|
| 86 |
pub page: Style, |
| 87 |
|
| 88 |
pub section: Style, |
| 89 |
|
| 90 |
pub subsection: Style, |
| 91 |
|
| 92 |
pub action: Style, |
| 93 |
|
| 94 |
|
| 95 |
pub filled: Style, |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
pub sunken: Style, |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
pub focus: Modifier, |
| 106 |
|
| 107 |
pub meter_cells: u16, |
| 108 |
|
| 109 |
pub meter_full: char, |
| 110 |
|
| 111 |
pub meter_empty: char, |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
pub required_marker: &'static str, |
| 117 |
} |
| 118 |
|
| 119 |
impl Default for PieceStyle { |
| 120 |
|
| 121 |
|
| 122 |
fn default() -> Self { |
| 123 |
Self { |
| 124 |
content: Style::new(), |
| 125 |
secondary: Style::new(), |
| 126 |
muted: Style::new().add_modifier(Modifier::DIM), |
| 127 |
info: Style::new(), |
| 128 |
success: Style::new(), |
| 129 |
warning: Style::new(), |
| 130 |
danger: Style::new().add_modifier(Modifier::BOLD), |
| 131 |
page: Style::new().add_modifier(Modifier::BOLD), |
| 132 |
section: Style::new().add_modifier(Modifier::BOLD), |
| 133 |
subsection: Style::new(), |
| 134 |
action: Style::new().add_modifier(Modifier::UNDERLINED), |
| 135 |
filled: Style::new().add_modifier(Modifier::REVERSED), |
| 136 |
sunken: Style::new().add_modifier(Modifier::DIM), |
| 137 |
focus: Modifier::REVERSED, |
| 138 |
meter_cells: 10, |
| 139 |
meter_full: '#', |
| 140 |
meter_empty: '-', |
| 141 |
required_marker: "*", |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
impl PieceStyle { |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
#[cfg(feature = "theme")] |
| 154 |
#[must_use] |
| 155 |
pub fn from_theme(theme: &crate::Theme) -> Self { |
| 156 |
Self { |
| 157 |
content: Style::new().fg(theme.content_primary), |
| 158 |
secondary: Style::new().fg(theme.content_secondary), |
| 159 |
muted: Style::new().fg(theme.content_muted), |
| 160 |
info: Style::new().fg(theme.status_info), |
| 161 |
success: Style::new().fg(theme.status_success), |
| 162 |
warning: Style::new().fg(theme.status_warning), |
| 163 |
danger: Style::new().fg(theme.status_danger), |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
page: Style::new() |
| 171 |
.fg(theme.action_primary) |
| 172 |
.add_modifier(Modifier::BOLD), |
| 173 |
section: Style::new() |
| 174 |
.fg(theme.content_primary) |
| 175 |
.add_modifier(Modifier::BOLD), |
| 176 |
subsection: Style::new().fg(theme.content_secondary), |
| 177 |
action: Style::new().fg(theme.action_primary), |
| 178 |
filled: Style::new().fg(theme.selection_on).bg(theme.action_primary), |
| 179 |
sunken: Style::new().bg(theme.surface_sunken), |
| 180 |
focus: Modifier::REVERSED, |
| 181 |
meter_cells: 10, |
| 182 |
meter_full: '#', |
| 183 |
meter_empty: '-', |
| 184 |
required_marker: "*", |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
#[must_use] |
| 193 |
pub const fn tone(&self, tone: Tone) -> Style { |
| 194 |
match tone { |
| 195 |
Tone::Neutral => self.content, |
| 196 |
Tone::Info => self.info, |
| 197 |
Tone::Success => self.success, |
| 198 |
Tone::Warning => self.warning, |
| 199 |
Tone::Danger => self.danger, |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
#[must_use] |
| 205 |
pub const fn heading(&self, level: Heading) -> Style { |
| 206 |
match level { |
| 207 |
Heading::Page => self.page, |
| 208 |
Heading::Section => self.section, |
| 209 |
Heading::Subsection => self.subsection, |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
#[must_use] |
| 218 |
pub fn focused(&self, focused: bool, style: Style) -> Style { |
| 219 |
if focused { |
| 220 |
style.add_modifier(self.focus) |
| 221 |
} else { |
| 222 |
style |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] |
| 238 |
pub enum Held<'a> { |
| 239 |
|
| 240 |
#[default] |
| 241 |
Absent, |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
Text(&'a str), |
| 246 |
|
| 247 |
On(bool), |
| 248 |
} |
| 249 |
|
| 250 |
impl<'a> Held<'a> { |
| 251 |
|
| 252 |
#[must_use] |
| 253 |
pub const fn text(self) -> &'a str { |
| 254 |
match self { |
| 255 |
Self::Text(text) => text, |
| 256 |
Self::Absent | Self::On(_) => "", |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
#[must_use] |
| 262 |
pub const fn on(self) -> bool { |
| 263 |
matches!(self, Self::On(true)) |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
#[must_use] |
| 273 |
pub fn meter(style: &PieceStyle, meter: &Meter<'_>) -> Line<'static> { |
| 274 |
let cells = u32::from(style.meter_cells); |
| 275 |
let filled = meter |
| 276 |
.done |
| 277 |
.checked_mul(cells) |
| 278 |
.and_then(|reached| reached.checked_div(meter.total)) |
| 279 |
.unwrap_or(0) |
| 280 |
.min(cells); |
| 281 |
let bar = format!( |
| 282 |
"{}{}", |
| 283 |
style.meter_full.to_string().repeat(filled as usize), |
| 284 |
style |
| 285 |
.meter_empty |
| 286 |
.to_string() |
| 287 |
.repeat((cells - filled) as usize) |
| 288 |
); |
| 289 |
let reading = match meter.label { |
| 290 |
Some(label) => format!(" {}/{} {label}", meter.done, meter.total), |
| 291 |
None => format!(" {}/{}", meter.done, meter.total), |
| 292 |
}; |
| 293 |
Line::from(vec![ |
| 294 |
Span::styled(bar, style.tone(meter.tone)), |
| 295 |
Span::styled(reading, style.muted), |
| 296 |
]) |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
#[must_use] |
| 316 |
pub fn token( |
| 317 |
style: &PieceStyle, |
| 318 |
label: &str, |
| 319 |
kind: Token, |
| 320 |
tone: Tone, |
| 321 |
latched: bool, |
| 322 |
focused: bool, |
| 323 |
) -> Span<'static> { |
| 324 |
let painted = style.tone(tone); |
| 325 |
let painted = if latched { |
| 326 |
painted.add_modifier(style.focus) |
| 327 |
} else { |
| 328 |
style.focused(focused, painted) |
| 329 |
}; |
| 330 |
match kind { |
| 331 |
Token::Badge => Span::styled(format!("({label})"), painted), |
| 332 |
Token::Chip { .. } => Span::styled(format!("[{label}]"), painted), |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
#[must_use] |
| 347 |
pub fn act(style: &PieceStyle, act: &Act<'_>, focused: bool) -> Line<'static> { |
| 348 |
let painted = if act.disabled() { |
| 349 |
style.muted |
| 350 |
} else { |
| 351 |
style.focused(focused, style.tone(act.tone)) |
| 352 |
}; |
| 353 |
let label = match act.key { |
| 354 |
Some(key) => format!("< {} > ({key})", act.label), |
| 355 |
None => format!("< {} >", act.label), |
| 356 |
}; |
| 357 |
Line::from(Span::styled(label, painted)) |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
#[must_use] |
| 366 |
pub fn filled_act(style: &PieceStyle, label: &str, focused: bool) -> Line<'static> { |
| 367 |
Line::from(Span::styled( |
| 368 |
format!("[ {label} ]"), |
| 369 |
style.focused(focused, style.filled), |
| 370 |
)) |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
#[must_use] |
| 375 |
pub fn figure_height(figure: &Figure<'_>, width: u16) -> u16 { |
| 376 |
text::height(figure.value, width) + text::height(figure.caption, width) |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
pub fn figure(style: &PieceStyle, figure: &Figure<'_>, area: Rect, buf: &mut Buffer) -> u16 { |
| 385 |
let value = match figure.change { |
| 386 |
Some(change) => format!("{} {change}", figure.value), |
| 387 |
None => figure.value.to_owned(), |
| 388 |
}; |
| 389 |
let used = text::draw( |
| 390 |
&value, |
| 391 |
style.tone(figure.tone).add_modifier(Modifier::BOLD), |
| 392 |
area, |
| 393 |
buf, |
| 394 |
); |
| 395 |
used + text::draw(figure.caption, style.muted, below(area, used), buf) |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
#[must_use] |
| 404 |
pub fn field_height(style: &PieceStyle, field: &Field<'_>, width: u16) -> u16 { |
| 405 |
if !field.kind.visible() { |
| 406 |
return 0; |
| 407 |
} |
| 408 |
let label = text::height(&label_of(style, field), width); |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
let body = match field.kind { |
| 413 |
FieldKind::Textarea => 3, |
| 414 |
kind if kind.offers_options() => u16::try_from(field.options.len()).unwrap_or(u16::MAX), |
| 415 |
_ => 1, |
| 416 |
}; |
| 417 |
let note = note_of(field).map_or(0, |note| text::height(note, width)); |
| 418 |
label + body + note |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
pub fn field( |
| 429 |
style: &PieceStyle, |
| 430 |
field: &Field<'_>, |
| 431 |
held: Held<'_>, |
| 432 |
focused: bool, |
| 433 |
area: Rect, |
| 434 |
buf: &mut Buffer, |
| 435 |
) -> u16 { |
| 436 |
|
| 437 |
|
| 438 |
if !field.kind.visible() || area.width == 0 || area.height == 0 { |
| 439 |
return 0; |
| 440 |
} |
| 441 |
|
| 442 |
let mut used = text::draw(&label_of(style, field), style.secondary, area, buf); |
| 443 |
|
| 444 |
let well = style.focused(focused, style.content); |
| 445 |
let placeholder = field.placeholder.unwrap_or_default(); |
| 446 |
|
| 447 |
used += match field.kind { |
| 448 |
FieldKind::Checkbox => text::draw( |
| 449 |
if held.on() { "[x]" } else { "[ ]" }, |
| 450 |
well, |
| 451 |
below(area, used), |
| 452 |
buf, |
| 453 |
), |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
FieldKind::Range if field.bounded() => { |
| 463 |
let line = range_line(style, field, held.text(), well); |
| 464 |
text::draw_line(&line, below(area, used), buf) |
| 465 |
} |
| 466 |
kind if kind.offers_options() => { |
| 467 |
let mut rows = 0; |
| 468 |
for choice in field.options { |
| 469 |
let chosen = held.text() == choice.value; |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
let (mark, painted, suffix) = match choice.unavailable { |
| 475 |
Some(reason) => ("( )", style.muted, format!(": {reason}")), |
| 476 |
None if chosen => ("(*)", well, String::new()), |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
None => ("( )", style.secondary, String::new()), |
| 483 |
}; |
| 484 |
rows += text::draw( |
| 485 |
&format!("{mark} {}{suffix}", choice.label), |
| 486 |
painted, |
| 487 |
below(area, used + rows), |
| 488 |
buf, |
| 489 |
); |
| 490 |
} |
| 491 |
rows |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
FieldKind::Secret if !held.text().is_empty() => { |
| 498 |
let dots = "*".repeat(held.text().chars().count()); |
| 499 |
text::draw(&dots, well, below(area, used), buf).max(1) |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
_ if held.text().is_empty() => { |
| 505 |
empty_well(style, placeholder, well, focused, below(area, used), buf) |
| 506 |
} |
| 507 |
_ => text::draw(held.text(), well, below(area, used), buf), |
| 508 |
}; |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
match note_of(field) { |
| 514 |
Some(note) => { |
| 515 |
let painted = if field.error.is_some() { |
| 516 |
style.danger |
| 517 |
} else { |
| 518 |
style.muted |
| 519 |
}; |
| 520 |
used + text::draw(note, painted, below(area, used), buf) |
| 521 |
} |
| 522 |
None => used, |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
fn range_line(style: &PieceStyle, field: &Field<'_>, value: &str, well: Style) -> Line<'static> { |
| 543 |
let cells = usize::from(style.meter_cells); |
| 544 |
let ends = field |
| 545 |
.min |
| 546 |
.zip(field.max) |
| 547 |
.and_then(|(min, max)| Some((min.parse::<f64>().ok()?, max.parse::<f64>().ok()?))); |
| 548 |
let filled = match (ends, value.parse::<f64>()) { |
| 549 |
(Some((min, max)), Ok(number)) if max > min => { |
| 550 |
#[expect( |
| 551 |
clippy::cast_possible_truncation, |
| 552 |
clippy::cast_sign_loss, |
| 553 |
reason = "the proportion is clamped to 0..=1 before it is scaled by a cell count \ |
| 554 |
that came from a u16" |
| 555 |
)] |
| 556 |
let reached = (((number - min) / (max - min)).clamp(0.0, 1.0) * cells as f64) as usize; |
| 557 |
reached |
| 558 |
} |
| 559 |
_ => 0, |
| 560 |
}; |
| 561 |
let bar = format!( |
| 562 |
"{}{}", |
| 563 |
style.meter_full.to_string().repeat(filled), |
| 564 |
style.meter_empty.to_string().repeat(cells - filled) |
| 565 |
); |
| 566 |
Line::from(vec![ |
| 567 |
Span::styled(format!("{} ", field.min.unwrap_or_default()), style.muted), |
| 568 |
Span::styled(bar, well), |
| 569 |
Span::styled(format!(" {}", field.max.unwrap_or_default()), style.muted), |
| 570 |
Span::styled(format!(" {value}"), well), |
| 571 |
]) |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
fn label_of(style: &PieceStyle, field: &Field<'_>) -> String { |
| 576 |
if field.required { |
| 577 |
format!("{} {}", field.label, style.required_marker) |
| 578 |
} else { |
| 579 |
field.label.to_owned() |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
fn note_of<'a>(field: &Field<'a>) -> Option<&'a str> { |
| 585 |
field.error.or(field.hint) |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
fn empty_well( |
| 596 |
style: &PieceStyle, |
| 597 |
placeholder: &str, |
| 598 |
well: Style, |
| 599 |
focused: bool, |
| 600 |
area: Rect, |
| 601 |
buf: &mut Buffer, |
| 602 |
) -> u16 { |
| 603 |
let used = text::draw(placeholder, style.muted, area, buf).max(1); |
| 604 |
if focused |
| 605 |
&& area.height > 0 |
| 606 |
&& area.width > 0 |
| 607 |
&& let Some(cell) = buf.cell_mut((area.x, area.y)) |
| 608 |
{ |
| 609 |
cell.set_style(well); |
| 610 |
} |
| 611 |
used |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
fn below(area: Rect, used: u16) -> Rect { |
| 616 |
let used = used.min(area.height); |
| 617 |
Rect { |
| 618 |
x: area.x, |
| 619 |
y: area.y + used, |
| 620 |
width: area.width, |
| 621 |
height: area.height - used, |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
#[cfg(test)] |
| 626 |
mod tests { |
| 627 |
use super::*; |
| 628 |
use makeover_layout::{Choice, State}; |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
fn style() -> PieceStyle { |
| 633 |
PieceStyle { |
| 634 |
content: Style::new().add_modifier(Modifier::BOLD), |
| 635 |
secondary: Style::new().add_modifier(Modifier::ITALIC), |
| 636 |
muted: Style::new().add_modifier(Modifier::DIM), |
| 637 |
danger: Style::new().add_modifier(Modifier::CROSSED_OUT), |
| 638 |
..PieceStyle::default() |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
fn buffer(width: u16, height: u16) -> Buffer { |
| 643 |
Buffer::empty(Rect::new(0, 0, width, height)) |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
fn rows(buf: &Buffer) -> Vec<String> { |
| 648 |
(0..buf.area.height) |
| 649 |
.map(|y| { |
| 650 |
(0..buf.area.width) |
| 651 |
.map(|x| { |
| 652 |
buf.cell((x, y)) |
| 653 |
.map_or(' ', |c| c.symbol().chars().next().unwrap_or(' ')) |
| 654 |
}) |
| 655 |
.collect::<String>() |
| 656 |
.trim_end() |
| 657 |
.to_owned() |
| 658 |
}) |
| 659 |
.collect() |
| 660 |
} |
| 661 |
|
| 662 |
#[test] |
| 663 |
fn a_bar_fills_in_proportion_and_reads_out_the_two_numbers() { |
| 664 |
let style = style(); |
| 665 |
let line = meter(&style, &Meter::new(3, 10).label("subtasks")); |
| 666 |
let drawn: String = line.spans.iter().map(|s| s.content.as_ref()).collect(); |
| 667 |
assert_eq!(drawn, "###------- 3/10 subtasks"); |
| 668 |
|
| 669 |
|
| 670 |
let bare = meter(&style, &Meter::new(3, 10)); |
| 671 |
let drawn: String = bare.spans.iter().map(|s| s.content.as_ref()).collect(); |
| 672 |
assert_eq!(drawn, "###------- 3/10"); |
| 673 |
} |
| 674 |
|
| 675 |
#[test] |
| 676 |
fn an_empty_set_is_an_empty_bar_rather_than_a_divide_by_zero() { |
| 677 |
|
| 678 |
|
| 679 |
let line = meter(&style(), &Meter::new(0, 0)); |
| 680 |
let drawn: String = line.spans.iter().map(|s| s.content.as_ref()).collect(); |
| 681 |
assert_eq!(drawn, "---------- 0/0"); |
| 682 |
} |
| 683 |
|
| 684 |
#[test] |
| 685 |
fn an_over_run_fills_the_bar_and_still_reports_the_overflow() { |
| 686 |
|
| 687 |
|
| 688 |
let line = meter(&style(), &Meter::new(14, 10)); |
| 689 |
let drawn: String = line.spans.iter().map(|s| s.content.as_ref()).collect(); |
| 690 |
assert_eq!(drawn, "########## 14/10"); |
| 691 |
} |
| 692 |
|
| 693 |
#[test] |
| 694 |
fn a_badge_is_round_and_a_chip_is_square() { |
| 695 |
|
| 696 |
|
| 697 |
let style = style(); |
| 698 |
let badge = token(&style, "draft", Token::Badge, Tone::Neutral, false, false); |
| 699 |
assert_eq!(badge.content.as_ref(), "(draft)"); |
| 700 |
let chip = token( |
| 701 |
&style, |
| 702 |
"rust", |
| 703 |
Token::Chip { removable: false }, |
| 704 |
Tone::Neutral, |
| 705 |
false, |
| 706 |
false, |
| 707 |
); |
| 708 |
assert_eq!(chip.content.as_ref(), "[rust]"); |
| 709 |
} |
| 710 |
|
| 711 |
#[test] |
| 712 |
fn a_latched_chip_reads_the_same_as_a_focused_one() { |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
let style = style(); |
| 717 |
let kind = Token::Chip { removable: false }; |
| 718 |
let latched = token(&style, "rust", kind, Tone::Neutral, true, false); |
| 719 |
let focused = token(&style, "rust", kind, Tone::Neutral, false, true); |
| 720 |
assert_eq!(latched.style, focused.style); |
| 721 |
assert!(latched.style.add_modifier.contains(Modifier::REVERSED)); |
| 722 |
} |
| 723 |
|
| 724 |
#[test] |
| 725 |
fn a_control_draws_its_key_only_where_one_was_named() { |
| 726 |
let style = style(); |
| 727 |
let line = act(&style, &Act::new("Delete"), false); |
| 728 |
assert_eq!(line.spans[0].content.as_ref(), "< Delete >"); |
| 729 |
let line = act(&style, &Act::new("Quit").key("q"), false); |
| 730 |
assert_eq!(line.spans[0].content.as_ref(), "< Quit > (q)"); |
| 731 |
} |
| 732 |
|
| 733 |
#[test] |
| 734 |
fn a_disabled_control_is_never_marked_focused() { |
| 735 |
|
| 736 |
|
| 737 |
let style = style(); |
| 738 |
let disabled = Act::new("Save").state(State::Disabled); |
| 739 |
let line = act(&style, &disabled, true); |
| 740 |
assert!( |
| 741 |
!line.spans[0] |
| 742 |
.style |
| 743 |
.add_modifier |
| 744 |
.contains(Modifier::REVERSED) |
| 745 |
); |
| 746 |
assert_eq!(line.spans[0].style, style.muted); |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
let unstated = Act::new("Save"); |
| 751 |
let line = act(&style, &unstated, true); |
| 752 |
assert!( |
| 753 |
line.spans[0] |
| 754 |
.style |
| 755 |
.add_modifier |
| 756 |
.contains(Modifier::REVERSED) |
| 757 |
); |
| 758 |
} |
| 759 |
|
| 760 |
#[test] |
| 761 |
fn a_danger_control_keeps_its_tone_under_focus() { |
| 762 |
|
| 763 |
|
| 764 |
let style = style(); |
| 765 |
let line = act(&style, &Act::new("Delete").tone(Tone::Danger), true); |
| 766 |
assert_eq!( |
| 767 |
line.spans[0].style.add_modifier, |
| 768 |
style.danger.add_modifier | Modifier::REVERSED |
| 769 |
); |
| 770 |
} |
| 771 |
|
| 772 |
#[test] |
| 773 |
fn a_figure_puts_the_number_over_what_it_counts() { |
| 774 |
let style = style(); |
| 775 |
let figure_ = Figure::new("42", "open tasks"); |
| 776 |
let mut buf = buffer(20, 4); |
| 777 |
let used = figure(&style, &figure_, buf.area, &mut buf); |
| 778 |
assert_eq!(used, 2); |
| 779 |
assert_eq!(rows(&buf)[..2], ["42".to_owned(), "open tasks".to_owned()]); |
| 780 |
assert_eq!(figure_height(&figure_, 20), 2); |
| 781 |
} |
| 782 |
|
| 783 |
#[test] |
| 784 |
fn a_figures_change_rides_on_the_value_row() { |
| 785 |
|
| 786 |
|
| 787 |
let style = style(); |
| 788 |
let figure_ = Figure::new("42", "open tasks") |
| 789 |
.change("+3") |
| 790 |
.tone(Tone::Success); |
| 791 |
let mut buf = buffer(20, 4); |
| 792 |
figure(&style, &figure_, buf.area, &mut buf); |
| 793 |
assert_eq!(rows(&buf)[0], "42 +3"); |
| 794 |
} |
| 795 |
|
| 796 |
#[test] |
| 797 |
fn a_compulsory_field_says_so_in_its_label() { |
| 798 |
let style = style(); |
| 799 |
let mut field_ = Field::new(FieldKind::Text, "email", "Email"); |
| 800 |
field_.required = true; |
| 801 |
let mut buf = buffer(20, 4); |
| 802 |
field(&style, &field_, Held::Absent, false, buf.area, &mut buf); |
| 803 |
assert_eq!(rows(&buf)[0], "Email *"); |
| 804 |
} |
| 805 |
|
| 806 |
#[test] |
| 807 |
fn a_hidden_field_costs_no_rows_at_all() { |
| 808 |
|
| 809 |
let style = style(); |
| 810 |
let field_ = Field::new(FieldKind::Hidden, "csrf", "Token"); |
| 811 |
let mut buf = buffer(20, 4); |
| 812 |
assert_eq!( |
| 813 |
field( |
| 814 |
&style, |
| 815 |
&field_, |
| 816 |
Held::Text("abc"), |
| 817 |
false, |
| 818 |
buf.area, |
| 819 |
&mut buf |
| 820 |
), |
| 821 |
0 |
| 822 |
); |
| 823 |
assert_eq!(field_height(&style, &field_, 20), 0); |
| 824 |
assert_eq!(rows(&buf)[0], ""); |
| 825 |
} |
| 826 |
|
| 827 |
#[test] |
| 828 |
fn a_secret_is_dotted_from_the_callers_buffer_and_never_from_the_description() { |
| 829 |
|
| 830 |
|
| 831 |
let style = style(); |
| 832 |
let field_ = Field::new(FieldKind::Secret, "password", "Password"); |
| 833 |
let mut buf = buffer(20, 4); |
| 834 |
field( |
| 835 |
&style, |
| 836 |
&field_, |
| 837 |
Held::Text("hunter2"), |
| 838 |
false, |
| 839 |
buf.area, |
| 840 |
&mut buf, |
| 841 |
); |
| 842 |
assert_eq!(rows(&buf)[1], "*******"); |
| 843 |
} |
| 844 |
|
| 845 |
#[test] |
| 846 |
fn an_error_takes_the_row_the_hint_would_have_had() { |
| 847 |
|
| 848 |
|
| 849 |
let style = style(); |
| 850 |
let mut field_ = Field::new(FieldKind::Text, "email", "Email"); |
| 851 |
field_.hint = Some("work address"); |
| 852 |
field_.error = Some("not an address"); |
| 853 |
let mut buf = buffer(20, 5); |
| 854 |
field( |
| 855 |
&style, |
| 856 |
&field_, |
| 857 |
Held::Text("nope"), |
| 858 |
false, |
| 859 |
buf.area, |
| 860 |
&mut buf, |
| 861 |
); |
| 862 |
assert_eq!(rows(&buf)[2], "not an address"); |
| 863 |
assert_eq!(field_height(&style, &field_, 20), 3); |
| 864 |
} |
| 865 |
|
| 866 |
#[test] |
| 867 |
fn a_focused_empty_box_shows_where_the_typing_will_land() { |
| 868 |
|
| 869 |
|
| 870 |
let style = style(); |
| 871 |
let field_ = Field::new(FieldKind::Text, "email", "Email"); |
| 872 |
let mut buf = buffer(20, 4); |
| 873 |
field(&style, &field_, Held::Absent, true, buf.area, &mut buf); |
| 874 |
let caret = buf.cell((0, 1)).expect("the well's first cell").style(); |
| 875 |
assert!(caret.add_modifier.contains(Modifier::REVERSED)); |
| 876 |
} |
| 877 |
|
| 878 |
#[test] |
| 879 |
fn a_choice_field_marks_the_chosen_option_and_costs_a_row_each() { |
| 880 |
let style = style(); |
| 881 |
let mut field_ = Field::new(FieldKind::Radio, "size", "Size"); |
| 882 |
let options = [Choice::plain("small"), Choice::plain("large")]; |
| 883 |
field_.options = &options; |
| 884 |
let mut buf = buffer(20, 5); |
| 885 |
field( |
| 886 |
&style, |
| 887 |
&field_, |
| 888 |
Held::Text("large"), |
| 889 |
false, |
| 890 |
buf.area, |
| 891 |
&mut buf, |
| 892 |
); |
| 893 |
assert_eq!(rows(&buf)[1], "( ) small"); |
| 894 |
assert_eq!(rows(&buf)[2], "(*) large"); |
| 895 |
assert_eq!(field_height(&style, &field_, 20), 3); |
| 896 |
} |
| 897 |
|
| 898 |
#[test] |
| 899 |
fn a_range_draws_its_two_ends_and_where_the_value_sits_between_them() { |
| 900 |
let style = style(); |
| 901 |
let field_ = Field::range("review", "Review above", "0", "1"); |
| 902 |
let mut buf = buffer(40, 3); |
| 903 |
field( |
| 904 |
&style, |
| 905 |
&field_, |
| 906 |
Held::Text("0.5"), |
| 907 |
false, |
| 908 |
buf.area, |
| 909 |
&mut buf, |
| 910 |
); |
| 911 |
|
| 912 |
|
| 913 |
assert_eq!(rows(&buf)[1].trim_end(), "0 #####----- 1 0.5"); |
| 914 |
assert_eq!(field_height(&style, &field_, 40), 2); |
| 915 |
} |
| 916 |
|
| 917 |
#[test] |
| 918 |
fn a_range_holding_something_unreadable_still_shows_it() { |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
let style = style(); |
| 923 |
let field_ = Field::range("review", "Review above", "0", "1"); |
| 924 |
let mut buf = buffer(40, 3); |
| 925 |
field( |
| 926 |
&style, |
| 927 |
&field_, |
| 928 |
Held::Text("unset"), |
| 929 |
false, |
| 930 |
buf.area, |
| 931 |
&mut buf, |
| 932 |
); |
| 933 |
assert_eq!(rows(&buf)[1].trim_end(), "0 ---------- 1 unset"); |
| 934 |
} |
| 935 |
|
| 936 |
#[test] |
| 937 |
fn an_unbounded_range_is_typed_into_rather_than_dragged() { |
| 938 |
|
| 939 |
|
| 940 |
let style = style(); |
| 941 |
let field_ = Field { |
| 942 |
max: Some("1"), |
| 943 |
..Field::new(FieldKind::Range, "review", "Review above") |
| 944 |
}; |
| 945 |
let mut buf = buffer(40, 3); |
| 946 |
field( |
| 947 |
&style, |
| 948 |
&field_, |
| 949 |
Held::Text("0.5"), |
| 950 |
false, |
| 951 |
buf.area, |
| 952 |
&mut buf, |
| 953 |
); |
| 954 |
assert_eq!(rows(&buf)[1].trim_end(), "0.5"); |
| 955 |
} |
| 956 |
|
| 957 |
#[test] |
| 958 |
fn an_unavailable_option_reads_as_inert_and_says_why() { |
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
let style = style(); |
| 963 |
let options = [ |
| 964 |
Choice::new("chromatic", "Chromatic"), |
| 965 |
Choice::new("multi", "Multi-sample").unless("Drop a second sample."), |
| 966 |
]; |
| 967 |
let mut field_ = Field::new(FieldKind::Radio, "mode", "Mode"); |
| 968 |
field_.options = &options; |
| 969 |
let mut buf = buffer(46, 4); |
| 970 |
field( |
| 971 |
&style, |
| 972 |
&field_, |
| 973 |
Held::Text("chromatic"), |
| 974 |
false, |
| 975 |
buf.area, |
| 976 |
&mut buf, |
| 977 |
); |
| 978 |
assert_eq!(rows(&buf)[1].trim_end(), "(*) Chromatic"); |
| 979 |
assert_eq!( |
| 980 |
rows(&buf)[2].trim_end(), |
| 981 |
"( ) Multi-sample: Drop a second sample." |
| 982 |
); |
| 983 |
let muted = buf.cell((0, 2)).expect("the unavailable row").style(); |
| 984 |
assert!(muted.add_modifier.contains(Modifier::DIM)); |
| 985 |
} |
| 986 |
|
| 987 |
#[test] |
| 988 |
fn an_unchosen_option_does_not_read_as_disabled() { |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
let style = style(); |
| 993 |
let mut field_ = Field::new(FieldKind::Radio, "size", "Size"); |
| 994 |
let options = [Choice::plain("small"), Choice::plain("large")]; |
| 995 |
field_.options = &options; |
| 996 |
let mut buf = buffer(20, 5); |
| 997 |
field( |
| 998 |
&style, |
| 999 |
&field_, |
| 1000 |
Held::Text("large"), |
| 1001 |
false, |
| 1002 |
buf.area, |
| 1003 |
&mut buf, |
| 1004 |
); |
| 1005 |
let unchosen = buf.cell((0, 1)).expect("the first option").style(); |
| 1006 |
assert_eq!(unchosen.add_modifier, style.secondary.add_modifier); |
| 1007 |
assert_ne!(unchosen.add_modifier, style.muted.add_modifier); |
| 1008 |
} |
| 1009 |
|
| 1010 |
#[test] |
| 1011 |
fn a_checkbox_reads_a_bool_rather_than_a_submitted_string() { |
| 1012 |
|
| 1013 |
|
| 1014 |
let style = style(); |
| 1015 |
let field_ = Field::new(FieldKind::Checkbox, "agree", "Agree"); |
| 1016 |
let mut buf = buffer(20, 4); |
| 1017 |
field(&style, &field_, Held::On(true), false, buf.area, &mut buf); |
| 1018 |
assert_eq!(rows(&buf)[1], "[x]"); |
| 1019 |
let mut buf = buffer(20, 4); |
| 1020 |
field(&style, &field_, Held::On(false), false, buf.area, &mut buf); |
| 1021 |
assert_eq!(rows(&buf)[1], "[ ]"); |
| 1022 |
} |
| 1023 |
|
| 1024 |
#[test] |
| 1025 |
fn a_tone_and_a_heading_map_without_a_fallback_arm() { |
| 1026 |
|
| 1027 |
|
| 1028 |
let style = style(); |
| 1029 |
assert_eq!(style.tone(Tone::Neutral), style.content); |
| 1030 |
assert_eq!(style.tone(Tone::Danger), style.danger); |
| 1031 |
assert_eq!(style.heading(Heading::Page), style.page); |
| 1032 |
assert_eq!(style.heading(Heading::Subsection), style.subsection); |
| 1033 |
} |
| 1034 |
|
| 1035 |
#[test] |
| 1036 |
fn the_default_style_carries_no_colour_at_all() { |
| 1037 |
|
| 1038 |
|
| 1039 |
let style = PieceStyle::default(); |
| 1040 |
for painted in [style.content, style.danger, style.page, style.action] { |
| 1041 |
assert_eq!(painted.fg, None); |
| 1042 |
assert_eq!(painted.bg, None); |
| 1043 |
} |
| 1044 |
} |
| 1045 |
} |
| 1046 |
|