1use colored::{ColoredString, Colorize};
2use iced_x86::{FormatterOutput, FormatterTextKind};
3use std::fmt::{Display, Formatter, Result};
4
5#[derive(Default)]
6pub struct ColourFormatter {
7 output: Vec<ColoredString>,
8}
9
10impl Display for ColourFormatter {
11 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
12 for s in &self.output {
13 write!(f, "{}", s)?;
14 }
15 Ok(())
16 }
17}
18
19impl ColourFormatter {
20 pub fn new() -> Self { Self::default() }
21
22 pub fn clear(&mut self) { self.output.clear() }
23}
24
25impl FormatterOutput for ColourFormatter {
26 fn write(&mut self, text: &str, kind: FormatterTextKind) {
27 self.output.push(match kind {
28 FormatterTextKind::Function => text.red(),
29 FormatterTextKind::Mnemonic | FormatterTextKind::Prefix => text.yellow(),
30 FormatterTextKind::Keyword => text.normal(),
31 FormatterTextKind::Register => match text {
32 "sp" | "esp" | "rsp" | "ip" | "eip" | "rip" => text.red(),
33 _ => text.normal(),
34 },
35 _ => text.normal(),
36 })
37 }
38}