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