Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ cargo install --path .
glass <notes-directory>
```

Render a full ANSI debug snapshot without opening the interactive editor:

```bash
glass --render [--width 100] [--height rows] <path>
```

`--width` defaults to 100 columns. When `--height` is omitted, Glass renders the
entire document followed by the status bar.

## Keybindings

### Normal mode
Expand Down
240 changes: 240 additions & 0 deletions src/debug_render.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
use std::path::PathBuf;

use anyhow::Result;
use ratatui::{
Terminal,
backend::TestBackend,
layout::Rect,
style::{Color, Modifier},
};

use crate::{
app::App,
editor::render::{visible_rows, wrap_line},
markdown::{highlight::concealed_wrap_line, table::TableLayout},
ui,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct CellStyle {
fg: Color,
bg: Color,
modifier: Modifier,
}

impl Default for CellStyle {
fn default() -> Self {
Self {
fg: Color::Reset,
bg: Color::Reset,
modifier: Modifier::empty(),
}
}
}

pub fn render_path_to_ansi(
notes_dir: PathBuf,
initial_file: Option<PathBuf>,
width: u16,
height: Option<u16>,
) -> Result<String> {
let mut app = App::new(notes_dir, initial_file)?;
let width = width.max(1);
let height = height.unwrap_or_else(|| full_render_height(&app, width));
let backend = TestBackend::new(width, height.max(2));
let mut terminal = Terminal::new(backend)?;
terminal.draw(|frame| ui::draw(frame, &mut app))?;
Ok(buffer_to_ansi(terminal.backend().buffer()))
}

fn full_render_height(app: &App, width: u16) -> u16 {
let editor_area = Rect {
x: 0,
y: 0,
width,
height: 1,
};
let page = ui::editor::page_area(editor_area);
let gutter_width = (app.buffer.line_count().to_string().len() + 1).max(1);
let text_width = page.width.saturating_sub(gutter_width as u16).max(1) as usize;
let table_layout = TableLayout::new(&app.buffer);
let rows = visible_rows(
&app.buffer,
0,
0,
usize::MAX,
text_width,
|line_num, text, w| {
if line_num == app.cursor.line {
wrap_line(text, w)
} else if table_layout.is_table_row(line_num) {
table_layout.wrap_line(line_num, text, w)
} else {
concealed_wrap_line(text, w)
}
},
);
rows.len().saturating_add(1).min(u16::MAX as usize) as u16
}

fn buffer_to_ansi(buffer: &ratatui::buffer::Buffer) -> String {
let mut out = String::new();
let area = buffer.area;
for y in area.top()..area.bottom() {
let mut current = CellStyle::default();
for x in area.left()..area.right() {
let Some(cell) = buffer.cell((x, y)) else {
continue;
};
let next = CellStyle {
fg: cell.fg,
bg: cell.bg,
modifier: cell.modifier,
};
if next != current {
out.push_str(&style_ansi(next));
current = next;
}
out.push_str(cell.symbol());
}
out.push_str("\x1b[0m");
if y + 1 < area.bottom() {
out.push('\n');
}
}
out
}

fn style_ansi(style: CellStyle) -> String {
let mut sequence = String::from("\x1b[0m");
sequence.push_str(&fg_ansi(style.fg));
sequence.push_str(&bg_ansi(style.bg));
if style.modifier.contains(Modifier::BOLD) {
sequence.push_str("\x1b[1m");
}
if style.modifier.contains(Modifier::DIM) {
sequence.push_str("\x1b[2m");
}
if style.modifier.contains(Modifier::ITALIC) {
sequence.push_str("\x1b[3m");
}
if style.modifier.contains(Modifier::UNDERLINED) {
sequence.push_str("\x1b[4m");
}
if style.modifier.contains(Modifier::REVERSED) {
sequence.push_str("\x1b[7m");
}
if style.modifier.contains(Modifier::CROSSED_OUT) {
sequence.push_str("\x1b[9m");
}
sequence
}

fn fg_ansi(color: Color) -> String {
color_ansi(color, true)
}

fn bg_ansi(color: Color) -> String {
color_ansi(color, false)
}

fn color_ansi(color: Color, foreground: bool) -> String {
let base = if foreground { 30 } else { 40 };
let bright_base = if foreground { 90 } else { 100 };
match color {
Color::Reset => format!("\x1b[{}m", if foreground { 39 } else { 49 }),
Color::Black => format!("\x1b[{base}m"),
Color::Red => format!("\x1b[{}m", base + 1),
Color::Green => format!("\x1b[{}m", base + 2),
Color::Yellow => format!("\x1b[{}m", base + 3),
Color::Blue => format!("\x1b[{}m", base + 4),
Color::Magenta => format!("\x1b[{}m", base + 5),
Color::Cyan => format!("\x1b[{}m", base + 6),
Color::Gray | Color::White => format!("\x1b[{}m", base + 7),
Color::DarkGray => format!("\x1b[{bright_base}m"),
Color::LightRed => format!("\x1b[{}m", bright_base + 1),
Color::LightGreen => format!("\x1b[{}m", bright_base + 2),
Color::LightYellow => format!("\x1b[{}m", bright_base + 3),
Color::LightBlue => format!("\x1b[{}m", bright_base + 4),
Color::LightMagenta => format!("\x1b[{}m", bright_base + 5),
Color::LightCyan => format!("\x1b[{}m", bright_base + 6),
Color::Rgb(r, g, b) => {
format!("\x1b[{};2;{r};{g};{b}m", if foreground { 38 } else { 48 })
}
Color::Indexed(index) => {
format!("\x1b[{};5;{index}m", if foreground { 38 } else { 48 })
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use anyhow::Context;
use std::fs;

#[test]
fn render_full_height_includes_entire_file_and_status_bar() -> Result<()> {
let dir = std::env::temp_dir().join(format!("glass-render-test-{}", std::process::id()));
fs::create_dir_all(&dir)?;
let file = dir.join("note.md");
fs::write(&file, "one\ntwo\nthree\n")?;

let ansi = render_path_to_ansi(dir.clone(), Some(file), 80, None)?;

assert!(ansi.contains("one"));
assert!(ansi.contains("two"));
assert!(ansi.contains("three"));
assert!(ansi.contains(" NORMAL note.md"));

fs::remove_dir_all(dir).context("failed to clean render test directory")?;
Ok(())
}

#[test]
fn render_height_can_clip_document_but_keeps_status_bar() -> Result<()> {
let dir =
std::env::temp_dir().join(format!("glass-render-clip-test-{}", std::process::id()));
fs::create_dir_all(&dir)?;
let file = dir.join("note.md");
fs::write(&file, "one\ntwo\nthree\n")?;

let ansi = render_path_to_ansi(dir.clone(), Some(file), 80, Some(2))?;

assert!(ansi.contains("one"));
assert!(!ansi.contains("three"));
assert!(ansi.contains(" NORMAL note.md"));

fs::remove_dir_all(dir).context("failed to clean render test directory")?;
Ok(())
}

#[test]
fn render_height_has_status_bar_even_when_too_small() -> Result<()> {
let dir =
std::env::temp_dir().join(format!("glass-render-small-test-{}", std::process::id()));
fs::create_dir_all(&dir)?;
let file = dir.join("note.md");
fs::write(&file, "one\n")?;

let ansi = render_path_to_ansi(dir.clone(), Some(file), 80, Some(1))?;

assert!(ansi.contains(" NORMAL note.md"));

fs::remove_dir_all(dir).context("failed to clean render test directory")?;
Ok(())
}

#[test]
fn style_ansi_includes_rgb_and_modifiers() {
let ansi = style_ansi(CellStyle {
fg: Color::Rgb(1, 2, 3),
bg: Color::Reset,
modifier: Modifier::BOLD | Modifier::UNDERLINED,
});

assert!(ansi.contains("\x1b[38;2;1;2;3m"));
assert!(ansi.contains("\x1b[1m"));
assert!(ansi.contains("\x1b[4m"));
}
}
Loading