//! Regenerates the deterministic TUI screenshots embedded in the README. use std::{fs, io, path::PathBuf}; use ratatui::{ Terminal, backend::TestBackend, buffer::Buffer, style::{Color, Modifier}, }; use tui::{ ConfigurationSnapshot, ConfiguredApplication, ConfiguredFader, ConfiguredGroup, FaderDensity, GroupVolumeMode, LiveTui, MidiDevice, MixerClient, MixerItem, MixerLayoutEntry, MixerObjectId, MixerObjectKind, MixerOrientation, MixerSnapshot, PickupStatus, ThemeName, }; const CELL_WIDTH: u16 = 9; const CELL_HEIGHT: u16 = 18; #[derive(Clone)] struct ScreenshotClient(MixerSnapshot); impl MixerClient for ScreenshotClient { fn snapshot(&mut self) -> io::Result { Ok(self.0.clone()) } fn set_volume( &mut self, _id: MixerObjectId, _gain: f64, _expected_revision: u64, ) -> io::Result { Ok(self.0.clone()) } fn toggle_mute( &mut self, _id: MixerObjectId, _expected_revision: u64, ) -> io::Result { Ok(self.0.clone()) } } fn main() -> io::Result<()> { let output = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../docs/images"); fs::create_dir_all(&output)?; render( MixerOrientation::Horizontal, 148, 24, output.join("mixer-horizontal.svg"), )?; render( MixerOrientation::Vertical, 148, 27, output.join("mixer-vertical.svg"), ) } fn render( orientation: MixerOrientation, width: u16, height: u16, output: PathBuf, ) -> io::Result<()> { let app = LiveTui::new(ScreenshotClient(snapshot(orientation)))?; let mut terminal = Terminal::new(TestBackend::new(width, height))?; terminal.draw(|frame| frame.render_widget(&app, frame.area()))?; fs::write(output, buffer_svg(terminal.backend().buffer())) } fn snapshot(mixer_orientation: MixerOrientation) -> MixerSnapshot { let targets = [ (MixerObjectKind::Application, "music", "Music"), (MixerObjectKind::Application, "browser", "Browser"), (MixerObjectKind::Application, "voice", "Voice chat"), (MixerObjectKind::Group, "media", "Media group"), (MixerObjectKind::Output, "default", "Master output"), ]; let items = targets .into_iter() .enumerate() .map(|(index, (kind, id, label))| MixerItem { id: MixerObjectId { kind, id: id.to_owned(), }, label: label.to_owned(), detail: match kind { MixerObjectKind::Application => "2 streams", MixerObjectKind::Group => "Music + Browser", MixerObjectKind::Output => "Built-in Audio Analog Stereo", } .to_owned(), gain: Some([0.72, 0.38, 0.55, 0.72, 0.86][index]), gain_mixed: kind == MixerObjectKind::Group, muted: Some(index == 2), mute_mixed: kind == MixerObjectKind::Group, pending: false, pickup: Some(match index { 0 | 2 | 4 => PickupStatus::Tracking, 1 => PickupStatus::Waiting, _ => PickupStatus::Mixed, }), writable: true, error: None, }) .collect(); let faders = targets .into_iter() .map(|(kind, id, _)| ConfiguredFader { id: format!("{id}-fader"), target: MixerObjectId { kind, id: id.to_owned(), }, pickup: true, pickup_tolerance: 0.02, }) .collect(); MixerSnapshot { revision: 42, backend: "pulse · PipeWire".to_owned(), status: "ready".to_owned(), error: None, items, devices: vec![MidiDevice { id: "apc-mini".to_owned(), label: "APC MINI".to_owned(), status: "connected".to_owned(), backend_id: Some("24:0".to_owned()), error: None, }], discovered_midi_ports: Vec::new(), configuration: ConfigurationSnapshot { revision: 7, dirty: false, applied_revision: 7, selection_saved: true, learning: None, error: None, profile_id: "desktop".to_owned(), available_profiles: vec!["desktop".to_owned()], mixer_orientation, status_display: tui::StatusDisplayMode::Symbol, default_controller: Some("apc-mini".to_owned()), theme: ThemeName::CatppuccinMocha, applications: vec![ ConfiguredApplication { id: "music".to_owned(), label: "Music".to_owned(), selectors: Vec::new(), }, ConfiguredApplication { id: "browser".to_owned(), label: "Browser".to_owned(), selectors: Vec::new(), }, ConfiguredApplication { id: "voice".to_owned(), label: "Voice chat".to_owned(), selectors: Vec::new(), }, ], groups: vec![ConfiguredGroup { id: "media".to_owned(), label: "Media group".to_owned(), members: vec!["music".to_owned(), "browser".to_owned()], volume_mode: GroupVolumeMode::Absolute, }], faders, mixer_layout: if mixer_orientation == MixerOrientation::Horizontal { vec![ layout_fader("music", FaderDensity::Thick), layout_fader("browser", FaderDensity::Thick), layout_fader("voice", FaderDensity::Thick), separator( "group-divider", "", tui::SeparatorSize::Slim, tui::SeparatorDecoration::Double, ), layout_fader("media", FaderDensity::Thick), separator( "before-master", "", tui::SeparatorSize::Fill, tui::SeparatorDecoration::Plain, ), layout_fader("default", FaderDensity::Thick), ] } else { vec![ MixerLayoutEntry::Separator { id: "applications".to_owned(), label: "Applications".to_owned(), size: tui::SeparatorSize::Slim, decoration: tui::SeparatorDecoration::Double, }, layout_fader( "music", horizontal_density(mixer_orientation, FaderDensity::Standard), ), layout_fader( "browser", horizontal_density(mixer_orientation, FaderDensity::Slim), ), layout_fader("voice", FaderDensity::BorderedSlim), MixerLayoutEntry::Separator { id: "groups".to_owned(), label: "Groups".to_owned(), size: tui::SeparatorSize::Slim, decoration: tui::SeparatorDecoration::Line, }, layout_fader("media", FaderDensity::Standard), MixerLayoutEntry::Separator { id: "outputs".to_owned(), label: "Outputs".to_owned(), size: tui::SeparatorSize::Slim, decoration: tui::SeparatorDecoration::Line, }, layout_fader( "default", horizontal_density(mixer_orientation, FaderDensity::Standard), ), ] }, bindings: Vec::new(), }, } } fn separator( id: &str, label: &str, size: tui::SeparatorSize, decoration: tui::SeparatorDecoration, ) -> MixerLayoutEntry { MixerLayoutEntry::Separator { id: id.to_owned(), label: label.to_owned(), size, decoration, } } const fn horizontal_density( orientation: MixerOrientation, vertical_density: FaderDensity, ) -> FaderDensity { match orientation { MixerOrientation::Horizontal => FaderDensity::Thick, MixerOrientation::Vertical => vertical_density, } } fn layout_fader(id: &str, density: FaderDensity) -> MixerLayoutEntry { MixerLayoutEntry::Fader { fader: format!("{id}-fader"), hidden: false, density, } } fn buffer_svg(buffer: &Buffer) -> String { let width = buffer.area.width * CELL_WIDTH; let height = buffer.area.height * CELL_HEIGHT; let mut svg = format!( "\n\n\n" ); for y in 0..buffer.area.height { let mut start = 0; while start < buffer.area.width { let background = buffer[(start, y)].bg; let mut end = start + 1; while end < buffer.area.width && buffer[(end, y)].bg == background { end += 1; } if background != Color::Reset { svg.push_str(&format!( "\n", start * CELL_WIDTH, y * CELL_HEIGHT, (end - start) * CELL_WIDTH, CELL_HEIGHT, color(background, "#1e1e2e") )); } start = end; } for x in 0..buffer.area.width { let cell = &buffer[(x, y)]; let text = cell.symbol(); let foreground = color(cell.fg, "#cdd6f4"); if let Some(glyph) = cell_shape(text, x * CELL_WIDTH, y * CELL_HEIGHT, &foreground) { svg.push_str(&glyph); continue; } if !text.trim().is_empty() { let weight = if cell.modifier.contains(Modifier::BOLD) { " font-weight=\"bold\"" } else { "" }; svg.push_str(&format!( "{}\n", x * CELL_WIDTH, y * CELL_HEIGHT + 1, foreground, weight, escape_xml(text) )); } } } svg.push_str("\n\n"); svg } // Draw terminal strokes and blocks to their cell edges. Font metrics vary // between SVG viewers and otherwise leave gaps in borders and filled meters. fn cell_shape(symbol: &str, x: u16, y: u16, color: &str) -> Option { let path = match symbol { "─" => "M0 9 H9", "│" => "M4.5 0 V18", "┌" => "M4.5 18 V9 H9", "┐" => "M0 9 H4.5 V18", "└" => "M4.5 0 V9 H9", "┘" => "M0 9 H4.5 V0", "╭" => "M4.5 18 V13.5 Q4.5 9 9 9", "╮" => "M0 9 Q4.5 9 4.5 13.5 V18", "╰" => "M4.5 0 V4.5 Q4.5 9 9 9", "╯" => "M0 9 Q4.5 9 4.5 4.5 V0", "━" => "M0 8 H9 M0 9 H9", "┃" => "M4 0 V18 M5 0 V18", "┏" => "M4 18 V8 H9 M5 18 V9 H9", "┓" => "M0 8 H5 V18 M0 9 H4 V18", "┗" => "M4 0 V9 H9 M5 0 V8 H9", "┛" => "M0 9 H5 V0 M0 8 H4 V0", "═" => "M0 7 H9 M0 11 H9", "║" => "M3 0 V18 M6 0 V18", "╔" => "M3 18 V7 H9 M6 18 V11 H9", "╗" => "M0 7 H6 V18 M0 11 H3 V18", "╚" => "M3 0 V11 H9 M6 0 V7 H9", "╝" => "M0 11 H6 V0 M0 7 H3 V0", "█" | "░" => { return Some(format!( "\n", if symbol == "█" { "1" } else { "0.18" } )); } _ => return None, }; Some(format!( "\n" )) } fn color(color: Color, reset: &str) -> String { match color { Color::Reset => reset.to_owned(), Color::Black => "#11111b".to_owned(), Color::Red => "#f38ba8".to_owned(), Color::Green => "#a6e3a1".to_owned(), Color::Yellow => "#f9e2af".to_owned(), Color::Blue => "#89b4fa".to_owned(), Color::Magenta => "#cba6f7".to_owned(), Color::Cyan => "#89dceb".to_owned(), Color::Gray => "#bac2de".to_owned(), Color::DarkGray => "#585b70".to_owned(), Color::LightRed => "#eba0ac".to_owned(), Color::LightGreen => "#94e2d5".to_owned(), Color::LightYellow => "#fab387".to_owned(), Color::LightBlue => "#74c7ec".to_owned(), Color::LightMagenta => "#f5c2e7".to_owned(), Color::LightCyan => "#b4befe".to_owned(), Color::White => "#cdd6f4".to_owned(), Color::Rgb(red, green, blue) => format!("#{red:02x}{green:02x}{blue:02x}"), Color::Indexed(index) => indexed_color(index).to_owned(), } } const fn indexed_color(index: u8) -> &'static str { match index { 0..=7 => "#6c7086", 8..=15 => "#cdd6f4", 16..=51 => "#89b4fa", 52..=87 => "#f38ba8", 88..=123 => "#a6e3a1", 124..=159 => "#f9e2af", 160..=195 => "#cba6f7", 196..=231 => "#89dceb", _ => "#bac2de", } } fn escape_xml(value: &str) -> String { value .replace('&', "&") .replace('<', "<") .replace('>', ">") .replace('"', """) }