objdiff-cli: Migrate to ratatui for rendering

This commit is contained in:
Luke Street 2024-03-01 01:03:17 -07:00
parent 37ddbb7f4a
commit cb13638e07
4 changed files with 290 additions and 157 deletions

111
Cargo.lock generated
View File

@ -807,6 +807,21 @@ dependencies = [
"thiserror", "thiserror",
] ]
[[package]]
name = "cassowary"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df8670b8c7b9dae1793364eafadf7239c40d669904660c5960d74cfd80b46a53"
[[package]]
name = "castaway"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a17ed5635fc8536268e5d4de1e22e81ac34419e5f052d4d51f4e01dcc263fcc"
dependencies = [
"rustversion",
]
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.88" version = "1.0.88"
@ -939,6 +954,19 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "compact_str"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f86b9c4c00838774a6d902ef931eff7470720c51d90c2e32cfe15dc304737b3f"
dependencies = [
"castaway",
"cfg-if",
"itoa",
"ryu",
"static_assertions",
]
[[package]] [[package]]
name = "concurrent-queue" name = "concurrent-queue"
version = "2.4.0" version = "2.4.0"
@ -2168,6 +2196,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.3.8" version = "0.3.8"
@ -2337,6 +2371,12 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "indoc"
version = "2.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e186cfbae8084e513daff4240b4797e342f988cecda4fb6c939150f96315fd8"
[[package]] [[package]]
name = "inotify" name = "inotify"
version = "0.9.6" version = "0.9.6"
@ -2389,6 +2429,15 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7655c9839580ee829dfacba1d1278c2b7883e50a277ff7541299489d6bdfdc45" checksum = "7655c9839580ee829dfacba1d1278c2b7883e50a277ff7541299489d6bdfdc45"
[[package]]
name = "itertools"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.10" version = "1.0.10"
@ -2551,6 +2600,15 @@ version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "lru"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3262e75e648fce39813cb56ac41f3c3e3f65217ebf3844d818d1f9398cfb0dc"
dependencies = [
"hashbrown",
]
[[package]] [[package]]
name = "malloc_buf" name = "malloc_buf"
version = "0.0.6" version = "0.0.6"
@ -2973,6 +3031,7 @@ dependencies = [
"enable-ansi-support", "enable-ansi-support",
"log", "log",
"objdiff-core", "objdiff-core",
"ratatui",
"rayon", "rayon",
"serde", "serde",
"serde_json", "serde_json",
@ -3442,6 +3501,26 @@ dependencies = [
"getrandom", "getrandom",
] ]
[[package]]
name = "ratatui"
version = "0.26.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcb12f8fbf6c62614b0d56eb352af54f6a22410c3b079eb53ee93c7b97dd31d8"
dependencies = [
"bitflags 2.4.2",
"cassowary",
"compact_str",
"crossterm",
"indoc",
"itertools",
"lru",
"paste",
"stability",
"strum",
"unicode-segmentation",
"unicode-width",
]
[[package]] [[package]]
name = "raw-window-handle" name = "raw-window-handle"
version = "0.5.2" version = "0.5.2"
@ -4102,6 +4181,16 @@ dependencies = [
"bitflags 2.4.2", "bitflags 2.4.2",
] ]
[[package]]
name = "stability"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebd1b177894da2a2d9120208c3386066af06a488255caabc5de8ddca22dbc3ce"
dependencies = [
"quote",
"syn 1.0.109",
]
[[package]] [[package]]
name = "stable_deref_trait" name = "stable_deref_trait"
version = "1.2.0" version = "1.2.0"
@ -4120,6 +4209,28 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731" checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731"
[[package]]
name = "strum"
version = "0.26.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "723b93e8addf9aa965ebe2d11da6d7540fa2283fcea14b3371ff055f7ba13f5f"
dependencies = [
"strum_macros",
]
[[package]]
name = "strum_macros"
version = "0.26.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a3417fc93d76740d974a01654a09777cb500428cc874ca9f45edfe0c4d4cd18"
dependencies = [
"heck",
"proc-macro2",
"quote",
"rustversion",
"syn 2.0.51",
]
[[package]] [[package]]
name = "supports-color" name = "supports-color"
version = "3.0.0" version = "3.0.0"

View File

@ -20,6 +20,7 @@ crossterm = "0.27.0"
enable-ansi-support = "0.2.1" enable-ansi-support = "0.2.1"
log = "0.4.20" log = "0.4.20"
objdiff-core = { path = "../objdiff-core", features = ["all"] } objdiff-core = { path = "../objdiff-core", features = ["all"] }
ratatui = "0.26.1"
rayon = "1.8.1" rayon = "1.8.1"
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1.0.111" serde_json = "1.0.111"

View File

@ -1,21 +1,15 @@
use std::{ use std::{io::stdout, path::PathBuf};
io::{stdout, Write},
path::PathBuf,
};
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use argp::FromArgs; use argp::FromArgs;
use crossterm::{ use crossterm::{
cursor::{Hide, MoveRight, MoveTo, Show},
event, event,
event::{ event::{
DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind, MouseButton, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind, MouseButton,
MouseEventKind, MouseEventKind,
}, },
style::{Color, PrintStyledContent, Stylize},
terminal::{ terminal::{
disable_raw_mode, enable_raw_mode, size as terminal_size, Clear, ClearType, disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen, SetTitle,
EnterAlternateScreen, LeaveAlternateScreen, SetTitle,
}, },
}; };
use event::KeyModifiers; use event::KeyModifiers;
@ -26,6 +20,10 @@ use objdiff_core::{
obj, obj,
obj::{ObjInfo, ObjInsDiffKind, ObjSectionKind, ObjSymbol}, obj::{ObjInfo, ObjInsDiffKind, ObjSectionKind, ObjSymbol},
}; };
use ratatui::{
prelude::*,
widgets::{Block, Borders, Paragraph, Scrollbar, ScrollbarOrientation, ScrollbarState},
};
use crate::util::term::crossterm_panic_handler; use crate::util::term::crossterm_panic_handler;
@ -120,25 +118,26 @@ pub fn run(args: Args) -> Result<()> {
} }
_ => bail!("Either target and base or project and unit must be specified"), _ => bail!("Either target and base or project and unit must be specified"),
}; };
let mut state = FunctionDiffUi { let time_format = time::format_description::parse_borrowed::<2>("[hour]:[minute]:[second]")
clear: true, .context("Failed to parse time format")?;
let mut state = Box::new(FunctionDiffUi {
redraw: true, redraw: true,
size: (0, 0),
click_xy: None, click_xy: None,
left_highlight: HighlightKind::None, left_highlight: HighlightKind::None,
right_highlight: HighlightKind::None, right_highlight: HighlightKind::None,
skip: 0, scroll: 0,
y_offset: 2,
per_page: 0, per_page: 0,
max_len: 0, num_rows: 0,
symbol_name: args.symbol.clone(), symbol_name: args.symbol.clone(),
target_path, target_path,
base_path, base_path,
project_config, project_config,
left_sym: None, left_sym: None,
right_sym: None, right_sym: None,
reload_time: time::OffsetDateTime::now_local()?, reload_time: None,
}; time_format,
scroll_state: Default::default(),
});
state.reload()?; state.reload()?;
crossterm_panic_handler(); crossterm_panic_handler();
@ -146,36 +145,33 @@ pub fn run(args: Args) -> Result<()> {
crossterm::queue!( crossterm::queue!(
stdout(), stdout(),
EnterAlternateScreen, EnterAlternateScreen,
SetTitle(format!("{} - objdiff", args.symbol)),
Hide,
EnableMouseCapture, EnableMouseCapture,
SetTitle(format!("{} - objdiff", args.symbol)),
)?; )?;
state.size = terminal_size()?; let backend = CrosstermBackend::new(stdout());
let mut terminal = Terminal::new(backend)?;
loop { 'outer: loop {
let reload = loop { loop {
if state.redraw { if state.redraw {
state.draw()?; terminal.draw(|f| state.draw(f))?;
if state.redraw { if state.redraw {
continue; continue;
} }
} }
match state.handle_event(event::read()?) { match state.handle_event(event::read()?) {
FunctionDiffResult::Break => break false, FunctionDiffResult::Break => break 'outer,
FunctionDiffResult::Continue => {} FunctionDiffResult::Continue => {}
FunctionDiffResult::Reload => break true, FunctionDiffResult::Reload => break,
} }
};
if reload {
state.reload()?;
} else {
break;
} }
state.reload()?;
} }
// Reset terminal // Reset terminal
crossterm::execute!(stdout(), LeaveAlternateScreen, Show, DisableMouseCapture)?;
disable_raw_mode()?; disable_raw_mode()?;
crossterm::execute!(stdout(), LeaveAlternateScreen, DisableMouseCapture)?;
terminal.show_cursor()?;
Ok(()) Ok(())
} }
@ -195,23 +191,22 @@ fn find_function(obj: &ObjInfo, name: &str) -> Option<ObjSymbol> {
#[allow(dead_code)] #[allow(dead_code)]
struct FunctionDiffUi { struct FunctionDiffUi {
clear: bool,
redraw: bool, redraw: bool,
size: (u16, u16),
click_xy: Option<(u16, u16)>, click_xy: Option<(u16, u16)>,
left_highlight: HighlightKind, left_highlight: HighlightKind,
right_highlight: HighlightKind, right_highlight: HighlightKind,
skip: usize, scroll: usize,
y_offset: usize,
per_page: usize, per_page: usize,
max_len: usize, num_rows: usize,
symbol_name: String, symbol_name: String,
target_path: Option<PathBuf>, target_path: Option<PathBuf>,
base_path: Option<PathBuf>, base_path: Option<PathBuf>,
project_config: Option<ProjectConfig>, project_config: Option<ProjectConfig>,
left_sym: Option<ObjSymbol>, left_sym: Option<ObjSymbol>,
right_sym: Option<ObjSymbol>, right_sym: Option<ObjSymbol>,
reload_time: time::OffsetDateTime, reload_time: Option<time::OffsetDateTime>,
time_format: Vec<time::format_description::FormatItem<'static>>,
scroll_state: ScrollbarState,
} }
enum FunctionDiffResult { enum FunctionDiffResult {
@ -221,67 +216,94 @@ enum FunctionDiffResult {
} }
impl FunctionDiffUi { impl FunctionDiffUi {
fn draw(&mut self) -> Result<()> { fn draw(&mut self, f: &mut Frame) {
let mut w = stdout().lock(); let chunks = Layout::vertical([Constraint::Length(1), Constraint::Fill(1)]).split(f.size());
if self.clear { let header_chunks = Layout::horizontal([
crossterm::queue!(w, Clear(ClearType::All))?; Constraint::Fill(1),
} Constraint::Length(3),
let format = time::format_description::parse("[hour]:[minute]:[second]").unwrap(); Constraint::Fill(1),
let reload_time = self.reload_time.format(&format).unwrap(); Constraint::Length(2),
crossterm::queue!( ])
w, .split(chunks[0]);
MoveTo(0, 0), let content_chunks = Layout::horizontal([
PrintStyledContent(self.symbol_name.clone().with(Color::White)), Constraint::Fill(1),
MoveTo(0, 1), Constraint::Length(3),
PrintStyledContent(" ".repeat(self.size.0 as usize).underlined()), Constraint::Fill(1),
MoveTo(0, 1), Constraint::Length(2),
PrintStyledContent("TARGET ".underlined()), ])
MoveTo(self.size.0 / 2, 0), .split(chunks[1]);
PrintStyledContent(format!("Last reload: {}", reload_time).with(Color::White)),
MoveTo(self.size.0 / 2, 1),
PrintStyledContent("BASE ".underlined()),
)?;
if let Some(percent) = self.right_sym.as_ref().and_then(|s| s.match_percent) {
crossterm::queue!(
w,
PrintStyledContent(
format!("{:.2}%", percent).with(match_percent_color(percent)).underlined()
)
)?;
}
self.per_page = self.size.1 as usize - self.y_offset; self.per_page = chunks[1].height.saturating_sub(1) as usize;
let max_skip = self.max_len.saturating_sub(self.per_page); let max_scroll = self.num_rows.saturating_sub(self.per_page);
if self.skip > max_skip { if self.scroll > max_scroll {
self.skip = max_skip; self.scroll = max_scroll;
} }
self.scroll_state = self.scroll_state.content_length(max_scroll).position(self.scroll);
let mut line_l = Line::default();
line_l
.spans
.push(Span::styled(self.symbol_name.clone(), Style::new().fg(Color::White).bold()));
f.render_widget(line_l, header_chunks[0]);
let mut line_r = Line::default();
if let Some(percent) = self.right_sym.as_ref().and_then(|s| s.match_percent) {
line_r.spans.push(Span::styled(
format!("{:.2}% ", percent),
Style::new().fg(match_percent_color(percent)),
));
}
let reload_time = self
.reload_time
.as_ref()
.and_then(|t| t.format(&self.time_format).ok())
.unwrap_or_else(|| "N/A".to_string());
line_r.spans.push(Span::styled(
format!("Last reload: {}", reload_time),
Style::new().fg(Color::White),
));
f.render_widget(line_r, header_chunks[2]);
let create_block =
|title: &'static str| Block::new().borders(Borders::TOP).gray().title(title.bold());
let mut left_highlight = None; let mut left_highlight = None;
if let Some(symbol) = &self.left_sym { if let Some(symbol) = &self.left_sym {
let h = self.print_sym( // Render left column
&mut w, let mut text = Text::default();
symbol, let rect = margin_top(content_chunks[0], 1);
(0, self.y_offset as u16), let h = self.print_sym(&mut text, symbol, rect, &self.left_highlight);
(self.size.0 / 2 - 1, self.size.1), f.render_widget(Paragraph::new(text).block(create_block("TARGET")), content_chunks[0]);
&self.left_highlight,
)?;
if let Some(h) = h { if let Some(h) = h {
left_highlight = Some(h); left_highlight = Some(h);
} }
} }
let mut right_highlight = None; let mut right_highlight = None;
if let Some(symbol) = &self.right_sym { if let Some(symbol) = &self.right_sym {
let h = self.print_sym( // Render margin
&mut w, let mut text = Text::default();
symbol, let rect = margin_top(content_chunks[1], 1).inner(&Margin::new(1, 0));
(self.size.0 / 2, self.y_offset as u16), self.print_margin(&mut text, symbol, rect);
self.size, f.render_widget(text, rect);
&self.right_highlight,
)?; // Render right column
let mut text = Text::default();
let rect = margin_top(content_chunks[2], 1);
let h = self.print_sym(&mut text, symbol, rect, &self.right_highlight);
f.render_widget(Paragraph::new(text).block(create_block("CURRENT")), content_chunks[2]);
if let Some(h) = h { if let Some(h) = h {
right_highlight = Some(h); right_highlight = Some(h);
} }
} }
w.flush()?;
// Render scrollbar
f.render_stateful_widget(
Scrollbar::new(ScrollbarOrientation::VerticalRight).begin_symbol(None).end_symbol(None),
margin_top(chunks[1], 1),
&mut self.scroll_state,
);
if let Some(new_highlight) = left_highlight { if let Some(new_highlight) = left_highlight {
if new_highlight == self.left_highlight { if new_highlight == self.left_highlight {
if self.left_highlight != self.right_highlight { if self.left_highlight != self.right_highlight {
@ -295,7 +317,6 @@ impl FunctionDiffUi {
} }
self.redraw = true; self.redraw = true;
self.click_xy = None; self.click_xy = None;
self.clear = false;
} else if let Some(new_highlight) = right_highlight { } else if let Some(new_highlight) = right_highlight {
if new_highlight == self.right_highlight { if new_highlight == self.right_highlight {
if self.left_highlight != self.right_highlight { if self.left_highlight != self.right_highlight {
@ -309,13 +330,10 @@ impl FunctionDiffUi {
} }
self.redraw = true; self.redraw = true;
self.click_xy = None; self.click_xy = None;
self.clear = false;
} else { } else {
self.redraw = false; self.redraw = false;
self.click_xy = None; self.click_xy = None;
self.clear = true;
} }
Ok(())
} }
fn handle_event(&mut self, event: Event) -> FunctionDiffResult { fn handle_event(&mut self, event: Event) -> FunctionDiffResult {
@ -328,57 +346,57 @@ impl FunctionDiffUi {
KeyCode::Esc | KeyCode::Char('q') => return FunctionDiffResult::Break, KeyCode::Esc | KeyCode::Char('q') => return FunctionDiffResult::Break,
// Page up // Page up
KeyCode::PageUp => { KeyCode::PageUp => {
self.skip = self.skip.saturating_sub(self.per_page); self.scroll = self.scroll.saturating_sub(self.per_page);
self.redraw = true; self.redraw = true;
} }
// Page up (shift + space) // Page up (shift + space)
KeyCode::Char(' ') if event.modifiers.contains(KeyModifiers::SHIFT) => { KeyCode::Char(' ') if event.modifiers.contains(KeyModifiers::SHIFT) => {
self.skip = self.skip.saturating_sub(self.per_page); self.scroll = self.scroll.saturating_sub(self.per_page);
self.redraw = true; self.redraw = true;
} }
// Page down // Page down
KeyCode::Char(' ') | KeyCode::PageDown => { KeyCode::Char(' ') | KeyCode::PageDown => {
self.skip += self.per_page; self.scroll += self.per_page;
self.redraw = true; self.redraw = true;
} }
// Page down (ctrl + f) // Page down (ctrl + f)
KeyCode::Char('f') if event.modifiers.contains(KeyModifiers::CONTROL) => { KeyCode::Char('f') if event.modifiers.contains(KeyModifiers::CONTROL) => {
self.skip += self.per_page; self.scroll += self.per_page;
self.redraw = true; self.redraw = true;
} }
// Page up (ctrl + b) // Page up (ctrl + b)
KeyCode::Char('b') if event.modifiers.contains(KeyModifiers::CONTROL) => { KeyCode::Char('b') if event.modifiers.contains(KeyModifiers::CONTROL) => {
self.skip = self.skip.saturating_sub(self.per_page); self.scroll = self.scroll.saturating_sub(self.per_page);
self.redraw = true; self.redraw = true;
} }
// Half page down (ctrl + d) // Half page down (ctrl + d)
KeyCode::Char('d') if event.modifiers.contains(KeyModifiers::CONTROL) => { KeyCode::Char('d') if event.modifiers.contains(KeyModifiers::CONTROL) => {
self.skip += self.per_page / 2; self.scroll += self.per_page / 2;
self.redraw = true; self.redraw = true;
} }
// Half page up (ctrl + u) // Half page up (ctrl + u)
KeyCode::Char('u') if event.modifiers.contains(KeyModifiers::CONTROL) => { KeyCode::Char('u') if event.modifiers.contains(KeyModifiers::CONTROL) => {
self.skip = self.skip.saturating_sub(self.per_page / 2); self.scroll = self.scroll.saturating_sub(self.per_page / 2);
self.redraw = true; self.redraw = true;
} }
// Scroll down // Scroll down
KeyCode::Down | KeyCode::Char('j') => { KeyCode::Down | KeyCode::Char('j') => {
self.skip += 1; self.scroll += 1;
self.redraw = true; self.redraw = true;
} }
// Scroll up // Scroll up
KeyCode::Up | KeyCode::Char('k') => { KeyCode::Up | KeyCode::Char('k') => {
self.skip = self.skip.saturating_sub(1); self.scroll = self.scroll.saturating_sub(1);
self.redraw = true; self.redraw = true;
} }
// Scroll to start // Scroll to start
KeyCode::Char('g') => { KeyCode::Char('g') => {
self.skip = 0; self.scroll = 0;
self.redraw = true; self.redraw = true;
} }
// Scroll to end // Scroll to end
KeyCode::Char('G') => { KeyCode::Char('G') => {
self.skip = self.max_len; self.scroll = self.num_rows;
self.redraw = true; self.redraw = true;
} }
// Reload // Reload
@ -391,11 +409,11 @@ impl FunctionDiffUi {
} }
Event::Mouse(event) => match event.kind { Event::Mouse(event) => match event.kind {
MouseEventKind::ScrollDown => { MouseEventKind::ScrollDown => {
self.skip += 3; self.scroll += 3;
self.redraw = true; self.redraw = true;
} }
MouseEventKind::ScrollUp => { MouseEventKind::ScrollUp => {
self.skip = self.skip.saturating_sub(3); self.scroll = self.scroll.saturating_sub(3);
self.redraw = true; self.redraw = true;
} }
MouseEventKind::Down(MouseButton::Left) => { MouseEventKind::Down(MouseButton::Left) => {
@ -404,8 +422,7 @@ impl FunctionDiffUi {
} }
_ => {} _ => {}
}, },
Event::Resize(x, y) => { Event::Resize(_, _) => {
self.size = (x, y);
self.redraw = true; self.redraw = true;
} }
_ => {} _ => {}
@ -413,42 +430,30 @@ impl FunctionDiffUi {
FunctionDiffResult::Continue FunctionDiffResult::Continue
} }
fn print_sym<W>( fn print_sym(
&self, &self,
w: &mut W, out: &mut Text,
symbol: &ObjSymbol, symbol: &ObjSymbol,
origin: (u16, u16), rect: Rect,
max: (u16, u16),
highlight: &HighlightKind, highlight: &HighlightKind,
) -> Result<Option<HighlightKind>> ) -> Option<HighlightKind> {
where
W: Write,
{
let base_addr = symbol.address as u32; let base_addr = symbol.address as u32;
let mut new_highlight = None; let mut new_highlight = None;
let mut sy = origin.1; for (y, ins_diff) in
for ins_diff in symbol.instructions.iter().skip(self.skip) { symbol.instructions.iter().skip(self.scroll).take(rect.height as usize).enumerate()
let mut sx = origin.0; {
if ins_diff.kind != ObjInsDiffKind::None && sx > 2 { let mut sx = rect.x;
crossterm::queue!(w, MoveTo(sx - 2, sy))?; let sy = rect.y + y as u16;
let s = match ins_diff.kind { let mut line = Line::default();
ObjInsDiffKind::Delete => "< ",
ObjInsDiffKind::Insert => "> ",
_ => "| ",
};
crossterm::queue!(w, PrintStyledContent(s.with(Color::DarkGrey)))?;
} else {
crossterm::queue!(w, MoveTo(sx, sy))?;
}
display_diff(ins_diff, base_addr, |text| -> Result<()> { display_diff(ins_diff, base_addr, |text| -> Result<()> {
let mut label_text; let label_text;
let mut base_color = match ins_diff.kind { let mut base_color = match ins_diff.kind {
ObjInsDiffKind::None ObjInsDiffKind::None
| ObjInsDiffKind::OpMismatch | ObjInsDiffKind::OpMismatch
| ObjInsDiffKind::ArgMismatch => Color::Grey, | ObjInsDiffKind::ArgMismatch => Color::Gray,
ObjInsDiffKind::Replace => Color::DarkCyan, ObjInsDiffKind::Replace => Color::Cyan,
ObjInsDiffKind::Delete => Color::DarkRed, ObjInsDiffKind::Delete => Color::Red,
ObjInsDiffKind::Insert => Color::DarkGreen, ObjInsDiffKind::Insert => Color::Green,
}; };
let mut pad_to = 0; let mut pad_to = 0;
match text { match text {
@ -461,7 +466,7 @@ impl FunctionDiffUi {
} }
DiffText::Line(num) => { DiffText::Line(num) => {
label_text = format!("{num} "); label_text = format!("{num} ");
base_color = Color::DarkGrey; base_color = Color::DarkGray;
pad_to = 5; pad_to = 5;
} }
DiffText::Address(addr) => { DiffText::Address(addr) => {
@ -490,44 +495,52 @@ impl FunctionDiffUi {
base_color = Color::White; base_color = Color::White;
} }
DiffText::Spacing(n) => { DiffText::Spacing(n) => {
crossterm::queue!(w, MoveRight(n as u16))?; line.spans.push(Span::raw(" ".repeat(n)));
sx += n as u16; sx += n as u16;
return Ok(()); return Ok(());
} }
DiffText::Eol => { DiffText::Eol => {
sy += 1;
return Ok(()); return Ok(());
} }
} }
let len = label_text.len(); let len = label_text.len();
if sx >= max.0 {
return Ok(());
}
let highlighted = *highlight == text; let highlighted = *highlight == text;
if let Some((cx, cy)) = self.click_xy { if let Some((cx, cy)) = self.click_xy {
if cx >= sx && cx < sx + len as u16 && cy == sy { if cx >= sx && cx < sx + len as u16 && cy == sy {
new_highlight = Some(text.into()); new_highlight = Some(text.into());
} }
} }
label_text.truncate(max.0 as usize - sx as usize); let mut style = Style::new().fg(base_color);
let mut content = label_text.with(base_color);
if highlighted { if highlighted {
content = content.on_dark_grey(); style = style.bg(Color::DarkGray);
} }
crossterm::queue!(w, PrintStyledContent(content))?; line.spans.push(Span::styled(label_text, style));
sx += len as u16; sx += len as u16;
if pad_to > len { if pad_to > len {
let pad = (pad_to - len) as u16; let pad = (pad_to - len) as u16;
crossterm::queue!(w, MoveRight(pad))?; line.spans.push(Span::raw(" ".repeat(pad as usize)));
sx += pad; sx += pad;
} }
Ok(()) Ok(())
})?; })
if sy >= max.1 { .unwrap();
break; out.lines.push(line);
}
new_highlight
}
fn print_margin(&self, out: &mut Text, symbol: &ObjSymbol, rect: Rect) {
for ins_diff in symbol.instructions.iter().skip(self.scroll).take(rect.height as usize) {
if ins_diff.kind != ObjInsDiffKind::None {
out.lines.push(Line::raw(match ins_diff.kind {
ObjInsDiffKind::Delete => "<",
ObjInsDiffKind::Insert => ">",
_ => "|",
}));
} else {
out.lines.push(Line::raw(" "));
} }
} }
Ok(new_highlight)
} }
fn reload(&mut self) -> Result<()> { fn reload(&mut self) -> Result<()> {
@ -546,7 +559,7 @@ impl FunctionDiffUi {
let left_sym = target.as_ref().and_then(|o| find_function(o, &self.symbol_name)); let left_sym = target.as_ref().and_then(|o| find_function(o, &self.symbol_name));
let right_sym = base.as_ref().and_then(|o| find_function(o, &self.symbol_name)); let right_sym = base.as_ref().and_then(|o| find_function(o, &self.symbol_name));
self.max_len = match (&left_sym, &right_sym) { self.num_rows = match (&left_sym, &right_sym) {
(Some(l), Some(r)) => l.instructions.len().max(r.instructions.len()), (Some(l), Some(r)) => l.instructions.len().max(r.instructions.len()),
(Some(l), None) => l.instructions.len(), (Some(l), None) => l.instructions.len(),
(None, Some(r)) => r.instructions.len(), (None, Some(r)) => r.instructions.len(),
@ -554,18 +567,17 @@ impl FunctionDiffUi {
}; };
self.left_sym = left_sym; self.left_sym = left_sym;
self.right_sym = right_sym; self.right_sym = right_sym;
self.reload_time = time::OffsetDateTime::now_local()?; self.reload_time = time::OffsetDateTime::now_local().ok();
Ok(()) Ok(())
} }
} }
pub const COLOR_ROTATION: [Color; 8] = [ pub const COLOR_ROTATION: [Color; 7] = [
Color::Magenta, Color::Magenta,
Color::Cyan, Color::Cyan,
Color::Green, Color::Green,
Color::Red, Color::Red,
Color::Yellow, Color::Yellow,
Color::DarkMagenta,
Color::Blue, Color::Blue,
Color::Green, Color::Green,
]; ];
@ -574,8 +586,15 @@ pub fn match_percent_color(match_percent: f32) -> Color {
if match_percent == 100.0 { if match_percent == 100.0 {
Color::Green Color::Green
} else if match_percent >= 50.0 { } else if match_percent >= 50.0 {
Color::Blue Color::LightBlue
} else { } else {
Color::Red Color::LightRed
} }
} }
#[inline]
fn margin_top(mut rect: Rect, n: u16) -> Rect {
rect.y = rect.y.saturating_add(n);
rect.height = rect.height.saturating_sub(n);
rect
}

View File

@ -1,14 +1,16 @@
use std::panic; use std::{io::stdout, panic};
use crossterm::{
cursor::Show,
event::DisableMouseCapture,
terminal::{disable_raw_mode, LeaveAlternateScreen},
};
pub fn crossterm_panic_handler() { pub fn crossterm_panic_handler() {
let original_hook = panic::take_hook(); let original_hook = panic::take_hook();
panic::set_hook(Box::new(move |panic_info| { panic::set_hook(Box::new(move |panic_info| {
let _ = crossterm::execute!( let _ = crossterm::execute!(stdout(), LeaveAlternateScreen, DisableMouseCapture, Show);
std::io::stderr(), let _ = disable_raw_mode();
crossterm::terminal::LeaveAlternateScreen,
crossterm::event::DisableMouseCapture
);
let _ = crossterm::terminal::disable_raw_mode();
original_hook(panic_info); original_hook(panic_info);
})); }));
} }