Lines
96.75 %
Functions
100 %
Branches
use crate::app::{App, CliConfig};
use crate::hits::Hits;
use crate::keymap::{Hint, Keys, handle_key};
use crate::modal::Modal;
use crate::tabs::config::ConfigTab;
use crate::tabs::list::ListTab;
use crate::tabs::nms::ConsoleState;
use crate::tabs::reports::ReportsTab;
use crate::tabs::scripts::ScriptsTab;
use crate::tabs::ssh_keys::SshKeysTab;
use crate::tabs::tags::TagsTab;
use crate::tabs::templates::TemplatesTab;
use crate::view::{DrawCtx, ViewId};
use crate::widgets::grid::{Grid, GridLine, Item};
use crate::window::{Origin, Pane};
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use ratatui::buffer::Buffer;
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use sqlx::types::Uuid;
use tvui::{ColorDepth, Theme};
/// The status bar items of `app` as key and label.
pub(crate) fn hint_pairs(app: &App) -> Vec<(String, String)> {
crate::keymap::hinted(app).iter().map(Hint::pair).collect()
}
/// Draws `app` once on a `width` by `height` terminal, so the windows record their views.
pub(crate) fn draw_once(app: &App, width: u16, height: u16) {
draw_frame(app, width, height);
/// Draws `app` once and returns the screen and the areas the frame recorded.
pub(crate) fn draw_frame(app: &App, width: u16, height: u16) -> (Buffer, Hits) {
let mut terminal = Terminal::new(TestBackend::new(width, height)).expect("test terminal");
let mut hits = Hits::default();
terminal
.draw(|frame| crate::draw::draw(frame, app, &mut hits))
.expect("the frame draws");
(terminal.backend().buffer().clone(), hits)
/// Draws `app` on a `width` by `height` terminal and keeps the areas for the mouse.
pub(crate) fn draw_into(app: &mut App, width: u16, height: u16) {
app.hits = draw_frame(app, width, height).1;
/// Draws `app` on an 80 by 24 terminal and keeps the areas for the mouse.
pub(crate) fn redraw(app: &mut App) {
draw_into(app, 80, 24);
/// The area the last frame drew for `target`; the test fails when it was not drawn.
pub(crate) fn area_of(app: &App, target: crate::hits::Target) -> ratatui::layout::Rect {
match app.hits.entries().iter().find(|(_, t)| *t == target) {
Some((rect, _)) => *rect,
None => panic!("{target:?} was not drawn"),
/// The items of cell line `line` of `grid`.
pub(crate) fn cells<'a>(grid: &'a Grid<'a>, line: usize) -> &'a [Item<'a>] {
match grid.lines.get(line) {
Some(GridLine::Cells { items, .. }) => items,
_ => &[],
/// How many conversion arrows cell line `line` of `grid` has.
pub(crate) fn arrows(grid: &Grid<'_>, line: usize) -> usize {
cells(grid, line)
.iter()
.filter(|item| item.text == "\u{2192}")
.count()
pub(crate) fn dn_theme() -> Theme {
Theme::preset(tvui::DEFAULT_PRESET).expect("the dn preset parses")
pub(crate) fn draw_ctx<'a>(theme: &'a Theme, hits: &'a mut Hits) -> DrawCtx<'a> {
DrawCtx {
is_active: false,
frame: tvui::FrameState {
mode: tvui::FrameMode::Active,
..tvui::FrameState::default()
},
title: String::new(),
theme,
hits,
/// An app on an empty desktop, as it starts.
pub(crate) fn empty_app_for(user_id: Uuid) -> App {
match App::new(user_id, CliConfig::default(), ColorDepth::Rgb) {
Ok(app) => app,
Err(err) => panic!("the default theme builds: {err}"),
pub(crate) fn empty_app() -> App {
empty_app_for(Uuid::new_v4())
/// An app with a window of every view.
pub(crate) fn app_for(user_id: Uuid) -> App {
let mut app = empty_app_for(user_id);
app.open_every_view();
app
pub(crate) fn app() -> App {
app_for(Uuid::new_v4())
pub(crate) fn app_with_preset(name: &str) -> App {
let mut app = app();
app.keys = match Keys::preset(name) {
Ok(keys) => keys,
Err(err) => panic!("the {name} preset parses: {err}"),
};
pub(crate) fn emacs_app() -> App {
app_with_preset("emacs")
pub(crate) fn vim_app() -> App {
app_with_preset("vim")
pub(crate) fn key(code: KeyCode) -> KeyEvent {
KeyEvent::new(code, KeyModifiers::NONE)
pub(crate) fn ctrl(code: KeyCode) -> KeyEvent {
KeyEvent::new(code, KeyModifiers::CONTROL)
pub(crate) fn alt(code: KeyCode) -> KeyEvent {
KeyEvent::new(code, KeyModifiers::ALT)
/// Translate then apply, the way the runtime handles a key event.
pub(crate) fn press(app: &mut App, event: KeyEvent) {
handle_key(app, event);
app.flush_key_press();
macro_rules! test_panes {
($($get:ident $(, $get_mut:ident)?: $ty:ty = $kind:expr;)*) => {
impl App {
$(
pub(crate) fn $get(&self) -> &$ty {
self.recent::<$ty>($kind).expect(stringify!($ty))
pub(crate) fn $get_mut(&mut self) -> &mut $ty {
self.recent_mut::<$ty>($kind).expect(stringify!($ty))
)?
)*
test_panes! {
reports, reports_mut: ReportsTab = ViewId::Reports;
config, config_mut: ConfigTab = ViewId::Config;
ssh_keys: SshKeysTab = ViewId::SshKeys;
tags: TagsTab = ViewId::Tags;
templates: TemplatesTab = ViewId::Templates;
scripts, scripts_mut: ScriptsTab = ViewId::Scripts;
console, console_mut: ConsoleState = ViewId::Console;
/// The title of the front window, which must be a viewer.
pub(crate) fn viewer_title(app: &App) -> String {
match app.front::<crate::viewer::Viewer>() {
Some(viewer) => viewer.title(),
None => panic!("the front window is not a viewer"),
/// Whether a Help window is the front window.
pub(crate) fn help_open(app: &App) -> bool {
app.front::<crate::viewer::Viewer>()
.is_some_and(|viewer| matches!(viewer.source, crate::viewer::Source::Help { .. }))
/// Pushes a read-only draft dialog, for what only the `modal-view` scope does.
pub(crate) fn open_draft(app: &mut App) {
let lines = (0..60).map(|n| format!("line {n}")).collect();
app.overlays
.push(Modal::Draft(crate::modal::Draft::new(lines)));
/// Pushes a confirmation, the generic modal fixture.
pub(crate) fn open_overlay(app: &mut App) {
let modal = Modal::confirm("Sure?", crate::modal::ConfirmAction::DiscardForm);
app.overlays.push(modal);
/// Opens `modal` as a dialog of the front window, without loading it.
pub(crate) fn open_dialog_on_front(&mut self, modal: Modal) {
let window = self.front_window();
let origin = window.and_then(|window| {
let kind = self.window_view(window)?;
Some(Origin { window, kind })
});
self.overlays.set_origin(origin);
self.overlays.push(modal);
/// The `T` of the front-most window of `kind`.
fn recent<T: Pane>(&self, kind: ViewId) -> Option<&T> {
self.win::<T>(self.window_id(kind)?)
fn recent_mut<T: Pane>(&mut self, kind: ViewId) -> Option<&mut T> {
let id = self.window_id(kind)?;
self.win_mut::<T>(id)
pub(crate) fn accounts(&self) -> &ListTab {
self.recent(ViewId::Accounts).expect("Accounts")
pub(crate) fn accounts_mut(&mut self) -> &mut ListTab {
self.recent_mut(ViewId::Accounts).expect("Accounts")
pub(crate) fn transactions(&self) -> &ListTab {
self.recent(ViewId::Transactions).expect("Transactions")
pub(crate) fn transactions_mut(&mut self) -> &mut ListTab {
self.recent_mut(ViewId::Transactions).expect("Transactions")
pub(crate) fn commodities(&self) -> &ListTab {
self.recent(ViewId::Commodities).expect("Commodities")
pub(crate) fn commodities_mut(&mut self) -> &mut ListTab {
self.recent_mut(ViewId::Commodities).expect("Commodities")
/// The cells of a `place` that sits at a non-negative origin.
pub fn flat(place: tvui::Place) -> ratatui::layout::Rect {
let cell = |value: i32| u16::try_from(value).unwrap_or_default();
ratatui::layout::Rect::new(cell(place.x), cell(place.y), place.width, place.height)