-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ahmed <>
- Loading branch information
Ahmed
committed
Sep 22, 2024
1 parent
1f0d873
commit b57b14c
Showing
25 changed files
with
1,302 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "rair-visual" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
cli-clipboard = { workspace = true} | ||
parking_lot = {workspace = true} | ||
rair-core ={workspace = true} | ||
rair-eval = {workspace = true} | ||
rair-env = {workspace = true} | ||
rair-io = {workspace = true} | ||
ratatui = {workspace = true} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
mod visual_hex; | ||
|
||
use rair_core::Core; | ||
use visual_hex::VisualHex; | ||
|
||
pub fn register_commands(core: &mut Core) { | ||
let vx = VisualHex::new(core); | ||
core.add_command(vx); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use crate::visual_hex::VisualHexEnv; | ||
|
||
use super::VisualHex; | ||
use rair_core::Core; | ||
use ratatui::prelude::*; | ||
use std::iter::once; | ||
|
||
impl VisualHex { | ||
/// Finds the maximum width of any address that will be currently rendered | ||
pub(super) fn max_address_str_width(&mut self, core: &mut Core, chunk: &Rect) -> u16 { | ||
let loc = core.get_loc(); | ||
let size = self.calculate_bytes_to_render(chunk); | ||
// we don't put the address of the last bytes, but we put the address | ||
// and then the last byte is 16 + that address. | ||
let max_loc = loc.saturating_add(size).saturating_sub(16); | ||
format!("0x{:08x} ", max_loc).len() as u16 | ||
} | ||
/// renders the column of all addresses + header | ||
pub(super) fn render_addresses( | ||
&mut self, | ||
f: &mut Frame, | ||
chunk: Rect, | ||
core: &mut Core, | ||
env: &VisualHexEnv, | ||
) { | ||
// TODO center the header | ||
let banner = env.render_addresses_banner(core); | ||
let loc = core.get_loc(); | ||
// TODO overflow here | ||
let addresses = (1..chunk.height as u64).map(|i| env.render_addresses(loc + 16 * (i - 1))); | ||
let lines: Vec<_> = once(banner).chain(addresses).collect(); | ||
let text = Text::from(lines); | ||
f.render_widget(text, chunk); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use crate::visual_hex::VisualHexEnv; | ||
|
||
use super::{mode::Mode, VisualHex}; | ||
use rair_core::Core; | ||
use ratatui::prelude::*; | ||
use std::collections::BTreeMap; | ||
|
||
impl VisualHex { | ||
fn ascii_line<'a>( | ||
&self, | ||
loc: u64, | ||
data: &BTreeMap<u64, u8>, | ||
env: &'a VisualHexEnv, | ||
) -> Line<'a> { | ||
let mut spans = Vec::with_capacity(16); | ||
for j in 0..16 { | ||
let byte = data.get(&(j + loc)).copied(); | ||
let span = env.render_ascii(byte); | ||
let span = match &self.mode { | ||
Mode::Edit(editor) => editor.span_ascii(span, loc + j), | ||
Mode::Select(selector) => selector.span_ascii(span, loc + j), | ||
_ => span, | ||
}; | ||
spans.push(span); | ||
} | ||
Line::default().spans(spans) | ||
} | ||
pub(super) fn render_ascii( | ||
&self, | ||
f: &mut Frame, | ||
chunk: Rect, | ||
core: &Core, | ||
data: &BTreeMap<u64, u8>, | ||
env: &VisualHexEnv, | ||
) { | ||
let mut lines: Vec<Line<'_>> = Vec::with_capacity(chunk.height as usize + 1); | ||
lines.push(env.render_ascii_banner()); | ||
|
||
let loc = core.get_loc(); | ||
|
||
for i in 0..chunk.height as u64 { | ||
lines.push(self.ascii_line(loc + i * 16, data, env)); | ||
} | ||
let txt = Text::from(lines); | ||
f.render_widget(txt, chunk); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use ratatui::{ | ||
layout::{Position, Rect}, | ||
text::Line, | ||
Frame, | ||
}; | ||
use std::cmp::max; | ||
|
||
#[derive(Default)] | ||
pub struct Command { | ||
input: String, | ||
cursor: usize, | ||
} | ||
|
||
impl Command { | ||
pub fn move_cursor_left(&mut self) { | ||
self.cursor = self.cursor.saturating_sub(1); | ||
} | ||
pub fn move_cursor_right(&mut self) { | ||
self.cursor = max(self.cursor.saturating_add(1), self.input.len()) | ||
} | ||
pub fn delete_char(&mut self) { | ||
if self.cursor == 0 { | ||
return; | ||
} | ||
let from_left_to_custor = self.cursor - 1; | ||
// Getting all characters before the selected character. | ||
let before_char_to_delete = self.input.chars().take(from_left_to_custor); | ||
// Getting all characters after selected character. | ||
let after_char_to_delete = self.input.chars().skip(self.cursor); | ||
self.input = before_char_to_delete.chain(after_char_to_delete).collect(); | ||
self.move_cursor_left(); | ||
} | ||
pub fn enter_char(&mut self, c: char) { | ||
let index = self | ||
.input | ||
.char_indices() | ||
.map(|(i, _)| i) | ||
.nth(self.cursor) | ||
.unwrap_or(self.input.len()); | ||
self.input.insert(index, c); | ||
self.move_cursor_right(); | ||
} | ||
pub fn get_str(&self) -> &str { | ||
&self.input | ||
} | ||
pub fn render(&self, f: &mut Frame, chunk: Rect) { | ||
let input = Line::from(format!(":{}", self.input)); | ||
f.render_widget(input, chunk); | ||
f.set_cursor_position(Position::new(chunk.x + self.cursor as u16 + 1, chunk.y)); | ||
} | ||
} |
Oops, something went wrong.