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