1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//! Exporter
use bladvak::eframe::egui::{self, FontId, RichText, TextEdit};
use bladvak::eframe::egui::{Color32, Widget};
use bladvak::errors::ErrorManager;
use std::path::{Path, PathBuf};
use crate::selection::Selection;
/// export type
#[derive(Debug, PartialEq, Clone, serde::Deserialize, serde::Serialize)]
pub(crate) enum ExportType {
/// hex
Hex,
/// binary
Binary,
/// octal
Octal,
/// decimal
Decimal,
}
/// Histogram data
#[derive(serde::Deserialize, serde::Serialize, Debug)]
pub(crate) struct Exporter {
/// is open
pub(crate) is_open: bool,
/// prefix value
prefix: bool,
/// separator value
separator: String,
/// value type
pub(crate) value_type: ExportType,
#[serde(skip)]
/// export error
export_error: Option<String>,
}
impl Exporter {
/// New import data
pub(crate) fn new() -> Self {
Self {
is_open: false,
prefix: true,
separator: " ".to_string(),
value_type: ExportType::Hex,
export_error: None,
}
}
/// reset data
pub(crate) fn reset(&mut self) {
self.export_error = None;
}
/// Import
/// # Errors
/// return error if fails to parse the `value`
fn format_export(
selection: &[u8],
export_type: &ExportType,
prefix: bool,
separator: &str,
) -> String {
let prefix = if prefix {
match export_type {
ExportType::Binary => "0b",
ExportType::Decimal => "",
ExportType::Hex => "0x",
ExportType::Octal => "0o",
}
} else {
""
};
let tokens = selection
.iter()
.map(|one_u8| match export_type {
ExportType::Binary => format!("{prefix}{one_u8:08b}"),
ExportType::Hex => format!("{prefix}{one_u8:02X}"),
ExportType::Octal => format!("{prefix}{one_u8:03o}"),
ExportType::Decimal => format!("{one_u8}"),
})
.collect::<Vec<String>>();
tokens.join(separator)
}
/// Show the exporter ui
pub(crate) fn ui(
&mut self,
binary_file: &[u8],
file_path: &Path,
selection: &Selection,
ui: &mut egui::Ui,
error_manager: &mut ErrorManager,
) -> Option<Vec<u8>> {
if self.is_open {
let mut is_open = self.is_open;
egui::Window::new("Exporter")
.open(&mut is_open)
.vscroll(true)
.show(ui.ctx(), |ui| {
let previous_import_type = self.value_type.clone();
ui.horizontal(|ui| {
ui.label("Export selection to:");
ui.selectable_value(&mut self.value_type, ExportType::Hex, "Hex");
ui.selectable_value(&mut self.value_type, ExportType::Binary, "Binary");
ui.selectable_value(&mut self.value_type, ExportType::Octal, "Octal");
ui.selectable_value(&mut self.value_type, ExportType::Decimal, "Decimal");
});
if previous_import_type != self.value_type {
self.export_error = None;
}
ui.horizontal(|ui| {
ui.label("Separator");
ui.text_edit_singleline(&mut self.separator);
});
if self.value_type != ExportType::Decimal {
ui.checkbox(&mut self.prefix, "Prefix");
}
if binary_file.is_empty() {
ui.label("File is empty - no selection");
} else {
ui.horizontal(|ui| {
let export_selection = match selection.range {
Some(curr_select) => curr_select.0..=curr_select.1,
None => 0..=(binary_file.len() - 1),
};
if ui.button("Copy to clipboard").clicked() {
if let Some(file_selection) =
binary_file.get(export_selection.clone())
{
let data = Self::format_export(
file_selection,
&self.value_type,
self.prefix,
&self.separator,
);
ui.ctx().copy_text(data);
} else {
self.export_error =
Some("Cannot determine selection".to_string());
}
}
if let Some(slice) = binary_file.get(export_selection)
&& ui.button("Export as raw").clicked()
{
let file_name = file_path
.file_name()
.map_or_else(|| PathBuf::from("exported"), PathBuf::from);
if let Err(e) = bladvak::utils::save_file(
slice,
&file_name.with_extension("bin"),
) {
error_manager.add_error(e);
}
}
// if ui.button("Export to file").clicked() {
// // TODO
// }
});
let (selected_preview, is_file) = if let Some(range) = selection.range {
let stop = range.1.min(range.0 + 49);
(range.0..=stop, false)
} else {
let max = (binary_file.len() - 1).min(49);
(0..=max, true)
};
ui.horizontal(|ui| {
ui.label(format!(
"Preview on 50 bytes (of {})",
if is_file { "file" } else { "selection" }
));
if let Some(err) = &self.export_error {
ui.label(RichText::new(err).color(Color32::LIGHT_RED));
}
});
if let Some(preview_value) = binary_file.get(selected_preview) {
let mut formatted = Self::format_export(
preview_value,
&self.value_type,
self.prefix,
&self.separator,
);
TextEdit::multiline(&mut formatted)
.min_size(ui.available_size())
.desired_width(f32::INFINITY)
.font(FontId::monospace(12.0))
.ui(ui);
}
}
});
self.is_open = is_open;
}
None
}
}