| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
use makeover_layout as layout; |
| 8 |
use makeover_tui::{piece, table, text}; |
| 9 |
use quasi_router::{Act, Cell, Cells, Field, Figure, Meter, Node, Part, Picture, Row, Tag}; |
| 10 |
use ratatui::buffer::Buffer; |
| 11 |
use ratatui::layout::Rect; |
| 12 |
use ratatui::style::{Modifier, Style}; |
| 13 |
use ratatui::text::{Line, Span}; |
| 14 |
|
| 15 |
use crate::{Pass, Tui, View, below}; |
| 16 |
|
| 17 |
|
| 18 |
pub(crate) fn height(tui: &Tui, node: &Node, width: u16) -> u16 { |
| 19 |
match node { |
| 20 |
Node::Heading { text: content, .. } | Node::Text { text: content, .. } => { |
| 21 |
text::height(content, width) |
| 22 |
} |
| 23 |
Node::Rich { source } => { |
| 24 |
text::spans_height(&rich_spans(tui, source, rich_base(tui)), width) |
| 25 |
} |
| 26 |
Node::Act(act) => text::line_height(&act_line(tui, act, false), width), |
| 27 |
Node::Link { text: label, .. } => text::height(label, width), |
| 28 |
Node::Token(tag) => text::line_height(&Line::from(tag_span(tui, tag, false)), width), |
| 29 |
Node::Figure(figure) => figure_height(tui, figure, width), |
| 30 |
Node::Image(picture) => image_height(picture, width), |
| 31 |
Node::Notice { text: content, .. } => text::height(content, width), |
| 32 |
Node::StandIn { message, act, .. } => { |
| 33 |
text::height(message, width) + act.as_ref().map_or(0, |_| 1) |
| 34 |
} |
| 35 |
Node::Field(field) => field_height(tui, field, width), |
| 36 |
Node::Form { fields, .. } => { |
| 37 |
fields |
| 38 |
.iter() |
| 39 |
.map(|field| field_height(tui, field, width)) |
| 40 |
.sum::<u16>() |
| 41 |
|
| 42 |
+ 1 |
| 43 |
} |
| 44 |
Node::List { rows, more } => { |
| 45 |
let gutter = list_gutter(rows); |
| 46 |
rows.iter() |
| 47 |
.map(|row| { |
| 48 |
text::line_height(&row_line(tui, row, &[]), width.saturating_sub(gutter)).max(1) |
| 49 |
}) |
| 50 |
.sum::<u16>() |
| 51 |
+ u16::from(more.is_some()) |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
Node::Timeline { entries, .. } => entries |
| 66 |
.iter() |
| 67 |
.map(|entry| { |
| 68 |
text::line_height( |
| 69 |
&row_line(tui, &entry.row, &[]), |
| 70 |
width.saturating_sub(TIMELINE_GUTTER), |
| 71 |
) |
| 72 |
.max(1) |
| 73 |
}) |
| 74 |
.sum::<u16>(), |
| 75 |
Node::Table { columns, rows } => table_height(columns, rows), |
| 76 |
Node::Select { options, .. } => { |
| 77 |
text::line_height(&Line::from(select_spans(tui, options, None, &[])), width) |
| 78 |
} |
| 79 |
Node::Meter(meter) => text::line_height(&meter_line(tui, meter), width), |
| 80 |
Node::Stats { figures } => figures |
| 81 |
.iter() |
| 82 |
.map(|(figure, _)| figure_height(tui, figure, width)) |
| 83 |
.sum(), |
| 84 |
Node::Region(slot) => crate::region::height(tui, slot, width), |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
pub(crate) fn draw(pass: &mut Pass<'_>, node: &Node, area: Rect, buf: &mut Buffer) -> u16 { |
| 96 |
if area.width == 0 || area.height == 0 { |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
count(pass, node); |
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
let tui = pass.tui; |
| 106 |
match node { |
| 107 |
Node::Heading { level, text: title } => { |
| 108 |
text::draw(title, tui.style().heading(*level), area, buf) |
| 109 |
} |
| 110 |
|
| 111 |
Node::Text { |
| 112 |
text: content, |
| 113 |
tone, |
| 114 |
} => text::draw(content, tui.style().tone(*tone), area, buf), |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
Node::Rich { source } => { |
| 126 |
text::draw_spans(&rich_spans(tui, source, rich_base(tui)), area, buf) |
| 127 |
} |
| 128 |
|
| 129 |
Node::Act(act) => { |
| 130 |
let focused = claim_act(pass, act); |
| 131 |
text::draw_line(&act_line(tui, act, focused), area, buf) |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
Node::Link { text: label, .. } => { |
| 139 |
let focused = pass.claim(); |
| 140 |
text::draw( |
| 141 |
label, |
| 142 |
tui.style().focused(focused, link_style(tui)), |
| 143 |
area, |
| 144 |
buf, |
| 145 |
) |
| 146 |
} |
| 147 |
|
| 148 |
Node::Token(tag) => { |
| 149 |
let focused = claim_tag(pass, tag); |
| 150 |
text::draw_line(&Line::from(tag_span(tui, tag, focused)), area, buf) |
| 151 |
} |
| 152 |
|
| 153 |
Node::Figure(figure) => draw_figure(tui, figure, area, buf), |
| 154 |
|
| 155 |
Node::Image(picture) => draw_image(tui, picture, area, buf), |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
Node::Notice { |
| 162 |
tone, |
| 163 |
text: content, |
| 164 |
.. |
| 165 |
} => { |
| 166 |
let style = tui.style().tone(*tone).add_modifier(Modifier::BOLD); |
| 167 |
text::draw(content, style, area, buf) |
| 168 |
} |
| 169 |
|
| 170 |
Node::StandIn { |
| 171 |
state, |
| 172 |
message, |
| 173 |
act, |
| 174 |
} => { |
| 175 |
let style = match state { |
| 176 |
layout::Readiness::Failed => tui.style().tone(layout::Tone::Danger), |
| 177 |
_ => Style::default().fg(tui.theme().content_muted), |
| 178 |
}; |
| 179 |
let used = text::draw(message, style, area, buf); |
| 180 |
match act { |
| 181 |
Some(act) => { |
| 182 |
let focused = claim_act(pass, act); |
| 183 |
used + text::draw_line(&act_line(tui, act, focused), below(area, used), buf) |
| 184 |
} |
| 185 |
None => used, |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
Node::Field(field) => draw_field(pass, field, area, buf), |
| 190 |
|
| 191 |
Node::Form { submit, fields, .. } => { |
| 192 |
let mut used = 0; |
| 193 |
for field in fields { |
| 194 |
used += draw_field(pass, field, below(area, used), buf); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
let focused = pass.claim(); |
| 200 |
used + text::draw_line( |
| 201 |
&Line::from(vec![Span::styled( |
| 202 |
format!("[ {submit} ]"), |
| 203 |
tui.style().focused( |
| 204 |
focused, |
| 205 |
Style::default() |
| 206 |
.fg(tui.theme().selection_on) |
| 207 |
.bg(tui.theme().action_primary), |
| 208 |
), |
| 209 |
)]), |
| 210 |
below(area, used), |
| 211 |
buf, |
| 212 |
) |
| 213 |
} |
| 214 |
|
| 215 |
Node::List { rows, more } => { |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
let gutter = list_gutter(rows); |
| 222 |
let mut used = 0; |
| 223 |
for row in rows { |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
let focused = claim_row(pass, row); |
| 228 |
let parts = claim_parts(pass, row); |
| 229 |
let line = row_line(tui, row, &parts); |
| 230 |
let at = below(area, used); |
| 231 |
if at.height == 0 { |
| 232 |
continue; |
| 233 |
} |
| 234 |
draw_gutter(tui, pass.view, row, focused, at, buf); |
| 235 |
let body = Rect { |
| 236 |
x: at.x + gutter, |
| 237 |
width: at.width.saturating_sub(gutter), |
| 238 |
..at |
| 239 |
}; |
| 240 |
used += text::draw_line(&line, body, buf).max(1); |
| 241 |
} |
| 242 |
match more { |
| 243 |
Some(rest) => { |
| 244 |
let focused = pass.claim(); |
| 245 |
let label = rest.remaining.map_or_else( |
| 246 |
|| "More".to_string(), |
| 247 |
|remaining| format!("{remaining} more"), |
| 248 |
); |
| 249 |
used + text::draw( |
| 250 |
&label, |
| 251 |
tui.style() |
| 252 |
.focused(focused, Style::default().fg(tui.theme().action_primary)), |
| 253 |
below(area, used), |
| 254 |
buf, |
| 255 |
) |
| 256 |
} |
| 257 |
None => used, |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
Node::Timeline { track, entries, .. } => { |
| 272 |
let mut used = 0; |
| 273 |
for entry in entries { |
| 274 |
let focused = claim_row(pass, &entry.row); |
| 275 |
let parts = claim_parts(pass, &entry.row); |
| 276 |
let line = row_line(tui, &entry.row, &parts); |
| 277 |
let at = below(area, used); |
| 278 |
if at.height == 0 { |
| 279 |
continue; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
let minute = entry.placement.at(); |
| 286 |
let clock = format!("{:02}:{:02} ", (minute / 60) % 24, minute % 60); |
| 287 |
let style = if focused { |
| 288 |
tui.style().muted.add_modifier(Modifier::REVERSED) |
| 289 |
} else { |
| 290 |
tui.style().muted |
| 291 |
}; |
| 292 |
buf.set_span( |
| 293 |
at.x, |
| 294 |
at.y, |
| 295 |
&Span::styled(clock, style), |
| 296 |
TIMELINE_GUTTER.min(at.width), |
| 297 |
); |
| 298 |
|
| 299 |
let body = Rect { |
| 300 |
x: at.x + TIMELINE_GUTTER, |
| 301 |
width: at.width.saturating_sub(TIMELINE_GUTTER), |
| 302 |
..at |
| 303 |
}; |
| 304 |
used += text::draw_line(&line, body, buf).max(1); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
let _ = track; |
| 312 |
used |
| 313 |
} |
| 314 |
Node::Table { columns, rows } => draw_table(pass, columns, rows, area, buf), |
| 315 |
|
| 316 |
Node::Select { |
| 317 |
options, |
| 318 |
chosen, |
| 319 |
action, |
| 320 |
.. |
| 321 |
} => { |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
let reachable: Vec<bool> = options |
| 327 |
.iter() |
| 328 |
.map(|(_, own)| { |
| 329 |
let calls = own.is_some() || action.is_some(); |
| 330 |
calls && pass.claim() |
| 331 |
}) |
| 332 |
.collect(); |
| 333 |
text::draw_line( |
| 334 |
&Line::from(select_spans(tui, options, chosen.as_deref(), &reachable)), |
| 335 |
area, |
| 336 |
buf, |
| 337 |
) |
| 338 |
} |
| 339 |
|
| 340 |
Node::Meter(meter) => text::draw_line(&meter_line(tui, meter), area, buf), |
| 341 |
|
| 342 |
Node::Stats { figures } => { |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
let mut used = 0; |
| 349 |
for (figure, _) in figures { |
| 350 |
used += draw_figure(tui, figure, below(area, used), buf); |
| 351 |
} |
| 352 |
used |
| 353 |
} |
| 354 |
|
| 355 |
Node::Region(slot) => crate::region::draw(pass, slot, area, buf), |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
fn count(pass: &mut Pass<'_>, node: &Node) { |
| 362 |
let mut found = Vec::new(); |
| 363 |
|
| 364 |
crate::focus::node_spots(node, "", &mut found); |
| 365 |
pass.seq += found.len(); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
fn claim_act(pass: &mut Pass<'_>, act: &Act) -> bool { |
| 370 |
!act.state.is_some_and(layout::State::suppresses_interaction) && pass.claim() |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
fn claim_tag(pass: &mut Pass<'_>, tag: &Tag) -> bool { |
| 375 |
matches!(tag.kind, layout::Token::Chip { .. }) && tag.action.is_some() && pass.claim() |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
fn claim_row(pass: &mut Pass<'_>, row: &Row) -> bool { |
| 380 |
let reachable = row.activate.is_some() |
| 381 |
|| row.toggle.is_some() |
| 382 |
|| row.selected.is_some() |
| 383 |
|| !row.menu.is_empty(); |
| 384 |
reachable && pass.claim() |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
fn claim_parts(pass: &mut Pass<'_>, row: &Row) -> Vec<bool> { |
| 389 |
row.parts |
| 390 |
.iter() |
| 391 |
.map(|Part { node, .. }| match node { |
| 392 |
Node::Act(act) => claim_act(pass, act), |
| 393 |
Node::Link { .. } => pass.claim(), |
| 394 |
Node::Token(tag) => claim_tag(pass, tag), |
| 395 |
_ => false, |
| 396 |
}) |
| 397 |
.collect() |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
fn link_style(tui: &Tui) -> Style { |
| 402 |
Style::default() |
| 403 |
.fg(tui.theme().action_primary) |
| 404 |
.add_modifier(Modifier::UNDERLINED) |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
const TIMELINE_GUTTER: u16 = 6; |
| 424 |
|
| 425 |
fn list_gutter(rows: &[Row]) -> u16 { |
| 426 |
if rows.iter().any(|row| row.selected.is_some()) { |
| 427 |
4 |
| 428 |
} else if rows |
| 429 |
.iter() |
| 430 |
.any(|row| row.current || row.activate.is_some() || !row.menu.is_empty()) |
| 431 |
{ |
| 432 |
2 |
| 433 |
} else { |
| 434 |
0 |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
fn draw_gutter(tui: &Tui, view: &View, row: &Row, focused: bool, area: Rect, buf: &mut Buffer) { |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
let tick = match (row.selected, row.value.as_deref()) { |
| 452 |
(Some(_), Some(value)) if view.is_ticked(value) => "[x]", |
| 453 |
(Some(_), Some(_)) => "[ ]", |
| 454 |
(Some(true), None) => "[x]", |
| 455 |
(Some(false), None) => "[ ]", |
| 456 |
(None, _) => "", |
| 457 |
}; |
| 458 |
if !tick.is_empty() { |
| 459 |
buf.set_stringn( |
| 460 |
area.x, |
| 461 |
area.y, |
| 462 |
tick, |
| 463 |
3, |
| 464 |
tui.style() |
| 465 |
.focused(focused, Style::default().fg(tui.theme().content_secondary)), |
| 466 |
); |
| 467 |
return; |
| 468 |
} |
| 469 |
if row.current || focused { |
| 470 |
buf.set_stringn( |
| 471 |
area.x, |
| 472 |
area.y, |
| 473 |
">", |
| 474 |
1, |
| 475 |
tui.style() |
| 476 |
.focused(focused, Style::default().fg(tui.theme().action_primary)), |
| 477 |
); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
fn row_line(tui: &Tui, row: &Row, focus: &[bool]) -> Line<'static> { |
| 490 |
let mut spans = Vec::new(); |
| 491 |
for (index, Part { role, node }) in row.parts.iter().enumerate() { |
| 492 |
if !spans.is_empty() { |
| 493 |
spans.push(Span::raw(" ")); |
| 494 |
} |
| 495 |
let focused = focus.get(index).copied().unwrap_or(false); |
| 496 |
spans.extend(inline_spans(tui, node, part_style(tui, *role), focused)); |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
Line::from(spans) |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
fn part_style(tui: &Tui, role: layout::RowPart) -> Style { |
| 506 |
let theme = tui.theme(); |
| 507 |
match role { |
| 508 |
layout::RowPart::Primary => Style::default().fg(theme.content_primary), |
| 509 |
layout::RowPart::Secondary => Style::default().fg(theme.content_secondary), |
| 510 |
layout::RowPart::Meta => Style::default().fg(theme.content_muted), |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
_ => Style::default().fg(theme.content_primary), |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
fn inline_spans(tui: &Tui, node: &Node, inherited: Style, focused: bool) -> Vec<Span<'static>> { |
| 520 |
match node { |
| 521 |
Node::Text { text, tone } => { |
| 522 |
let style = match tone { |
| 523 |
layout::Tone::Neutral => inherited, |
| 524 |
other => tui.style().tone(*other), |
| 525 |
}; |
| 526 |
vec![Span::styled(text.clone(), style)] |
| 527 |
} |
| 528 |
Node::Rich { source } => rich_spans(tui, source, inherited), |
| 529 |
Node::Token(tag) => vec![tag_span(tui, tag, focused)], |
| 530 |
Node::Act(act) => act_line(tui, act, focused).spans, |
| 531 |
Node::Link { text, .. } => vec![Span::styled( |
| 532 |
text.clone(), |
| 533 |
tui.style().focused(focused, link_style(tui)), |
| 534 |
)], |
| 535 |
Node::Meter(meter) => meter_line(tui, meter).spans, |
| 536 |
Node::Figure(figure) => vec![Span::styled( |
| 537 |
format!("{} {}", figure.value, figure.caption), |
| 538 |
inherited, |
| 539 |
)], |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
other => vec![Span::styled( |
| 544 |
format!("{other:?}"), |
| 545 |
Style::default().fg(tui.theme().status_danger), |
| 546 |
)], |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
fn rich_base(tui: &Tui) -> Style { |
| 553 |
Style::default().fg(tui.theme().content_primary) |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
fn rich_spans(tui: &Tui, source: &str, base: Style) -> Vec<Span<'static>> { |
| 563 |
let mut spans = Vec::new(); |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
let mut starting = true; |
| 569 |
for run in docengine::render_runs(source) { |
| 570 |
if starting && let Some(marker) = marker(run.block) { |
| 571 |
spans.push(Span::styled( |
| 572 |
marker, |
| 573 |
Style::default().fg(tui.theme().content_muted), |
| 574 |
)); |
| 575 |
} |
| 576 |
starting = run.text.ends_with('\n'); |
| 577 |
let style = style_of(tui, base, &run); |
| 578 |
spans.push(Span::styled(run.text, style)); |
| 579 |
} |
| 580 |
spans |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
fn marker(block: docengine::Block) -> Option<&'static str> { |
| 589 |
match block { |
| 590 |
docengine::Block::Item => Some("- "), |
| 591 |
docengine::Block::Quote => Some("> "), |
| 592 |
docengine::Block::Prose | docengine::Block::Heading(_) => None, |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
fn style_of(tui: &Tui, base: Style, run: &docengine::TextRun) -> Style { |
| 602 |
let ground = match run.block { |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
docengine::Block::Heading(1) => tui.style().heading(layout::Heading::Page), |
| 607 |
docengine::Block::Heading(2) => tui.style().heading(layout::Heading::Section), |
| 608 |
docengine::Block::Heading(_) => tui.style().heading(layout::Heading::Subsection), |
| 609 |
docengine::Block::Quote => Style::default().fg(tui.theme().content_secondary), |
| 610 |
docengine::Block::Prose | docengine::Block::Item => base, |
| 611 |
}; |
| 612 |
mark(tui, ground, run.emphasis) |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
fn mark(tui: &Tui, base: Style, emphasis: docengine::Emphasis) -> Style { |
| 623 |
if emphasis.is_plain() { |
| 624 |
return base; |
| 625 |
} |
| 626 |
let mut style = base; |
| 627 |
if emphasis.strong { |
| 628 |
style = style.add_modifier(Modifier::BOLD); |
| 629 |
} |
| 630 |
if emphasis.italic { |
| 631 |
style = style.add_modifier(Modifier::ITALIC); |
| 632 |
} |
| 633 |
if emphasis.struck { |
| 634 |
style = style.add_modifier(Modifier::CROSSED_OUT); |
| 635 |
} |
| 636 |
if emphasis.code { |
| 637 |
style = style.bg(tui.theme().surface_sunken); |
| 638 |
} |
| 639 |
style |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
fn tag_span(tui: &Tui, tag: &Tag, focused: bool) -> Span<'static> { |
| 648 |
piece::token( |
| 649 |
tui.style(), |
| 650 |
&tag.label, |
| 651 |
tag.kind, |
| 652 |
tag.tone, |
| 653 |
tag.latched, |
| 654 |
focused, |
| 655 |
) |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
fn act_line(tui: &Tui, act: &Act, focused: bool) -> Line<'static> { |
| 665 |
piece::act(tui.style(), &act.as_layout(), focused) |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
fn meter_line(tui: &Tui, meter: &Meter) -> Line<'static> { |
| 670 |
piece::meter(tui.style(), &meter.as_layout()) |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
fn figure_height(_tui: &Tui, figure: &Figure, width: u16) -> u16 { |
| 675 |
piece::figure_height(&figure.as_layout(), width) |
| 676 |
} |
| 677 |
|
| 678 |
fn draw_figure(tui: &Tui, figure: &Figure, area: Rect, buf: &mut Buffer) -> u16 { |
| 679 |
piece::figure(tui.style(), &figure.as_layout(), area, buf) |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
fn image_height(picture: &Picture, width: u16) -> u16 { |
| 693 |
if !picture.as_layout().speaks() { |
| 694 |
return 0; |
| 695 |
} |
| 696 |
text::height(&picture.alt, width) |
| 697 |
+ picture |
| 698 |
.caption |
| 699 |
.as_ref() |
| 700 |
.map_or(0, |c| text::height(c, width)) |
| 701 |
} |
| 702 |
|
| 703 |
fn draw_image(tui: &Tui, picture: &Picture, area: Rect, buf: &mut Buffer) -> u16 { |
| 704 |
if !picture.as_layout().speaks() { |
| 705 |
return 0; |
| 706 |
} |
| 707 |
|
| 708 |
let used = text::draw(&picture.alt, tui.style().muted, area, buf); |
| 709 |
let Some(caption) = &picture.caption else { |
| 710 |
return used; |
| 711 |
}; |
| 712 |
|
| 713 |
|
| 714 |
used + text::draw(caption, tui.style().secondary, below(area, used), buf) |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
fn field_height(tui: &Tui, field: &Field, width: u16) -> u16 { |
| 720 |
field.with_layout(|field| piece::field_height(tui.style(), &field, width)) |
| 721 |
} |
| 722 |
|
| 723 |
fn draw_field(pass: &mut Pass<'_>, field: &Field, area: Rect, buf: &mut Buffer) -> u16 { |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
if matches!(field.kind, layout::FieldKind::Hidden) { |
| 728 |
return 0; |
| 729 |
} |
| 730 |
let focused = pass.claim(); |
| 731 |
let tui = pass.tui; |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
let held = pass.view.showing(&field.name, field.value.as_deref()); |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
let held = if matches!(field.kind, layout::FieldKind::Checkbox) { |
| 744 |
piece::Held::On(held == Node::SELECTED) |
| 745 |
} else { |
| 746 |
piece::Held::Text(held) |
| 747 |
}; |
| 748 |
|
| 749 |
field.with_layout(|described| piece::field(tui.style(), &described, held, focused, area, buf)) |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
fn select_spans( |
| 754 |
tui: &Tui, |
| 755 |
options: &[(quasi_router::Choice, Option<quasi_router::Action>)], |
| 756 |
chosen: Option<&str>, |
| 757 |
focus: &[bool], |
| 758 |
) -> Vec<Span<'static>> { |
| 759 |
let mut spans = Vec::new(); |
| 760 |
for (index, (choice, _)) in options.iter().enumerate() { |
| 761 |
if !spans.is_empty() { |
| 762 |
spans.push(Span::raw(" ")); |
| 763 |
} |
| 764 |
let picked = chosen == Some(choice.value.as_str()); |
| 765 |
let style = if picked { |
| 766 |
Style::default() |
| 767 |
.fg(tui.theme().selection_on) |
| 768 |
.bg(tui.theme().action_primary) |
| 769 |
} else { |
| 770 |
Style::default().fg(tui.theme().content_secondary) |
| 771 |
}; |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
let focused = focus.get(index).copied().unwrap_or(false); |
| 777 |
let label = if focused { |
| 778 |
format!("[{}]", choice.label) |
| 779 |
} else { |
| 780 |
format!(" {} ", choice.label) |
| 781 |
}; |
| 782 |
spans.push(Span::styled(label, tui.style().focused(focused, style))); |
| 783 |
} |
| 784 |
spans |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
fn table_height(_columns: &[quasi_router::Column], rows: &[Cells]) -> u16 { |
| 789 |
1 + u16::try_from(rows.len()).unwrap_or(u16::MAX) |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
fn draw_table( |
| 800 |
pass: &mut Pass<'_>, |
| 801 |
columns: &[quasi_router::Column], |
| 802 |
rows: &[Cells], |
| 803 |
area: Rect, |
| 804 |
buf: &mut Buffer, |
| 805 |
) -> u16 { |
| 806 |
use ratatui::widgets::{StatefulWidget, TableState}; |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
let mut focused = None; |
| 816 |
for (index, cells) in rows.iter().enumerate() { |
| 817 |
if cells.activate.is_some() && pass.claim() { |
| 818 |
focused = Some(index); |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
let tui = pass.tui; |
| 823 |
let named: Vec<layout::Column<'_>> = columns |
| 824 |
.iter() |
| 825 |
.map(quasi_router::Column::as_layout) |
| 826 |
.collect(); |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
let sizing = table::Sizing { |
| 832 |
lengths: &[], |
| 833 |
fallback: 12, |
| 834 |
}; |
| 835 |
|
| 836 |
let body: Vec<Vec<table::Cell<'_>>> = rows |
| 837 |
.iter() |
| 838 |
.map(|cells| { |
| 839 |
columns |
| 840 |
.iter() |
| 841 |
.zip(&cells.values) |
| 842 |
.map(|(column, cell)| { |
| 843 |
table::Cell::new(column.name.as_str(), cell_line(tui, cell)) |
| 844 |
.part(cell_part(cell)) |
| 845 |
}) |
| 846 |
.collect() |
| 847 |
}) |
| 848 |
.collect(); |
| 849 |
|
| 850 |
let widget = table::table(&named, &body, &sizing, &tui.table, area.width); |
| 851 |
let height = table_height(columns, rows).min(area.height); |
| 852 |
let within = Rect { height, ..area }; |
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
let mut state = TableState::default(); |
| 863 |
if let Some(index) = focused.or_else(|| rows.iter().position(|cells| cells.current)) { |
| 864 |
state.select(Some(index)); |
| 865 |
} |
| 866 |
StatefulWidget::render(widget, within, buf, &mut state); |
| 867 |
height |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
fn cell_line(tui: &Tui, cell: &Cell) -> Line<'static> { |
| 872 |
let mut spans = Vec::new(); |
| 873 |
for part in &cell.parts { |
| 874 |
if !spans.is_empty() { |
| 875 |
spans.push(Span::raw(" ")); |
| 876 |
} |
| 877 |
spans.extend(inline_spans( |
| 878 |
tui, |
| 879 |
part, |
| 880 |
Style::default().fg(tui.theme().content_primary), |
| 881 |
false, |
| 882 |
)); |
| 883 |
} |
| 884 |
Line::from(spans) |
| 885 |
} |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
fn cell_part(cell: &Cell) -> layout::CellPart { |
| 894 |
if cell.parts.iter().any(|part| matches!(part, Node::Act(_))) { |
| 895 |
return layout::CellPart::Actions; |
| 896 |
} |
| 897 |
if cell |
| 898 |
.parts |
| 899 |
.iter() |
| 900 |
.any(|part| matches!(part, Node::Link { .. })) |
| 901 |
{ |
| 902 |
return layout::CellPart::Link; |
| 903 |
} |
| 904 |
if cell.parts.iter().any(|part| matches!(part, Node::Token(_))) { |
| 905 |
return layout::CellPart::Tokens; |
| 906 |
} |
| 907 |
layout::CellPart::Value |
| 908 |
} |
| 909 |
|