| 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 |
use makeover_layout::{ |
| 39 |
Act, Awaiting, Field, FieldKind, Figure, Heading, Meter, ThemeVariant, Token, Tone, |
| 40 |
}; |
| 41 |
use ratatui::buffer::Buffer; |
| 42 |
use ratatui::layout::Rect; |
| 43 |
use ratatui::style::{Modifier, Style}; |
| 44 |
use ratatui::text::{Line, Span}; |
| 45 |
|
| 46 |
use crate::text; |
| 47 |
use std::time::Duration; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 64 |
pub struct PieceStyle { |
| 65 |
|
| 66 |
pub content: Style, |
| 67 |
|
| 68 |
pub secondary: Style, |
| 69 |
|
| 70 |
pub muted: Style, |
| 71 |
|
| 72 |
pub info: Style, |
| 73 |
|
| 74 |
pub success: Style, |
| 75 |
|
| 76 |
pub warning: Style, |
| 77 |
|
| 78 |
pub danger: Style, |
| 79 |
|
| 80 |
pub page: Style, |
| 81 |
|
| 82 |
pub section: Style, |
| 83 |
|
| 84 |
pub subsection: Style, |
| 85 |
|
| 86 |
pub action: Style, |
| 87 |
|
| 88 |
|
| 89 |
pub filled: Style, |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
pub sunken: Style, |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
pub focus: Modifier, |
| 100 |
|
| 101 |
pub meter_cells: u16, |
| 102 |
|
| 103 |
pub meter_full: char, |
| 104 |
|
| 105 |
pub meter_empty: char, |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
pub required_marker: &'static str, |
| 111 |
} |
| 112 |
|
| 113 |
impl Default for PieceStyle { |
| 114 |
|
| 115 |
|
| 116 |
fn default() -> Self { |
| 117 |
Self { |
| 118 |
content: Style::new(), |
| 119 |
secondary: Style::new(), |
| 120 |
muted: Style::new().add_modifier(Modifier::DIM), |
| 121 |
info: Style::new(), |
| 122 |
success: Style::new(), |
| 123 |
warning: Style::new(), |
| 124 |
danger: Style::new().add_modifier(Modifier::BOLD), |
| 125 |
page: Style::new().add_modifier(Modifier::BOLD), |
| 126 |
section: Style::new().add_modifier(Modifier::BOLD), |
| 127 |
subsection: Style::new(), |
| 128 |
action: Style::new().add_modifier(Modifier::UNDERLINED), |
| 129 |
filled: Style::new().add_modifier(Modifier::REVERSED), |
| 130 |
sunken: Style::new().add_modifier(Modifier::DIM), |
| 131 |
focus: Modifier::REVERSED, |
| 132 |
meter_cells: 10, |
| 133 |
meter_full: '#', |
| 134 |
meter_empty: '-', |
| 135 |
required_marker: "*", |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
impl PieceStyle { |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
#[cfg(feature = "theme")] |
| 148 |
#[must_use] |
| 149 |
pub fn from_theme(theme: &crate::Theme) -> Self { |
| 150 |
Self { |
| 151 |
content: Style::new().fg(theme.content_primary), |
| 152 |
secondary: Style::new().fg(theme.content_secondary), |
| 153 |
muted: Style::new().fg(theme.content_muted), |
| 154 |
info: Style::new().fg(theme.status_info), |
| 155 |
success: Style::new().fg(theme.status_success), |
| 156 |
warning: Style::new().fg(theme.status_warning), |
| 157 |
danger: Style::new().fg(theme.status_danger), |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
page: Style::new() |
| 165 |
.fg(theme.action_primary) |
| 166 |
.add_modifier(Modifier::BOLD), |
| 167 |
section: Style::new() |
| 168 |
.fg(theme.content_primary) |
| 169 |
.add_modifier(Modifier::BOLD), |
| 170 |
subsection: Style::new().fg(theme.content_secondary), |
| 171 |
action: Style::new().fg(theme.action_primary), |
| 172 |
filled: Style::new().fg(theme.selection_on).bg(theme.action_primary), |
| 173 |
sunken: Style::new().bg(theme.surface_sunken), |
| 174 |
focus: Modifier::REVERSED, |
| 175 |
meter_cells: 10, |
| 176 |
meter_full: '#', |
| 177 |
meter_empty: '-', |
| 178 |
required_marker: "*", |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
#[must_use] |
| 187 |
pub const fn tone(&self, tone: Tone) -> Style { |
| 188 |
match tone { |
| 189 |
Tone::Neutral => self.content, |
| 190 |
Tone::Info => self.info, |
| 191 |
Tone::Success => self.success, |
| 192 |
Tone::Warning => self.warning, |
| 193 |
Tone::Danger => self.danger, |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
#[must_use] |
| 199 |
pub const fn heading(&self, level: Heading) -> Style { |
| 200 |
match level { |
| 201 |
Heading::Page => self.page, |
| 202 |
Heading::Section => self.section, |
| 203 |
Heading::Subsection => self.subsection, |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
#[must_use] |
| 212 |
pub fn focused(&self, focused: bool, style: Style) -> Style { |
| 213 |
if focused { |
| 214 |
style.add_modifier(self.focus) |
| 215 |
} else { |
| 216 |
style |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] |
| 232 |
pub enum Held<'a> { |
| 233 |
|
| 234 |
#[default] |
| 235 |
Absent, |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
Text(&'a str), |
| 240 |
|
| 241 |
On(bool), |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
Between { |
| 252 |
|
| 253 |
lower: &'a str, |
| 254 |
|
| 255 |
upper: &'a str, |
| 256 |
}, |
| 257 |
} |
| 258 |
|
| 259 |
impl<'a> Held<'a> { |
| 260 |
|
| 261 |
#[must_use] |
| 262 |
pub const fn text(self) -> &'a str { |
| 263 |
match self { |
| 264 |
Self::Text(text) | Self::Between { lower: text, .. } => text, |
| 265 |
Self::Absent | Self::On(_) => "", |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
#[must_use] |
| 271 |
pub const fn upper(self) -> &'a str { |
| 272 |
match self { |
| 273 |
Self::Between { upper, .. } => upper, |
| 274 |
Self::Absent | Self::Text(_) | Self::On(_) => "", |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
#[must_use] |
| 280 |
pub const fn on(self) -> bool { |
| 281 |
matches!(self, Self::On(true)) |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] |
| 296 |
pub struct Progress { |
| 297 |
|
| 298 |
pub delivered: Option<u64>, |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
pub elapsed: Option<Duration>, |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
#[must_use] |
| 326 |
pub fn activity(style: &PieceStyle, lit: bool) -> Span<'static> { |
| 327 |
if lit { |
| 328 |
Span::styled(style.meter_full.to_string(), style.action) |
| 329 |
} else { |
| 330 |
Span::styled(style.meter_empty.to_string(), style.muted) |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
#[must_use] |
| 360 |
pub fn awaiting( |
| 361 |
style: &PieceStyle, |
| 362 |
awaiting: Awaiting, |
| 363 |
progress: Progress, |
| 364 |
lit: bool, |
| 365 |
) -> Line<'static> { |
| 366 |
let Some(total) = awaiting.amount else { |
| 367 |
return Line::from(vec![activity(style, lit)]); |
| 368 |
}; |
| 369 |
let Some(done) = progress.delivered else { |
| 370 |
return Line::from(vec![ |
| 371 |
activity(style, lit), |
| 372 |
Span::styled(format!(" {total}"), style.muted), |
| 373 |
]); |
| 374 |
}; |
| 375 |
let cells = u32::from(style.meter_cells); |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
let filled = u32::try_from( |
| 382 |
done.saturating_mul(u64::from(cells)) |
| 383 |
.checked_div(total) |
| 384 |
.unwrap_or(0), |
| 385 |
) |
| 386 |
.unwrap_or(cells) |
| 387 |
.min(cells); |
| 388 |
let bar = format!( |
| 389 |
"{}{}", |
| 390 |
style.meter_full.to_string().repeat(filled as usize), |
| 391 |
style |
| 392 |
.meter_empty |
| 393 |
.to_string() |
| 394 |
.repeat((cells - filled) as usize) |
| 395 |
); |
| 396 |
let reading = match progress.elapsed { |
| 397 |
Some(elapsed) => format!(" {done}/{total} {}s", elapsed.as_secs()), |
| 398 |
None => format!(" {done}/{total}"), |
| 399 |
}; |
| 400 |
Line::from(vec![ |
| 401 |
Span::styled(bar, style.action), |
| 402 |
Span::styled(reading, style.muted), |
| 403 |
]) |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
#[must_use] |
| 412 |
pub fn meter(style: &PieceStyle, meter: &Meter<'_>) -> Line<'static> { |
| 413 |
let cells = u32::from(style.meter_cells); |
| 414 |
let filled = meter |
| 415 |
.done |
| 416 |
.checked_mul(cells) |
| 417 |
.and_then(|reached| reached.checked_div(meter.total)) |
| 418 |
.unwrap_or(0) |
| 419 |
.min(cells); |
| 420 |
let bar = format!( |
| 421 |
"{}{}", |
| 422 |
style.meter_full.to_string().repeat(filled as usize), |
| 423 |
style |
| 424 |
.meter_empty |
| 425 |
.to_string() |
| 426 |
.repeat((cells - filled) as usize) |
| 427 |
); |
| 428 |
let reading = match meter.label { |
| 429 |
Some(label) => format!(" {}/{} {label}", meter.done, meter.total), |
| 430 |
None => format!(" {}/{}", meter.done, meter.total), |
| 431 |
}; |
| 432 |
Line::from(vec![ |
| 433 |
Span::styled(bar, style.tone(meter.tone)), |
| 434 |
Span::styled(reading, style.muted), |
| 435 |
]) |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
#[must_use] |
| 455 |
pub fn token( |
| 456 |
style: &PieceStyle, |
| 457 |
label: &str, |
| 458 |
kind: Token, |
| 459 |
tone: Tone, |
| 460 |
latched: bool, |
| 461 |
focused: bool, |
| 462 |
) -> Span<'static> { |
| 463 |
let painted = style.tone(tone); |
| 464 |
let painted = if latched { |
| 465 |
painted.add_modifier(style.focus) |
| 466 |
} else { |
| 467 |
style.focused(focused, painted) |
| 468 |
}; |
| 469 |
match kind { |
| 470 |
Token::Badge => Span::styled(format!("({label})"), painted), |
| 471 |
Token::Chip { .. } => Span::styled(format!("[{label}]"), painted), |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
#[must_use] |
| 486 |
pub fn act(style: &PieceStyle, act: &Act<'_>, focused: bool) -> Line<'static> { |
| 487 |
let painted = if act.disabled() { |
| 488 |
style.muted |
| 489 |
} else { |
| 490 |
style.focused(focused, style.tone(act.tone)) |
| 491 |
}; |
| 492 |
let label = match act.key { |
| 493 |
Some(key) => format!("< {} > ({key})", act.label), |
| 494 |
None => format!("< {} >", act.label), |
| 495 |
}; |
| 496 |
Line::from(Span::styled(label, painted)) |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
#[must_use] |
| 512 |
pub fn act_note(style: &PieceStyle, act: &Act<'_>) -> Option<Line<'static>> { |
| 513 |
act.hint |
| 514 |
.map(|hint| Line::from(Span::styled(hint.to_owned(), style.muted))) |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
#[must_use] |
| 523 |
pub fn filled_act(style: &PieceStyle, label: &str, focused: bool) -> Line<'static> { |
| 524 |
Line::from(Span::styled( |
| 525 |
format!("[ {label} ]"), |
| 526 |
style.focused(focused, style.filled), |
| 527 |
)) |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
#[must_use] |
| 532 |
pub fn figure_height(figure: &Figure<'_>, width: u16) -> u16 { |
| 533 |
text::height(figure.value, width) + text::height(figure.caption, width) |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
pub fn figure(style: &PieceStyle, figure: &Figure<'_>, area: Rect, buf: &mut Buffer) -> u16 { |
| 542 |
let value = match figure.change { |
| 543 |
Some(change) => format!("{} {change}", figure.value), |
| 544 |
None => figure.value.to_owned(), |
| 545 |
}; |
| 546 |
let used = text::draw( |
| 547 |
&value, |
| 548 |
style.tone(figure.tone).add_modifier(Modifier::BOLD), |
| 549 |
area, |
| 550 |
buf, |
| 551 |
); |
| 552 |
used + text::draw(figure.caption, style.muted, below(area, used), buf) |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
#[must_use] |
| 561 |
pub fn field_height(style: &PieceStyle, field: &Field<'_>, width: u16) -> u16 { |
| 562 |
if !field.kind.visible() { |
| 563 |
return 0; |
| 564 |
} |
| 565 |
let label = text::height(&label_of(style, field), width); |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
let body = match field.kind { |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
kind if kind.multiline() => 3, |
| 577 |
kind if kind.offers_options() => u16::try_from(field.options.len()).unwrap_or(u16::MAX), |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
kind if kind.offers_themes() => { |
| 584 |
let mut variants = 0u16; |
| 585 |
let mut open: Option<ThemeVariant> = None; |
| 586 |
for theme in field.themes { |
| 587 |
if open != Some(theme.variant) { |
| 588 |
variants = variants.saturating_add(1); |
| 589 |
open = Some(theme.variant); |
| 590 |
} |
| 591 |
} |
| 592 |
let rows = u16::try_from(field.themes.len()).unwrap_or(u16::MAX); |
| 593 |
rows.saturating_add(variants) |
| 594 |
.saturating_add(u16::from(field.follows.is_some())) |
| 595 |
} |
| 596 |
_ => 1, |
| 597 |
}; |
| 598 |
let note = message_of(style, field).map_or(0, |(text, _)| text::height(text, width)); |
| 599 |
label + body + note |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
pub fn field( |
| 617 |
style: &PieceStyle, |
| 618 |
field: &Field<'_>, |
| 619 |
held: Held<'_>, |
| 620 |
focused: bool, |
| 621 |
area: Rect, |
| 622 |
buf: &mut Buffer, |
| 623 |
) -> u16 { |
| 624 |
|
| 625 |
|
| 626 |
if !field.kind.visible() || area.width == 0 || area.height == 0 { |
| 627 |
return 0; |
| 628 |
} |
| 629 |
|
| 630 |
let mut used = text::draw(&label_of(style, field), style.secondary, area, buf); |
| 631 |
|
| 632 |
let well = style.focused(focused, style.content); |
| 633 |
let placeholder = field.placeholder.unwrap_or_default(); |
| 634 |
|
| 635 |
used += match field.kind { |
| 636 |
FieldKind::Checkbox => text::draw( |
| 637 |
if held.on() { "[x]" } else { "[ ]" }, |
| 638 |
well, |
| 639 |
below(area, used), |
| 640 |
buf, |
| 641 |
), |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
FieldKind::Range if field.bounded() => { |
| 651 |
let line = range_line(style, field, held.text(), well); |
| 652 |
text::draw_line(&line, below(area, used), buf) |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
FieldKind::Interval => { |
| 659 |
let line = interval_line(style, field, held, well); |
| 660 |
text::draw_line(&line, below(area, used), buf) |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
kind if kind.offers_themes() => { |
| 673 |
let mut rows = 0; |
| 674 |
if let Some(follow) = field.follows { |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
let chosen = held.text() == follow.value; |
| 679 |
let (mark, painted) = if chosen { |
| 680 |
("(*)", well) |
| 681 |
} else { |
| 682 |
("( )", style.secondary) |
| 683 |
}; |
| 684 |
rows += text::draw( |
| 685 |
&format!("{mark} {}", follow.label), |
| 686 |
painted, |
| 687 |
below(area, used + rows), |
| 688 |
buf, |
| 689 |
); |
| 690 |
} |
| 691 |
let mut open: Option<ThemeVariant> = None; |
| 692 |
for theme in field.themes { |
| 693 |
if open != Some(theme.variant) { |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
rows += text::draw( |
| 698 |
theme.variant.heading(), |
| 699 |
style.muted, |
| 700 |
below(area, used + rows), |
| 701 |
buf, |
| 702 |
); |
| 703 |
open = Some(theme.variant); |
| 704 |
} |
| 705 |
let chosen = held.text() == theme.id; |
| 706 |
let (mark, painted) = if chosen { |
| 707 |
("(*)", well) |
| 708 |
} else { |
| 709 |
("( )", style.secondary) |
| 710 |
}; |
| 711 |
rows += text::draw( |
| 712 |
&format!("{mark} {} [{}]", theme.name, theme.contrast.badge()), |
| 713 |
painted, |
| 714 |
below(area, used + rows), |
| 715 |
buf, |
| 716 |
); |
| 717 |
} |
| 718 |
rows |
| 719 |
} |
| 720 |
kind if kind.offers_options() => { |
| 721 |
let mut rows = 0; |
| 722 |
for choice in field.options { |
| 723 |
let chosen = held.text() == choice.value; |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
let (mark, painted, suffix) = match choice.unavailable { |
| 729 |
Some(reason) => ("( )", style.muted, format!(": {reason}")), |
| 730 |
None if chosen => ("(*)", well, String::new()), |
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
None => ("( )", style.secondary, String::new()), |
| 737 |
}; |
| 738 |
rows += text::draw( |
| 739 |
&format!("{mark} {}{suffix}", choice.label), |
| 740 |
painted, |
| 741 |
below(area, used + rows), |
| 742 |
buf, |
| 743 |
); |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
if let Some(detail) = choice.detail { |
| 755 |
rows += text::draw(detail, style.muted, indented(area, used + rows), buf); |
| 756 |
} |
| 757 |
} |
| 758 |
rows |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
FieldKind::Secret if !held.text().is_empty() => { |
| 765 |
let dots = "*".repeat(held.text().chars().count()); |
| 766 |
text::draw(&dots, well, below(area, used), buf).max(1) |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
_ if held.text().is_empty() => { |
| 779 |
empty_well(style, placeholder, well, focused, below(area, used), buf) |
| 780 |
} |
| 781 |
_ => text::draw(&measured(field, held.text()), well, below(area, used), buf), |
| 782 |
}; |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
match message_of(style, field) { |
| 789 |
Some((text, painted)) => used + text::draw(text, painted, below(area, used), buf), |
| 790 |
None => used, |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
fn range_line(style: &PieceStyle, field: &Field<'_>, value: &str, well: Style) -> Line<'static> { |
| 811 |
let cells = usize::from(style.meter_cells); |
| 812 |
let ends = field |
| 813 |
.min |
| 814 |
.zip(field.max) |
| 815 |
.and_then(|(min, max)| Some((min.parse::<f64>().ok()?, max.parse::<f64>().ok()?))); |
| 816 |
let filled = match (ends, value.parse::<f64>()) { |
| 817 |
(Some((min, max)), Ok(number)) if max > min => { |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
#[expect( |
| 825 |
clippy::cast_possible_truncation, |
| 826 |
clippy::cast_sign_loss, |
| 827 |
reason = "`position_of` returns 0..=1, and the cell count came from a u16" |
| 828 |
)] |
| 829 |
let reached = (field.curve.position_of(number, min, max) * cells as f64) as usize; |
| 830 |
reached.min(cells) |
| 831 |
} |
| 832 |
_ => 0, |
| 833 |
}; |
| 834 |
let bar = format!( |
| 835 |
"{}{}", |
| 836 |
style.meter_full.to_string().repeat(filled), |
| 837 |
style.meter_empty.to_string().repeat(cells - filled) |
| 838 |
); |
| 839 |
Line::from(vec![ |
| 840 |
Span::styled(format!("{} ", field.min.unwrap_or_default()), style.muted), |
| 841 |
Span::styled(bar, well), |
| 842 |
Span::styled(format!(" {}", field.max.unwrap_or_default()), style.muted), |
| 843 |
Span::styled(format!(" {}", measured(field, value)), well), |
| 844 |
]) |
| 845 |
} |
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
fn interval_line( |
| 867 |
style: &PieceStyle, |
| 868 |
field: &Field<'_>, |
| 869 |
held: Held<'_>, |
| 870 |
well: Style, |
| 871 |
) -> Line<'static> { |
| 872 |
let end = |value: &str, fallback: Option<&str>| match (value.is_empty(), fallback) { |
| 873 |
(false, _) => Span::styled(measured(field, value), well), |
| 874 |
(true, Some(bound)) => Span::styled(measured(field, bound), style.muted), |
| 875 |
(true, None) => Span::styled(String::new(), style.muted), |
| 876 |
}; |
| 877 |
Line::from(vec![ |
| 878 |
end(held.text(), field.min), |
| 879 |
Span::styled(" to ", style.secondary), |
| 880 |
end(held.upper(), field.max), |
| 881 |
]) |
| 882 |
} |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
fn unit_of<'a>(field: &Field<'a>) -> Option<&'a str> { |
| 891 |
field.unit.filter(|_| field.kind.measurable()) |
| 892 |
} |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
fn measured(field: &Field<'_>, value: &str) -> String { |
| 900 |
match unit_of(field) { |
| 901 |
Some(unit) => format!("{value} {unit}"), |
| 902 |
None => value.to_owned(), |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
|
| 907 |
fn label_of(style: &PieceStyle, field: &Field<'_>) -> String { |
| 908 |
if field.required { |
| 909 |
format!("{} {}", field.label, style.required_marker) |
| 910 |
} else { |
| 911 |
field.label.to_owned() |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
fn message_of<'a>(style: &PieceStyle, field: &Field<'a>) -> Option<(&'a str, Style)> { |
| 926 |
if let Some(error) = field.error { |
| 927 |
return Some((error, style.danger)); |
| 928 |
} |
| 929 |
if let Some((tone, note)) = field.note { |
| 930 |
return Some((note, style.tone(tone))); |
| 931 |
} |
| 932 |
field.hint.map(|hint| (hint, style.muted)) |
| 933 |
} |
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
fn empty_well( |
| 943 |
style: &PieceStyle, |
| 944 |
placeholder: &str, |
| 945 |
well: Style, |
| 946 |
focused: bool, |
| 947 |
area: Rect, |
| 948 |
buf: &mut Buffer, |
| 949 |
) -> u16 { |
| 950 |
let used = text::draw(placeholder, style.muted, area, buf).max(1); |
| 951 |
if focused |
| 952 |
&& area.height > 0 |
| 953 |
&& area.width > 0 |
| 954 |
&& let Some(cell) = buf.cell_mut((area.x, area.y)) |
| 955 |
{ |
| 956 |
cell.set_style(well); |
| 957 |
} |
| 958 |
used |
| 959 |
} |
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
fn indented(area: Rect, used: u16) -> Rect { |
| 973 |
const MARK: u16 = 4; |
| 974 |
let area = below(area, used); |
| 975 |
Rect { |
| 976 |
x: area.x + MARK.min(area.width), |
| 977 |
width: area.width.saturating_sub(MARK), |
| 978 |
..area |
| 979 |
} |
| 980 |
} |
| 981 |
|
| 982 |
fn below(area: Rect, used: u16) -> Rect { |
| 983 |
let used = used.min(area.height); |
| 984 |
Rect { |
| 985 |
x: area.x, |
| 986 |
y: area.y + used, |
| 987 |
width: area.width, |
| 988 |
height: area.height - used, |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
#[cfg(test)] |
| 993 |
mod tests { |
| 994 |
|
| 995 |
#[test] |
| 996 |
fn one_line_takes_the_error_then_the_note_then_the_hint() { |
| 997 |
|
| 998 |
|
| 999 |
let style = PieceStyle::default(); |
| 1000 |
let mut f = Field::new(FieldKind::Text, "title", "Title"); |
| 1001 |
f.hint = Some("how it works"); |
| 1002 |
assert_eq!(message_of(&style, &f).unwrap().0, "how it works"); |
| 1003 |
|
| 1004 |
f.note = Some((Tone::Warning, "what it costs")); |
| 1005 |
assert_eq!(message_of(&style, &f).unwrap().0, "what it costs"); |
| 1006 |
assert_eq!(message_of(&style, &f).unwrap().1, style.warning); |
| 1007 |
|
| 1008 |
f.error = Some("what is wrong"); |
| 1009 |
assert_eq!(message_of(&style, &f).unwrap().0, "what is wrong"); |
| 1010 |
assert_eq!(message_of(&style, &f).unwrap().1, style.danger); |
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
f.error = None; |
| 1015 |
f.note = Some((Tone::Neutral, "an ordinary fact")); |
| 1016 |
assert_eq!(message_of(&style, &f).unwrap().1, style.content); |
| 1017 |
} |
| 1018 |
use super::*; |
| 1019 |
use makeover_layout::{Choice, State}; |
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
fn style() -> PieceStyle { |
| 1024 |
PieceStyle { |
| 1025 |
content: Style::new().add_modifier(Modifier::BOLD), |
| 1026 |
secondary: Style::new().add_modifier(Modifier::ITALIC), |
| 1027 |
muted: Style::new().add_modifier(Modifier::DIM), |
| 1028 |
danger: Style::new().add_modifier(Modifier::CROSSED_OUT), |
| 1029 |
..PieceStyle::default() |
| 1030 |
} |
| 1031 |
} |
| 1032 |
|
| 1033 |
fn buffer(width: u16, height: u16) -> Buffer { |
| 1034 |
Buffer::empty(Rect::new(0, 0, width, height)) |
| 1035 |
} |
| 1036 |
|
| 1037 |
|
| 1038 |
fn rows(buf: &Buffer) -> Vec<String> { |
| 1039 |
(0..buf.area.height) |
| 1040 |
.map(|y| { |
| 1041 |
(0..buf.area.width) |
| 1042 |
.map(|x| { |
| 1043 |
buf.cell((x, y)) |
| 1044 |
.map_or(' ', |c| c.symbol().chars().next().unwrap_or(' ')) |
| 1045 |
}) |
| 1046 |
.collect::<String>() |
| 1047 |
.trim_end() |
| 1048 |
.to_owned() |
| 1049 |
}) |
| 1050 |
.collect() |
| 1051 |
} |
| 1052 |
|
| 1053 |
#[test] |
| 1054 |
fn a_bar_fills_in_proportion_and_reads_out_the_two_numbers() { |
| 1055 |
let style = style(); |
| 1056 |
let line = meter(&style, &Meter::new(3, 10).label("subtasks")); |
| 1057 |
let drawn: String = line.spans.iter().map(|s| s.content.as_ref()).collect(); |
| 1058 |
assert_eq!(drawn, "###------- 3/10 subtasks"); |
| 1059 |
|
| 1060 |
|
| 1061 |
let bare = meter(&style, &Meter::new(3, 10)); |
| 1062 |
let drawn: String = bare.spans.iter().map(|s| s.content.as_ref()).collect(); |
| 1063 |
assert_eq!(drawn, "###------- 3/10"); |
| 1064 |
} |
| 1065 |
|
| 1066 |
#[test] |
| 1067 |
fn an_empty_set_is_an_empty_bar_rather_than_a_divide_by_zero() { |
| 1068 |
|
| 1069 |
|
| 1070 |
let line = meter(&style(), &Meter::new(0, 0)); |
| 1071 |
let drawn: String = line.spans.iter().map(|s| s.content.as_ref()).collect(); |
| 1072 |
assert_eq!(drawn, "---------- 0/0"); |
| 1073 |
} |
| 1074 |
|
| 1075 |
#[test] |
| 1076 |
fn an_over_run_fills_the_bar_and_still_reports_the_overflow() { |
| 1077 |
|
| 1078 |
|
| 1079 |
let line = meter(&style(), &Meter::new(14, 10)); |
| 1080 |
let drawn: String = line.spans.iter().map(|s| s.content.as_ref()).collect(); |
| 1081 |
assert_eq!(drawn, "########## 14/10"); |
| 1082 |
} |
| 1083 |
|
| 1084 |
#[test] |
| 1085 |
fn a_badge_is_round_and_a_chip_is_square() { |
| 1086 |
|
| 1087 |
|
| 1088 |
let style = style(); |
| 1089 |
let badge = token(&style, "draft", Token::Badge, Tone::Neutral, false, false); |
| 1090 |
assert_eq!(badge.content.as_ref(), "(draft)"); |
| 1091 |
let chip = token( |
| 1092 |
&style, |
| 1093 |
"rust", |
| 1094 |
Token::Chip { removable: false }, |
| 1095 |
Tone::Neutral, |
| 1096 |
false, |
| 1097 |
false, |
| 1098 |
); |
| 1099 |
assert_eq!(chip.content.as_ref(), "[rust]"); |
| 1100 |
} |
| 1101 |
|
| 1102 |
#[test] |
| 1103 |
fn a_latched_chip_reads_the_same_as_a_focused_one() { |
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
let style = style(); |
| 1108 |
let kind = Token::Chip { removable: false }; |
| 1109 |
let latched = token(&style, "rust", kind, Tone::Neutral, true, false); |
| 1110 |
let focused = token(&style, "rust", kind, Tone::Neutral, false, true); |
| 1111 |
assert_eq!(latched.style, focused.style); |
| 1112 |
assert!(latched.style.add_modifier.contains(Modifier::REVERSED)); |
| 1113 |
} |
| 1114 |
|
| 1115 |
#[test] |
| 1116 |
fn a_control_draws_its_key_only_where_one_was_named() { |
| 1117 |
let style = style(); |
| 1118 |
let line = act(&style, &Act::new("Delete"), false); |
| 1119 |
assert_eq!(line.spans[0].content.as_ref(), "< Delete >"); |
| 1120 |
let line = act(&style, &Act::new("Quit").key("q"), false); |
| 1121 |
assert_eq!(line.spans[0].content.as_ref(), "< Quit > (q)"); |
| 1122 |
} |
| 1123 |
|
| 1124 |
#[test] |
| 1125 |
fn a_disabled_control_is_never_marked_focused() { |
| 1126 |
|
| 1127 |
|
| 1128 |
let style = style(); |
| 1129 |
let disabled = Act::new("Save").state(State::Disabled); |
| 1130 |
let line = act(&style, &disabled, true); |
| 1131 |
assert!( |
| 1132 |
!line.spans[0] |
| 1133 |
.style |
| 1134 |
.add_modifier |
| 1135 |
.contains(Modifier::REVERSED) |
| 1136 |
); |
| 1137 |
assert_eq!(line.spans[0].style, style.muted); |
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
let unstated = Act::new("Save"); |
| 1142 |
let line = act(&style, &unstated, true); |
| 1143 |
assert!( |
| 1144 |
line.spans[0] |
| 1145 |
.style |
| 1146 |
.add_modifier |
| 1147 |
.contains(Modifier::REVERSED) |
| 1148 |
); |
| 1149 |
} |
| 1150 |
|
| 1151 |
#[test] |
| 1152 |
fn a_danger_control_keeps_its_tone_under_focus() { |
| 1153 |
|
| 1154 |
|
| 1155 |
let style = style(); |
| 1156 |
let line = act(&style, &Act::new("Delete").tone(Tone::Danger), true); |
| 1157 |
assert_eq!( |
| 1158 |
line.spans[0].style.add_modifier, |
| 1159 |
style.danger.add_modifier | Modifier::REVERSED |
| 1160 |
); |
| 1161 |
} |
| 1162 |
|
| 1163 |
#[test] |
| 1164 |
fn a_figure_puts_the_number_over_what_it_counts() { |
| 1165 |
let style = style(); |
| 1166 |
let figure_ = Figure::new("42", "open tasks"); |
| 1167 |
let mut buf = buffer(20, 4); |
| 1168 |
let used = figure(&style, &figure_, buf.area, &mut buf); |
| 1169 |
assert_eq!(used, 2); |
| 1170 |
assert_eq!(rows(&buf)[..2], ["42".to_owned(), "open tasks".to_owned()]); |
| 1171 |
assert_eq!(figure_height(&figure_, 20), 2); |
| 1172 |
} |
| 1173 |
|
| 1174 |
#[test] |
| 1175 |
fn a_figures_change_rides_on_the_value_row() { |
| 1176 |
|
| 1177 |
|
| 1178 |
let style = style(); |
| 1179 |
let figure_ = Figure::new("42", "open tasks") |
| 1180 |
.change("+3") |
| 1181 |
.tone(Tone::Success); |
| 1182 |
let mut buf = buffer(20, 4); |
| 1183 |
figure(&style, &figure_, buf.area, &mut buf); |
| 1184 |
assert_eq!(rows(&buf)[0], "42 +3"); |
| 1185 |
} |
| 1186 |
|
| 1187 |
#[test] |
| 1188 |
fn a_compulsory_field_says_so_in_its_label() { |
| 1189 |
let style = style(); |
| 1190 |
let mut field_ = Field::new(FieldKind::Text, "email", "Email"); |
| 1191 |
field_.required = true; |
| 1192 |
let mut buf = buffer(20, 4); |
| 1193 |
field(&style, &field_, Held::Absent, false, buf.area, &mut buf); |
| 1194 |
assert_eq!(rows(&buf)[0], "Email *"); |
| 1195 |
} |
| 1196 |
|
| 1197 |
#[test] |
| 1198 |
fn a_hidden_field_costs_no_rows_at_all() { |
| 1199 |
|
| 1200 |
let style = style(); |
| 1201 |
let field_ = Field::new(FieldKind::Hidden, "csrf", "Token"); |
| 1202 |
let mut buf = buffer(20, 4); |
| 1203 |
assert_eq!( |
| 1204 |
field( |
| 1205 |
&style, |
| 1206 |
&field_, |
| 1207 |
Held::Text("abc"), |
| 1208 |
false, |
| 1209 |
buf.area, |
| 1210 |
&mut buf |
| 1211 |
), |
| 1212 |
0 |
| 1213 |
); |
| 1214 |
assert_eq!(field_height(&style, &field_, 20), 0); |
| 1215 |
assert_eq!(rows(&buf)[0], ""); |
| 1216 |
} |
| 1217 |
|
| 1218 |
#[test] |
| 1219 |
fn a_secret_is_dotted_from_the_callers_buffer_and_never_from_the_description() { |
| 1220 |
|
| 1221 |
|
| 1222 |
let style = style(); |
| 1223 |
let field_ = Field::new(FieldKind::Secret, "password", "Password"); |
| 1224 |
let mut buf = buffer(20, 4); |
| 1225 |
field( |
| 1226 |
&style, |
| 1227 |
&field_, |
| 1228 |
Held::Text("hunter2"), |
| 1229 |
false, |
| 1230 |
buf.area, |
| 1231 |
&mut buf, |
| 1232 |
); |
| 1233 |
assert_eq!(rows(&buf)[1], "*******"); |
| 1234 |
} |
| 1235 |
|
| 1236 |
#[test] |
| 1237 |
fn an_error_takes_the_row_the_hint_would_have_had() { |
| 1238 |
|
| 1239 |
|
| 1240 |
let style = style(); |
| 1241 |
let mut field_ = Field::new(FieldKind::Text, "email", "Email"); |
| 1242 |
field_.hint = Some("work address"); |
| 1243 |
field_.error = Some("not an address"); |
| 1244 |
let mut buf = buffer(20, 5); |
| 1245 |
field( |
| 1246 |
&style, |
| 1247 |
&field_, |
| 1248 |
Held::Text("nope"), |
| 1249 |
false, |
| 1250 |
buf.area, |
| 1251 |
&mut buf, |
| 1252 |
); |
| 1253 |
assert_eq!(rows(&buf)[2], "not an address"); |
| 1254 |
assert_eq!(field_height(&style, &field_, 20), 3); |
| 1255 |
} |
| 1256 |
|
| 1257 |
#[test] |
| 1258 |
fn a_focused_empty_box_shows_where_the_typing_will_land() { |
| 1259 |
|
| 1260 |
|
| 1261 |
let style = style(); |
| 1262 |
let field_ = Field::new(FieldKind::Text, "email", "Email"); |
| 1263 |
let mut buf = buffer(20, 4); |
| 1264 |
field(&style, &field_, Held::Absent, true, buf.area, &mut buf); |
| 1265 |
let caret = buf.cell((0, 1)).expect("the well's first cell").style(); |
| 1266 |
assert!(caret.add_modifier.contains(Modifier::REVERSED)); |
| 1267 |
} |
| 1268 |
|
| 1269 |
#[test] |
| 1270 |
fn a_choice_field_marks_the_chosen_option_and_costs_a_row_each() { |
| 1271 |
let style = style(); |
| 1272 |
let mut field_ = Field::new(FieldKind::Radio, "size", "Size"); |
| 1273 |
let options = [Choice::plain("small"), Choice::plain("large")]; |
| 1274 |
field_.options = &options; |
| 1275 |
let mut buf = buffer(20, 5); |
| 1276 |
field( |
| 1277 |
&style, |
| 1278 |
&field_, |
| 1279 |
Held::Text("large"), |
| 1280 |
false, |
| 1281 |
buf.area, |
| 1282 |
&mut buf, |
| 1283 |
); |
| 1284 |
assert_eq!(rows(&buf)[1], "( ) small"); |
| 1285 |
assert_eq!(rows(&buf)[2], "(*) large"); |
| 1286 |
assert_eq!(field_height(&style, &field_, 20), 3); |
| 1287 |
} |
| 1288 |
|
| 1289 |
#[test] |
| 1290 |
fn a_range_draws_its_two_ends_and_where_the_value_sits_between_them() { |
| 1291 |
let style = style(); |
| 1292 |
let field_ = Field::range("review", "Review above", "0", "1"); |
| 1293 |
let mut buf = buffer(40, 3); |
| 1294 |
field( |
| 1295 |
&style, |
| 1296 |
&field_, |
| 1297 |
Held::Text("0.5"), |
| 1298 |
false, |
| 1299 |
buf.area, |
| 1300 |
&mut buf, |
| 1301 |
); |
| 1302 |
|
| 1303 |
|
| 1304 |
assert_eq!(rows(&buf)[1].trim_end(), "0 #####----- 1 0.5"); |
| 1305 |
assert_eq!(field_height(&style, &field_, 40), 2); |
| 1306 |
} |
| 1307 |
|
| 1308 |
#[test] |
| 1309 |
fn a_unit_rides_on_the_value_and_not_on_the_label() { |
| 1310 |
|
| 1311 |
let style = style(); |
| 1312 |
let field_ = Field { |
| 1313 |
unit: Some("s"), |
| 1314 |
..Field::range("attack", "Attack", "0", "5") |
| 1315 |
}; |
| 1316 |
let mut buf = buffer(40, 3); |
| 1317 |
field( |
| 1318 |
&style, |
| 1319 |
&field_, |
| 1320 |
Held::Text("2.5"), |
| 1321 |
false, |
| 1322 |
buf.area, |
| 1323 |
&mut buf, |
| 1324 |
); |
| 1325 |
assert_eq!(rows(&buf)[0].trim_end(), "Attack"); |
| 1326 |
assert_eq!(rows(&buf)[1].trim_end(), "0 #####----- 5 2.5 s"); |
| 1327 |
} |
| 1328 |
|
| 1329 |
#[test] |
| 1330 |
fn a_typed_number_reads_with_its_unit_too() { |
| 1331 |
let style = style(); |
| 1332 |
let field_ = Field { |
| 1333 |
unit: Some("ms"), |
| 1334 |
..Field::new(FieldKind::Number, "fade", "Fade") |
| 1335 |
}; |
| 1336 |
let mut buf = buffer(40, 3); |
| 1337 |
field(&style, &field_, Held::Text("50"), false, buf.area, &mut buf); |
| 1338 |
assert_eq!(rows(&buf)[1].trim_end(), "50 ms"); |
| 1339 |
} |
| 1340 |
|
| 1341 |
#[test] |
| 1342 |
fn a_unit_on_a_kind_that_is_not_a_quantity_is_ignored() { |
| 1343 |
|
| 1344 |
|
| 1345 |
let style = style(); |
| 1346 |
let field_ = Field { |
| 1347 |
unit: Some("s"), |
| 1348 |
..Field::new(FieldKind::Text, "name", "Name") |
| 1349 |
}; |
| 1350 |
let mut buf = buffer(40, 3); |
| 1351 |
field( |
| 1352 |
&style, |
| 1353 |
&field_, |
| 1354 |
Held::Text("kick"), |
| 1355 |
false, |
| 1356 |
buf.area, |
| 1357 |
&mut buf, |
| 1358 |
); |
| 1359 |
assert_eq!(rows(&buf)[1].trim_end(), "kick"); |
| 1360 |
} |
| 1361 |
|
| 1362 |
#[test] |
| 1363 |
fn an_interval_is_one_line_with_both_ends_on_it() { |
| 1364 |
|
| 1365 |
|
| 1366 |
let style = style(); |
| 1367 |
let field_ = Field { |
| 1368 |
min: Some("0"), |
| 1369 |
max: Some("300"), |
| 1370 |
unit: Some("BPM"), |
| 1371 |
..Field::interval("bpm_min", "bpm_max", "BPM range") |
| 1372 |
}; |
| 1373 |
let mut buf = buffer(40, 3); |
| 1374 |
field( |
| 1375 |
&style, |
| 1376 |
&field_, |
| 1377 |
Held::Between { |
| 1378 |
lower: "90", |
| 1379 |
upper: "130", |
| 1380 |
}, |
| 1381 |
false, |
| 1382 |
buf.area, |
| 1383 |
&mut buf, |
| 1384 |
); |
| 1385 |
assert_eq!(rows(&buf)[0].trim_end(), "BPM range"); |
| 1386 |
assert_eq!(rows(&buf)[1].trim_end(), "90 BPM to 130 BPM"); |
| 1387 |
assert_eq!(rows(&buf)[2].trim_end(), ""); |
| 1388 |
} |
| 1389 |
|
| 1390 |
#[test] |
| 1391 |
fn an_open_end_falls_back_to_the_bound_it_means() { |
| 1392 |
|
| 1393 |
|
| 1394 |
let style = style(); |
| 1395 |
let field_ = Field { |
| 1396 |
min: Some("0"), |
| 1397 |
max: Some("300"), |
| 1398 |
..Field::interval("bpm_min", "bpm_max", "BPM range") |
| 1399 |
}; |
| 1400 |
let mut buf = buffer(40, 3); |
| 1401 |
field( |
| 1402 |
&style, |
| 1403 |
&field_, |
| 1404 |
Held::Between { |
| 1405 |
lower: "120", |
| 1406 |
upper: "", |
| 1407 |
}, |
| 1408 |
false, |
| 1409 |
buf.area, |
| 1410 |
&mut buf, |
| 1411 |
); |
| 1412 |
assert_eq!(rows(&buf)[1].trim_end(), "120 to 300"); |
| 1413 |
} |
| 1414 |
|
| 1415 |
#[test] |
| 1416 |
fn an_unbounded_open_end_draws_nothing_rather_than_a_number() { |
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
let style = style(); |
| 1421 |
let field_ = Field::interval("bpm_min", "bpm_max", "BPM range"); |
| 1422 |
let mut buf = buffer(40, 3); |
| 1423 |
field( |
| 1424 |
&style, |
| 1425 |
&field_, |
| 1426 |
Held::Between { |
| 1427 |
lower: "", |
| 1428 |
upper: "130", |
| 1429 |
}, |
| 1430 |
false, |
| 1431 |
buf.area, |
| 1432 |
&mut buf, |
| 1433 |
); |
| 1434 |
assert_eq!(rows(&buf)[1].trim_end(), "to 130"); |
| 1435 |
} |
| 1436 |
|
| 1437 |
#[test] |
| 1438 |
fn a_range_holding_something_unreadable_still_shows_it() { |
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
let style = style(); |
| 1443 |
let field_ = Field::range("review", "Review above", "0", "1"); |
| 1444 |
let mut buf = buffer(40, 3); |
| 1445 |
field( |
| 1446 |
&style, |
| 1447 |
&field_, |
| 1448 |
Held::Text("unset"), |
| 1449 |
false, |
| 1450 |
buf.area, |
| 1451 |
&mut buf, |
| 1452 |
); |
| 1453 |
assert_eq!(rows(&buf)[1].trim_end(), "0 ---------- 1 unset"); |
| 1454 |
} |
| 1455 |
|
| 1456 |
#[test] |
| 1457 |
fn an_unbounded_range_is_typed_into_rather_than_dragged() { |
| 1458 |
|
| 1459 |
|
| 1460 |
let style = style(); |
| 1461 |
let field_ = Field { |
| 1462 |
max: Some("1"), |
| 1463 |
..Field::new(FieldKind::Range, "review", "Review above") |
| 1464 |
}; |
| 1465 |
let mut buf = buffer(40, 3); |
| 1466 |
field( |
| 1467 |
&style, |
| 1468 |
&field_, |
| 1469 |
Held::Text("0.5"), |
| 1470 |
false, |
| 1471 |
buf.area, |
| 1472 |
&mut buf, |
| 1473 |
); |
| 1474 |
assert_eq!(rows(&buf)[1].trim_end(), "0.5"); |
| 1475 |
} |
| 1476 |
|
| 1477 |
#[test] |
| 1478 |
fn an_unavailable_option_reads_as_inert_and_says_why() { |
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
let style = style(); |
| 1483 |
let options = [ |
| 1484 |
Choice::new("chromatic", "Chromatic"), |
| 1485 |
Choice::new("multi", "Multi-sample").unless("Drop a second sample."), |
| 1486 |
]; |
| 1487 |
let mut field_ = Field::new(FieldKind::Radio, "mode", "Mode"); |
| 1488 |
field_.options = &options; |
| 1489 |
let mut buf = buffer(46, 4); |
| 1490 |
field( |
| 1491 |
&style, |
| 1492 |
&field_, |
| 1493 |
Held::Text("chromatic"), |
| 1494 |
false, |
| 1495 |
buf.area, |
| 1496 |
&mut buf, |
| 1497 |
); |
| 1498 |
assert_eq!(rows(&buf)[1].trim_end(), "(*) Chromatic"); |
| 1499 |
assert_eq!( |
| 1500 |
rows(&buf)[2].trim_end(), |
| 1501 |
"( ) Multi-sample: Drop a second sample." |
| 1502 |
); |
| 1503 |
let muted = buf.cell((0, 2)).expect("the unavailable row").style(); |
| 1504 |
assert!(muted.add_modifier.contains(Modifier::DIM)); |
| 1505 |
} |
| 1506 |
|
| 1507 |
#[test] |
| 1508 |
fn an_option_can_carry_the_line_that_says_what_it_means() { |
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
let style = style(); |
| 1513 |
let options = [ |
| 1514 |
Choice::new("16", "Basic").detailing("$16/mo. Fits text, blogs, newsletters."), |
| 1515 |
Choice::new("24", "Small Files"), |
| 1516 |
]; |
| 1517 |
let mut field_ = Field::new(FieldKind::Radio, "tier", "Tier"); |
| 1518 |
field_.options = &options; |
| 1519 |
let mut buf = buffer(46, 5); |
| 1520 |
field(&style, &field_, Held::Text("16"), false, buf.area, &mut buf); |
| 1521 |
|
| 1522 |
let drawn = rows(&buf); |
| 1523 |
assert_eq!(drawn[1].trim_end(), "(*) Basic"); |
| 1524 |
assert_eq!( |
| 1525 |
drawn[2].trim_end(), |
| 1526 |
" $16/mo. Fits text, blogs, newsletters." |
| 1527 |
); |
| 1528 |
|
| 1529 |
|
| 1530 |
assert_eq!(drawn[3].trim_end(), "( ) Small Files"); |
| 1531 |
let muted = buf.cell((4, 2)).expect("the detail row").style(); |
| 1532 |
assert!(muted.add_modifier.contains(Modifier::DIM)); |
| 1533 |
} |
| 1534 |
|
| 1535 |
#[test] |
| 1536 |
fn an_unchosen_option_does_not_read_as_disabled() { |
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
let style = style(); |
| 1541 |
let mut field_ = Field::new(FieldKind::Radio, "size", "Size"); |
| 1542 |
let options = [Choice::plain("small"), Choice::plain("large")]; |
| 1543 |
field_.options = &options; |
| 1544 |
let mut buf = buffer(20, 5); |
| 1545 |
field( |
| 1546 |
&style, |
| 1547 |
&field_, |
| 1548 |
Held::Text("large"), |
| 1549 |
false, |
| 1550 |
buf.area, |
| 1551 |
&mut buf, |
| 1552 |
); |
| 1553 |
let unchosen = buf.cell((0, 1)).expect("the first option").style(); |
| 1554 |
assert_eq!(unchosen.add_modifier, style.secondary.add_modifier); |
| 1555 |
assert_ne!(unchosen.add_modifier, style.muted.add_modifier); |
| 1556 |
} |
| 1557 |
|
| 1558 |
#[test] |
| 1559 |
fn a_checkbox_reads_a_bool_rather_than_a_submitted_string() { |
| 1560 |
|
| 1561 |
|
| 1562 |
let style = style(); |
| 1563 |
let field_ = Field::new(FieldKind::Checkbox, "agree", "Agree"); |
| 1564 |
let mut buf = buffer(20, 4); |
| 1565 |
field(&style, &field_, Held::On(true), false, buf.area, &mut buf); |
| 1566 |
assert_eq!(rows(&buf)[1], "[x]"); |
| 1567 |
let mut buf = buffer(20, 4); |
| 1568 |
field(&style, &field_, Held::On(false), false, buf.area, &mut buf); |
| 1569 |
assert_eq!(rows(&buf)[1], "[ ]"); |
| 1570 |
} |
| 1571 |
|
| 1572 |
#[test] |
| 1573 |
fn a_markdown_field_gets_the_rows_a_textarea_does() { |
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
let style = PieceStyle::default(); |
| 1578 |
let rich = Field::new(FieldKind::Rich, "body", "Body"); |
| 1579 |
let textarea = Field::new(FieldKind::Textarea, "body", "Body"); |
| 1580 |
let plain = Field::new(FieldKind::Text, "body", "Body"); |
| 1581 |
|
| 1582 |
assert_eq!( |
| 1583 |
field_height(&style, &rich, 40), |
| 1584 |
field_height(&style, &textarea, 40) |
| 1585 |
); |
| 1586 |
assert!(field_height(&style, &rich, 40) > field_height(&style, &plain, 40)); |
| 1587 |
} |
| 1588 |
|
| 1589 |
#[test] |
| 1590 |
fn a_tone_and_a_heading_map_without_a_fallback_arm() { |
| 1591 |
|
| 1592 |
|
| 1593 |
let style = style(); |
| 1594 |
assert_eq!(style.tone(Tone::Neutral), style.content); |
| 1595 |
assert_eq!(style.tone(Tone::Danger), style.danger); |
| 1596 |
assert_eq!(style.heading(Heading::Page), style.page); |
| 1597 |
assert_eq!(style.heading(Heading::Subsection), style.subsection); |
| 1598 |
} |
| 1599 |
|
| 1600 |
#[test] |
| 1601 |
fn the_default_style_carries_no_colour_at_all() { |
| 1602 |
|
| 1603 |
|
| 1604 |
let style = PieceStyle::default(); |
| 1605 |
for painted in [style.content, style.danger, style.page, style.action] { |
| 1606 |
assert_eq!(painted.fg, None); |
| 1607 |
assert_eq!(painted.bg, None); |
| 1608 |
} |
| 1609 |
} |
| 1610 |
|
| 1611 |
#[test] |
| 1612 |
fn the_three_states_of_a_wait_are_three_drawings() { |
| 1613 |
|
| 1614 |
|
| 1615 |
let style = PieceStyle::default(); |
| 1616 |
let bare = awaiting(&style, Awaiting::unmeasured(), Progress::default(), true); |
| 1617 |
let sized = awaiting(&style, Awaiting::of(41_943_040), Progress::default(), true); |
| 1618 |
let watched = awaiting( |
| 1619 |
&style, |
| 1620 |
Awaiting::of(40), |
| 1621 |
Progress { |
| 1622 |
delivered: Some(20), |
| 1623 |
elapsed: Some(Duration::from_secs(4)), |
| 1624 |
}, |
| 1625 |
true, |
| 1626 |
); |
| 1627 |
let read = |line: &Line<'_>| { |
| 1628 |
line.spans |
| 1629 |
.iter() |
| 1630 |
.map(|s| s.content.to_string()) |
| 1631 |
.collect::<String>() |
| 1632 |
}; |
| 1633 |
assert_eq!(read(&bare), "#"); |
| 1634 |
assert_eq!(read(&sized), "# 41943040"); |
| 1635 |
assert_eq!(read(&watched), "#####----- 20/40 4s"); |
| 1636 |
} |
| 1637 |
|
| 1638 |
#[test] |
| 1639 |
fn a_dark_mark_still_occupies_its_cell() { |
| 1640 |
|
| 1641 |
|
| 1642 |
let style = PieceStyle::default(); |
| 1643 |
assert_eq!(activity(&style, true).content.chars().count(), 1); |
| 1644 |
assert_eq!(activity(&style, false).content.chars().count(), 1); |
| 1645 |
} |
| 1646 |
|
| 1647 |
#[test] |
| 1648 |
fn an_over_delivered_wait_clamps_and_does_not_panic() { |
| 1649 |
|
| 1650 |
|
| 1651 |
let style = PieceStyle::default(); |
| 1652 |
let over = awaiting( |
| 1653 |
&style, |
| 1654 |
Awaiting::of(4), |
| 1655 |
Progress { |
| 1656 |
delivered: Some(9), |
| 1657 |
elapsed: None, |
| 1658 |
}, |
| 1659 |
true, |
| 1660 |
); |
| 1661 |
assert!(over.spans[0].content.chars().all(|c| c == '#')); |
| 1662 |
assert_eq!(over.spans[0].content.chars().count(), 10); |
| 1663 |
|
| 1664 |
let empty = awaiting( |
| 1665 |
&style, |
| 1666 |
Awaiting::of(0), |
| 1667 |
Progress { |
| 1668 |
delivered: Some(9), |
| 1669 |
elapsed: None, |
| 1670 |
}, |
| 1671 |
true, |
| 1672 |
); |
| 1673 |
assert!(empty.spans[0].content.starts_with('-')); |
| 1674 |
} |
| 1675 |
|
| 1676 |
#[test] |
| 1677 |
fn a_theme_picker_heads_each_group_and_marks_each_tier() { |
| 1678 |
const THEMES: &[makeover_layout::ThemeChoice<'_>] = &[ |
| 1679 |
makeover_layout::ThemeChoice::new( |
| 1680 |
"goingson", |
| 1681 |
"GoingsOn", |
| 1682 |
ThemeVariant::Light, |
| 1683 |
makeover_layout::Contrast::High, |
| 1684 |
), |
| 1685 |
makeover_layout::ThemeChoice::new( |
| 1686 |
"carbonfox", |
| 1687 |
"Carbonfox", |
| 1688 |
ThemeVariant::Dark, |
| 1689 |
makeover_layout::Contrast::Standard, |
| 1690 |
), |
| 1691 |
]; |
| 1692 |
let style = style(); |
| 1693 |
let field_ = Field::theme("theme", "Theme", THEMES) |
| 1694 |
.following(makeover_layout::Choice::new("system", "Follow System")); |
| 1695 |
let mut buf = buffer(32, 8); |
| 1696 |
field( |
| 1697 |
&style, |
| 1698 |
&field_, |
| 1699 |
Held::Text("carbonfox"), |
| 1700 |
false, |
| 1701 |
buf.area, |
| 1702 |
&mut buf, |
| 1703 |
); |
| 1704 |
|
| 1705 |
let rows = rows(&buf); |
| 1706 |
assert_eq!(rows[1], "( ) Follow System"); |
| 1707 |
assert_eq!(rows[2], "Light"); |
| 1708 |
assert_eq!(rows[3], "( ) GoingsOn [AA]"); |
| 1709 |
assert_eq!(rows[4], "Dark"); |
| 1710 |
assert_eq!(rows[5], "(*) Carbonfox [OK]"); |
| 1711 |
} |
| 1712 |
|
| 1713 |
#[test] |
| 1714 |
fn a_theme_picker_asks_for_the_rows_it_draws() { |
| 1715 |
|
| 1716 |
|
| 1717 |
const THEMES: &[makeover_layout::ThemeChoice<'_>] = &[ |
| 1718 |
makeover_layout::ThemeChoice::new( |
| 1719 |
"goingson", |
| 1720 |
"GoingsOn", |
| 1721 |
ThemeVariant::Light, |
| 1722 |
makeover_layout::Contrast::High, |
| 1723 |
), |
| 1724 |
makeover_layout::ThemeChoice::new( |
| 1725 |
"carbonfox", |
| 1726 |
"Carbonfox", |
| 1727 |
ThemeVariant::Dark, |
| 1728 |
makeover_layout::Contrast::Standard, |
| 1729 |
), |
| 1730 |
]; |
| 1731 |
let style = style(); |
| 1732 |
let field_ = Field::theme("theme", "Theme", THEMES) |
| 1733 |
.following(makeover_layout::Choice::new("system", "Follow System")); |
| 1734 |
assert_eq!(field_height(&style, &field_, 32), 6); |
| 1735 |
|
| 1736 |
|
| 1737 |
let one = Field::theme("theme", "Theme", &THEMES[..1]); |
| 1738 |
assert_eq!(field_height(&style, &one, 32), 3); |
| 1739 |
} |
| 1740 |
} |
| 1741 |
|