| 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 |
use makeover_layout::{Act, Field, FieldKind, Figure, Heading, Meter, Token, Tone}; |
| 32 |
use ratatui::buffer::Buffer; |
| 33 |
use ratatui::layout::Rect; |
| 34 |
use ratatui::style::{Modifier, Style}; |
| 35 |
use ratatui::text::{Line, Span}; |
| 36 |
|
| 37 |
use crate::text; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 54 |
pub struct WidgetStyle { |
| 55 |
|
| 56 |
pub content: Style, |
| 57 |
|
| 58 |
pub secondary: Style, |
| 59 |
|
| 60 |
pub muted: Style, |
| 61 |
|
| 62 |
pub info: Style, |
| 63 |
|
| 64 |
pub success: Style, |
| 65 |
|
| 66 |
pub warning: Style, |
| 67 |
|
| 68 |
pub danger: Style, |
| 69 |
|
| 70 |
pub page: Style, |
| 71 |
|
| 72 |
pub section: Style, |
| 73 |
|
| 74 |
pub subsection: Style, |
| 75 |
|
| 76 |
pub action: Style, |
| 77 |
|
| 78 |
|
| 79 |
pub filled: Style, |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
pub sunken: Style, |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
pub focus: Modifier, |
| 90 |
|
| 91 |
pub meter_cells: u16, |
| 92 |
|
| 93 |
pub meter_full: char, |
| 94 |
|
| 95 |
pub meter_empty: char, |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
pub required_marker: &'static str, |
| 101 |
} |
| 102 |
|
| 103 |
impl Default for WidgetStyle { |
| 104 |
|
| 105 |
|
| 106 |
fn default() -> Self { |
| 107 |
Self { |
| 108 |
content: Style::new(), |
| 109 |
secondary: Style::new(), |
| 110 |
muted: Style::new().add_modifier(Modifier::DIM), |
| 111 |
info: Style::new(), |
| 112 |
success: Style::new(), |
| 113 |
warning: Style::new(), |
| 114 |
danger: Style::new().add_modifier(Modifier::BOLD), |
| 115 |
page: Style::new().add_modifier(Modifier::BOLD), |
| 116 |
section: Style::new().add_modifier(Modifier::BOLD), |
| 117 |
subsection: Style::new(), |
| 118 |
action: Style::new().add_modifier(Modifier::UNDERLINED), |
| 119 |
filled: Style::new().add_modifier(Modifier::REVERSED), |
| 120 |
sunken: Style::new().add_modifier(Modifier::DIM), |
| 121 |
focus: Modifier::REVERSED, |
| 122 |
meter_cells: 10, |
| 123 |
meter_full: '#', |
| 124 |
meter_empty: '-', |
| 125 |
required_marker: "*", |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
impl WidgetStyle { |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
#[cfg(feature = "theme")] |
| 138 |
#[must_use] |
| 139 |
pub fn from_theme(theme: &crate::Theme) -> Self { |
| 140 |
Self { |
| 141 |
content: Style::new().fg(theme.content_primary), |
| 142 |
secondary: Style::new().fg(theme.content_secondary), |
| 143 |
muted: Style::new().fg(theme.content_muted), |
| 144 |
info: Style::new().fg(theme.status_info), |
| 145 |
success: Style::new().fg(theme.status_success), |
| 146 |
warning: Style::new().fg(theme.status_warning), |
| 147 |
danger: Style::new().fg(theme.status_danger), |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
page: Style::new() |
| 155 |
.fg(theme.action_primary) |
| 156 |
.add_modifier(Modifier::BOLD), |
| 157 |
section: Style::new() |
| 158 |
.fg(theme.content_primary) |
| 159 |
.add_modifier(Modifier::BOLD), |
| 160 |
subsection: Style::new().fg(theme.content_secondary), |
| 161 |
action: Style::new().fg(theme.action_primary), |
| 162 |
filled: Style::new().fg(theme.selection_on).bg(theme.action_primary), |
| 163 |
sunken: Style::new().bg(theme.surface_sunken), |
| 164 |
focus: Modifier::REVERSED, |
| 165 |
meter_cells: 10, |
| 166 |
meter_full: '#', |
| 167 |
meter_empty: '-', |
| 168 |
required_marker: "*", |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
#[must_use] |
| 177 |
pub const fn tone(&self, tone: Tone) -> Style { |
| 178 |
match tone { |
| 179 |
Tone::Neutral => self.content, |
| 180 |
Tone::Info => self.info, |
| 181 |
Tone::Success => self.success, |
| 182 |
Tone::Warning => self.warning, |
| 183 |
Tone::Danger => self.danger, |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
#[must_use] |
| 189 |
pub const fn heading(&self, level: Heading) -> Style { |
| 190 |
match level { |
| 191 |
Heading::Page => self.page, |
| 192 |
Heading::Section => self.section, |
| 193 |
Heading::Subsection => self.subsection, |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
#[must_use] |
| 202 |
pub fn focused(&self, focused: bool, style: Style) -> Style { |
| 203 |
if focused { |
| 204 |
style.add_modifier(self.focus) |
| 205 |
} else { |
| 206 |
style |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] |
| 222 |
pub enum Held<'a> { |
| 223 |
|
| 224 |
#[default] |
| 225 |
Absent, |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
Text(&'a str), |
| 230 |
|
| 231 |
On(bool), |
| 232 |
} |
| 233 |
|
| 234 |
impl<'a> Held<'a> { |
| 235 |
|
| 236 |
#[must_use] |
| 237 |
pub const fn text(self) -> &'a str { |
| 238 |
match self { |
| 239 |
Self::Text(text) => text, |
| 240 |
Self::Absent | Self::On(_) => "", |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
#[must_use] |
| 246 |
pub const fn on(self) -> bool { |
| 247 |
matches!(self, Self::On(true)) |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
#[must_use] |
| 257 |
pub fn meter(style: &WidgetStyle, meter: &Meter<'_>) -> Line<'static> { |
| 258 |
let cells = u32::from(style.meter_cells); |
| 259 |
let filled = meter |
| 260 |
.done |
| 261 |
.checked_mul(cells) |
| 262 |
.and_then(|reached| reached.checked_div(meter.total)) |
| 263 |
.unwrap_or(0) |
| 264 |
.min(cells); |
| 265 |
let bar = format!( |
| 266 |
"{}{}", |
| 267 |
style.meter_full.to_string().repeat(filled as usize), |
| 268 |
style |
| 269 |
.meter_empty |
| 270 |
.to_string() |
| 271 |
.repeat((cells - filled) as usize) |
| 272 |
); |
| 273 |
let reading = match meter.label { |
| 274 |
Some(label) => format!(" {}/{} {label}", meter.done, meter.total), |
| 275 |
None => format!(" {}/{}", meter.done, meter.total), |
| 276 |
}; |
| 277 |
Line::from(vec![ |
| 278 |
Span::styled(bar, style.tone(meter.tone)), |
| 279 |
Span::styled(reading, style.muted), |
| 280 |
]) |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
#[must_use] |
| 300 |
pub fn token( |
| 301 |
style: &WidgetStyle, |
| 302 |
label: &str, |
| 303 |
kind: Token, |
| 304 |
tone: Tone, |
| 305 |
latched: bool, |
| 306 |
focused: bool, |
| 307 |
) -> Span<'static> { |
| 308 |
let painted = style.tone(tone); |
| 309 |
let painted = if latched { |
| 310 |
painted.add_modifier(style.focus) |
| 311 |
} else { |
| 312 |
style.focused(focused, painted) |
| 313 |
}; |
| 314 |
match kind { |
| 315 |
Token::Badge => Span::styled(format!("({label})"), painted), |
| 316 |
Token::Chip { .. } => Span::styled(format!("[{label}]"), painted), |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
#[must_use] |
| 331 |
pub fn act(style: &WidgetStyle, act: &Act<'_>, focused: bool) -> Line<'static> { |
| 332 |
let painted = if act.disabled() { |
| 333 |
style.muted |
| 334 |
} else { |
| 335 |
style.focused(focused, style.tone(act.tone)) |
| 336 |
}; |
| 337 |
let label = match act.key { |
| 338 |
Some(key) => format!("< {} > ({key})", act.label), |
| 339 |
None => format!("< {} >", act.label), |
| 340 |
}; |
| 341 |
Line::from(Span::styled(label, painted)) |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
#[must_use] |
| 350 |
pub fn filled_act(style: &WidgetStyle, label: &str, focused: bool) -> Line<'static> { |
| 351 |
Line::from(Span::styled( |
| 352 |
format!("[ {label} ]"), |
| 353 |
style.focused(focused, style.filled), |
| 354 |
)) |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
#[must_use] |
| 359 |
pub fn figure_height(figure: &Figure<'_>, width: u16) -> u16 { |
| 360 |
text::height(figure.value, width) + text::height(figure.caption, width) |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
pub fn figure(style: &WidgetStyle, figure: &Figure<'_>, area: Rect, buf: &mut Buffer) -> u16 { |
| 369 |
let value = match figure.change { |
| 370 |
Some(change) => format!("{} {change}", figure.value), |
| 371 |
None => figure.value.to_owned(), |
| 372 |
}; |
| 373 |
let used = text::draw( |
| 374 |
&value, |
| 375 |
style.tone(figure.tone).add_modifier(Modifier::BOLD), |
| 376 |
area, |
| 377 |
buf, |
| 378 |
); |
| 379 |
used + text::draw(figure.caption, style.muted, below(area, used), buf) |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
#[must_use] |
| 388 |
pub fn field_height(style: &WidgetStyle, field: &Field<'_>, width: u16) -> u16 { |
| 389 |
if !field.kind.visible() { |
| 390 |
return 0; |
| 391 |
} |
| 392 |
let label = text::height(&label_of(style, field), width); |
| 393 |
let body = match field.kind { |
| 394 |
FieldKind::Textarea => 3, |
| 395 |
kind if kind.offers_options() => u16::try_from(field.options.len()).unwrap_or(u16::MAX), |
| 396 |
_ => 1, |
| 397 |
}; |
| 398 |
let note = note_of(field).map_or(0, |note| text::height(note, width)); |
| 399 |
label + body + note |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
pub fn field( |
| 410 |
style: &WidgetStyle, |
| 411 |
field: &Field<'_>, |
| 412 |
held: Held<'_>, |
| 413 |
focused: bool, |
| 414 |
area: Rect, |
| 415 |
buf: &mut Buffer, |
| 416 |
) -> u16 { |
| 417 |
|
| 418 |
|
| 419 |
if !field.kind.visible() || area.width == 0 || area.height == 0 { |
| 420 |
return 0; |
| 421 |
} |
| 422 |
|
| 423 |
let mut used = text::draw(&label_of(style, field), style.secondary, area, buf); |
| 424 |
|
| 425 |
let well = style.focused(focused, style.content); |
| 426 |
let placeholder = field.placeholder.unwrap_or_default(); |
| 427 |
|
| 428 |
used += match field.kind { |
| 429 |
FieldKind::Checkbox => text::draw( |
| 430 |
if held.on() { "[x]" } else { "[ ]" }, |
| 431 |
well, |
| 432 |
below(area, used), |
| 433 |
buf, |
| 434 |
), |
| 435 |
kind if kind.offers_options() => { |
| 436 |
let mut rows = 0; |
| 437 |
for choice in field.options { |
| 438 |
let chosen = held.text() == choice.value; |
| 439 |
let mark = if chosen { "(*)" } else { "( )" }; |
| 440 |
rows += text::draw( |
| 441 |
&format!("{mark} {}", choice.label), |
| 442 |
if chosen { well } else { style.muted }, |
| 443 |
below(area, used + rows), |
| 444 |
buf, |
| 445 |
); |
| 446 |
} |
| 447 |
rows |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
FieldKind::Secret if !held.text().is_empty() => { |
| 454 |
let dots = "*".repeat(held.text().chars().count()); |
| 455 |
text::draw(&dots, well, below(area, used), buf).max(1) |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
_ if held.text().is_empty() => { |
| 461 |
empty_well(style, placeholder, well, focused, below(area, used), buf) |
| 462 |
} |
| 463 |
_ => text::draw(held.text(), well, below(area, used), buf), |
| 464 |
}; |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
match note_of(field) { |
| 470 |
Some(note) => { |
| 471 |
let painted = if field.error.is_some() { |
| 472 |
style.danger |
| 473 |
} else { |
| 474 |
style.muted |
| 475 |
}; |
| 476 |
used + text::draw(note, painted, below(area, used), buf) |
| 477 |
} |
| 478 |
None => used, |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
fn label_of(style: &WidgetStyle, field: &Field<'_>) -> String { |
| 484 |
if field.required { |
| 485 |
format!("{} {}", field.label, style.required_marker) |
| 486 |
} else { |
| 487 |
field.label.to_owned() |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
fn note_of<'a>(field: &Field<'a>) -> Option<&'a str> { |
| 493 |
field.error.or(field.hint) |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
fn empty_well( |
| 504 |
style: &WidgetStyle, |
| 505 |
placeholder: &str, |
| 506 |
well: Style, |
| 507 |
focused: bool, |
| 508 |
area: Rect, |
| 509 |
buf: &mut Buffer, |
| 510 |
) -> u16 { |
| 511 |
let used = text::draw(placeholder, style.muted, area, buf).max(1); |
| 512 |
if focused |
| 513 |
&& area.height > 0 |
| 514 |
&& area.width > 0 |
| 515 |
&& let Some(cell) = buf.cell_mut((area.x, area.y)) |
| 516 |
{ |
| 517 |
cell.set_style(well); |
| 518 |
} |
| 519 |
used |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
fn below(area: Rect, used: u16) -> Rect { |
| 524 |
let used = used.min(area.height); |
| 525 |
Rect { |
| 526 |
x: area.x, |
| 527 |
y: area.y + used, |
| 528 |
width: area.width, |
| 529 |
height: area.height - used, |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
#[cfg(test)] |
| 534 |
mod tests { |
| 535 |
use super::*; |
| 536 |
use makeover_layout::{Choice, State}; |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
fn style() -> WidgetStyle { |
| 541 |
WidgetStyle { |
| 542 |
content: Style::new().add_modifier(Modifier::BOLD), |
| 543 |
muted: Style::new().add_modifier(Modifier::DIM), |
| 544 |
danger: Style::new().add_modifier(Modifier::CROSSED_OUT), |
| 545 |
..WidgetStyle::default() |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
fn buffer(width: u16, height: u16) -> Buffer { |
| 550 |
Buffer::empty(Rect::new(0, 0, width, height)) |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
fn rows(buf: &Buffer) -> Vec<String> { |
| 555 |
(0..buf.area.height) |
| 556 |
.map(|y| { |
| 557 |
(0..buf.area.width) |
| 558 |
.map(|x| { |
| 559 |
buf.cell((x, y)) |
| 560 |
.map_or(' ', |c| c.symbol().chars().next().unwrap_or(' ')) |
| 561 |
}) |
| 562 |
.collect::<String>() |
| 563 |
.trim_end() |
| 564 |
.to_owned() |
| 565 |
}) |
| 566 |
.collect() |
| 567 |
} |
| 568 |
|
| 569 |
#[test] |
| 570 |
fn a_bar_fills_in_proportion_and_reads_out_the_two_numbers() { |
| 571 |
let style = style(); |
| 572 |
let line = meter(&style, &Meter::new(3, 10).label("subtasks")); |
| 573 |
let drawn: String = line.spans.iter().map(|s| s.content.as_ref()).collect(); |
| 574 |
assert_eq!(drawn, "###------- 3/10 subtasks"); |
| 575 |
|
| 576 |
|
| 577 |
let bare = meter(&style, &Meter::new(3, 10)); |
| 578 |
let drawn: String = bare.spans.iter().map(|s| s.content.as_ref()).collect(); |
| 579 |
assert_eq!(drawn, "###------- 3/10"); |
| 580 |
} |
| 581 |
|
| 582 |
#[test] |
| 583 |
fn an_empty_set_is_an_empty_bar_rather_than_a_divide_by_zero() { |
| 584 |
|
| 585 |
|
| 586 |
let line = meter(&style(), &Meter::new(0, 0)); |
| 587 |
let drawn: String = line.spans.iter().map(|s| s.content.as_ref()).collect(); |
| 588 |
assert_eq!(drawn, "---------- 0/0"); |
| 589 |
} |
| 590 |
|
| 591 |
#[test] |
| 592 |
fn an_over_run_fills_the_bar_and_still_reports_the_overflow() { |
| 593 |
|
| 594 |
|
| 595 |
let line = meter(&style(), &Meter::new(14, 10)); |
| 596 |
let drawn: String = line.spans.iter().map(|s| s.content.as_ref()).collect(); |
| 597 |
assert_eq!(drawn, "########## 14/10"); |
| 598 |
} |
| 599 |
|
| 600 |
#[test] |
| 601 |
fn a_badge_is_round_and_a_chip_is_square() { |
| 602 |
|
| 603 |
|
| 604 |
let style = style(); |
| 605 |
let badge = token(&style, "draft", Token::Badge, Tone::Neutral, false, false); |
| 606 |
assert_eq!(badge.content.as_ref(), "(draft)"); |
| 607 |
let chip = token( |
| 608 |
&style, |
| 609 |
"rust", |
| 610 |
Token::Chip { removable: false }, |
| 611 |
Tone::Neutral, |
| 612 |
false, |
| 613 |
false, |
| 614 |
); |
| 615 |
assert_eq!(chip.content.as_ref(), "[rust]"); |
| 616 |
} |
| 617 |
|
| 618 |
#[test] |
| 619 |
fn a_latched_chip_reads_the_same_as_a_focused_one() { |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
let style = style(); |
| 624 |
let kind = Token::Chip { removable: false }; |
| 625 |
let latched = token(&style, "rust", kind, Tone::Neutral, true, false); |
| 626 |
let focused = token(&style, "rust", kind, Tone::Neutral, false, true); |
| 627 |
assert_eq!(latched.style, focused.style); |
| 628 |
assert!(latched.style.add_modifier.contains(Modifier::REVERSED)); |
| 629 |
} |
| 630 |
|
| 631 |
#[test] |
| 632 |
fn a_control_draws_its_key_only_where_one_was_named() { |
| 633 |
let style = style(); |
| 634 |
let line = act(&style, &Act::new("Delete"), false); |
| 635 |
assert_eq!(line.spans[0].content.as_ref(), "< Delete >"); |
| 636 |
let line = act(&style, &Act::new("Quit").key("q"), false); |
| 637 |
assert_eq!(line.spans[0].content.as_ref(), "< Quit > (q)"); |
| 638 |
} |
| 639 |
|
| 640 |
#[test] |
| 641 |
fn a_disabled_control_is_never_marked_focused() { |
| 642 |
|
| 643 |
|
| 644 |
let style = style(); |
| 645 |
let disabled = Act::new("Save").state(State::Disabled); |
| 646 |
let line = act(&style, &disabled, true); |
| 647 |
assert!( |
| 648 |
!line.spans[0] |
| 649 |
.style |
| 650 |
.add_modifier |
| 651 |
.contains(Modifier::REVERSED) |
| 652 |
); |
| 653 |
assert_eq!(line.spans[0].style, style.muted); |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
let unstated = Act::new("Save"); |
| 658 |
let line = act(&style, &unstated, true); |
| 659 |
assert!( |
| 660 |
line.spans[0] |
| 661 |
.style |
| 662 |
.add_modifier |
| 663 |
.contains(Modifier::REVERSED) |
| 664 |
); |
| 665 |
} |
| 666 |
|
| 667 |
#[test] |
| 668 |
fn a_danger_control_keeps_its_tone_under_focus() { |
| 669 |
|
| 670 |
|
| 671 |
let style = style(); |
| 672 |
let line = act(&style, &Act::new("Delete").tone(Tone::Danger), true); |
| 673 |
assert_eq!( |
| 674 |
line.spans[0].style.add_modifier, |
| 675 |
style.danger.add_modifier | Modifier::REVERSED |
| 676 |
); |
| 677 |
} |
| 678 |
|
| 679 |
#[test] |
| 680 |
fn a_figure_puts_the_number_over_what_it_counts() { |
| 681 |
let style = style(); |
| 682 |
let figure_ = Figure::new("42", "open tasks"); |
| 683 |
let mut buf = buffer(20, 4); |
| 684 |
let used = figure(&style, &figure_, buf.area, &mut buf); |
| 685 |
assert_eq!(used, 2); |
| 686 |
assert_eq!(rows(&buf)[..2], ["42".to_owned(), "open tasks".to_owned()]); |
| 687 |
assert_eq!(figure_height(&figure_, 20), 2); |
| 688 |
} |
| 689 |
|
| 690 |
#[test] |
| 691 |
fn a_figures_change_rides_on_the_value_row() { |
| 692 |
|
| 693 |
|
| 694 |
let style = style(); |
| 695 |
let figure_ = Figure::new("42", "open tasks") |
| 696 |
.change("+3") |
| 697 |
.tone(Tone::Success); |
| 698 |
let mut buf = buffer(20, 4); |
| 699 |
figure(&style, &figure_, buf.area, &mut buf); |
| 700 |
assert_eq!(rows(&buf)[0], "42 +3"); |
| 701 |
} |
| 702 |
|
| 703 |
#[test] |
| 704 |
fn a_compulsory_field_says_so_in_its_label() { |
| 705 |
let style = style(); |
| 706 |
let mut field_ = Field::new(FieldKind::Text, "email", "Email"); |
| 707 |
field_.required = true; |
| 708 |
let mut buf = buffer(20, 4); |
| 709 |
field(&style, &field_, Held::Absent, false, buf.area, &mut buf); |
| 710 |
assert_eq!(rows(&buf)[0], "Email *"); |
| 711 |
} |
| 712 |
|
| 713 |
#[test] |
| 714 |
fn a_hidden_field_costs_no_rows_at_all() { |
| 715 |
|
| 716 |
let style = style(); |
| 717 |
let field_ = Field::new(FieldKind::Hidden, "csrf", "Token"); |
| 718 |
let mut buf = buffer(20, 4); |
| 719 |
assert_eq!( |
| 720 |
field( |
| 721 |
&style, |
| 722 |
&field_, |
| 723 |
Held::Text("abc"), |
| 724 |
false, |
| 725 |
buf.area, |
| 726 |
&mut buf |
| 727 |
), |
| 728 |
0 |
| 729 |
); |
| 730 |
assert_eq!(field_height(&style, &field_, 20), 0); |
| 731 |
assert_eq!(rows(&buf)[0], ""); |
| 732 |
} |
| 733 |
|
| 734 |
#[test] |
| 735 |
fn a_secret_is_dotted_from_the_callers_buffer_and_never_from_the_description() { |
| 736 |
|
| 737 |
|
| 738 |
let style = style(); |
| 739 |
let field_ = Field::new(FieldKind::Secret, "password", "Password"); |
| 740 |
let mut buf = buffer(20, 4); |
| 741 |
field( |
| 742 |
&style, |
| 743 |
&field_, |
| 744 |
Held::Text("hunter2"), |
| 745 |
false, |
| 746 |
buf.area, |
| 747 |
&mut buf, |
| 748 |
); |
| 749 |
assert_eq!(rows(&buf)[1], "*******"); |
| 750 |
} |
| 751 |
|
| 752 |
#[test] |
| 753 |
fn an_error_takes_the_row_the_hint_would_have_had() { |
| 754 |
|
| 755 |
|
| 756 |
let style = style(); |
| 757 |
let mut field_ = Field::new(FieldKind::Text, "email", "Email"); |
| 758 |
field_.hint = Some("work address"); |
| 759 |
field_.error = Some("not an address"); |
| 760 |
let mut buf = buffer(20, 5); |
| 761 |
field( |
| 762 |
&style, |
| 763 |
&field_, |
| 764 |
Held::Text("nope"), |
| 765 |
false, |
| 766 |
buf.area, |
| 767 |
&mut buf, |
| 768 |
); |
| 769 |
assert_eq!(rows(&buf)[2], "not an address"); |
| 770 |
assert_eq!(field_height(&style, &field_, 20), 3); |
| 771 |
} |
| 772 |
|
| 773 |
#[test] |
| 774 |
fn a_focused_empty_box_shows_where_the_typing_will_land() { |
| 775 |
|
| 776 |
|
| 777 |
let style = style(); |
| 778 |
let field_ = Field::new(FieldKind::Text, "email", "Email"); |
| 779 |
let mut buf = buffer(20, 4); |
| 780 |
field(&style, &field_, Held::Absent, true, buf.area, &mut buf); |
| 781 |
let caret = buf.cell((0, 1)).expect("the well's first cell").style(); |
| 782 |
assert!(caret.add_modifier.contains(Modifier::REVERSED)); |
| 783 |
} |
| 784 |
|
| 785 |
#[test] |
| 786 |
fn a_choice_field_marks_the_chosen_option_and_costs_a_row_each() { |
| 787 |
let style = style(); |
| 788 |
let mut field_ = Field::new(FieldKind::Radio, "size", "Size"); |
| 789 |
let options = [Choice::plain("small"), Choice::plain("large")]; |
| 790 |
field_.options = &options; |
| 791 |
let mut buf = buffer(20, 5); |
| 792 |
field( |
| 793 |
&style, |
| 794 |
&field_, |
| 795 |
Held::Text("large"), |
| 796 |
false, |
| 797 |
buf.area, |
| 798 |
&mut buf, |
| 799 |
); |
| 800 |
assert_eq!(rows(&buf)[1], "( ) small"); |
| 801 |
assert_eq!(rows(&buf)[2], "(*) large"); |
| 802 |
assert_eq!(field_height(&style, &field_, 20), 3); |
| 803 |
} |
| 804 |
|
| 805 |
#[test] |
| 806 |
fn a_checkbox_reads_a_bool_rather_than_a_submitted_string() { |
| 807 |
|
| 808 |
|
| 809 |
let style = style(); |
| 810 |
let field_ = Field::new(FieldKind::Checkbox, "agree", "Agree"); |
| 811 |
let mut buf = buffer(20, 4); |
| 812 |
field(&style, &field_, Held::On(true), false, buf.area, &mut buf); |
| 813 |
assert_eq!(rows(&buf)[1], "[x]"); |
| 814 |
let mut buf = buffer(20, 4); |
| 815 |
field(&style, &field_, Held::On(false), false, buf.area, &mut buf); |
| 816 |
assert_eq!(rows(&buf)[1], "[ ]"); |
| 817 |
} |
| 818 |
|
| 819 |
#[test] |
| 820 |
fn a_tone_and_a_heading_map_without_a_fallback_arm() { |
| 821 |
|
| 822 |
|
| 823 |
let style = style(); |
| 824 |
assert_eq!(style.tone(Tone::Neutral), style.content); |
| 825 |
assert_eq!(style.tone(Tone::Danger), style.danger); |
| 826 |
assert_eq!(style.heading(Heading::Page), style.page); |
| 827 |
assert_eq!(style.heading(Heading::Subsection), style.subsection); |
| 828 |
} |
| 829 |
|
| 830 |
#[test] |
| 831 |
fn the_default_style_carries_no_colour_at_all() { |
| 832 |
|
| 833 |
|
| 834 |
let style = WidgetStyle::default(); |
| 835 |
for painted in [style.content, style.danger, style.page, style.action] { |
| 836 |
assert_eq!(painted.fg, None); |
| 837 |
assert_eq!(painted.bg, None); |
| 838 |
} |
| 839 |
} |
| 840 |
} |
| 841 |
|