1use std::{
4 default,
5 ops::{Add, Div},
6};
7
8#[derive(Default, Clone)]
9pub struct Line {
10 no: String,
11 hex: String,
12 info: String,
13}
14
15impl Line {
16 pub fn no(&self) -> &str {
17 &self.no
18 }
19 pub fn hex(&self) -> &str {
20 &self.hex
21 }
22 pub fn info(&self) -> &str {
23 &self.info
24 }
25 pub fn push_no(&mut self, no: String) {
26 self.no.push_str(&no);
27 }
28 pub fn push_hex(&mut self, hex: String) {
29 self.hex.push_str(&hex);
30 }
31 pub fn push_info(&mut self, info: String) {
32 self.info.push_str(&info);
33 }
34 pub fn set_no(&mut self, no: String) {
35 self.no = no;
36 }
37 pub fn set_hex(&mut self, hex: String) {
38 self.hex = hex;
39 }
40 pub fn set_info(&mut self, info: String) {
41 self.info = info;
42 }
43 pub fn to_string(&self) -> String {
44 format!("{} {} | {}", self.no, self.hex, self.info)
45 }
46}
47
48pub struct Matrix {
49 width: usize,
50 b_arr: Vec<u8>,
51 lines: Vec<Line>,
52}
53
54impl Matrix {
63 pub fn new(w: usize, b: Vec<u8>) -> Self {
64 Self {
65 width: w,
66 b_arr: b.clone(),
67 lines: Self::to_matrix(w, b.clone()),
68 }
69 }
70
71 pub fn print_matrix(&mut self) {
81 for (i, v) in self.lines.iter().enumerate() {
82 println!("{}", v.to_string());
83 }
84 }
85
86 pub fn reset(&mut self, b: &[u8]) {
98 self.b_arr = b.to_vec();
99 Self::to_matrix(self.width, b.to_vec());
100 }
101
102 pub fn get_line_by_index(&mut self, index: usize) -> &mut Line {
113 self.lines.get_mut(index).unwrap()
114 }
115
116 pub fn line_count(&self) -> usize {
127 self.lines.len()
128 }
129
130 pub fn lines(&self) -> Vec<Line> {
145 self.lines.clone()
146 }
147
148 fn to_matrix(width: usize, b_arr: Vec<u8>) -> Vec<Line> {
149 let len = b_arr.len();
150 let mut lines: Vec<Line> = vec![];
151 let mut line = Line::default();
152 let mut max_line_count = len.div(width);
154 max_line_count = if max_line_count % width > 0 {
155 max_line_count.add(1)
156 } else {
157 max_line_count
158 };
159 let mut max_line_number = String::from(format!("{}", (max_line_count * width))).len();
161 let mut current_line_number: usize = 0;
163 for (i, b) in b_arr.iter().enumerate() {
164 let line_number: usize = current_line_number * width;
166 line.push_hex(format!("{:<02X} ", b));
167 if i % width == width - 1 || i == len - 1 {
168 for _j in 0..width - 1 - (i % (width)) {
169 line.push_hex("00 ".to_string());
170 }
171 for j in i - i % width..i + 1 {
172 if b_arr[j].is_ascii_alphabetic() {
173 line.push_info(format!("{}", b_arr[j] as char));
174 } else {
175 line.push_info(".".to_string());
176 }
177 }
178 let mut fill_zero = String::from("");
179 for i in 0..(max_line_number - line_number.to_string().len()) {
180 fill_zero.push_str("0");
181 }
182 line.set_no(format!("{}{}", fill_zero, line_number.to_string()));
183 lines.push(line.clone());
184 line = Line::default();
185 current_line_number += 1;
186 }
187 }
188 lines
189 }
190}