mod.rs - icy_draw - [fork] icy_draw is the successor to mystic draw.
HTML git clone https://git.drkhsh.at/icy_draw.git
DIR Log
DIR Files
DIR Refs
DIR README
---
mod.rs (2359B)
---
1 use super::{ansi, BufferParser};
2 use crate::{Buffer, CallbackAction, Caret, EngineResult, TextAttribute};
3
4 pub struct Parser {
5 ansi_parser: ansi::Parser,
6
7 // PCB
8 pub pcb_code: bool,
9 pub pcb_color: bool,
10 pub pcb_value: u8,
11 pub pcb_pos: i32,
12 }
13
14 impl Default for Parser {
15 fn default() -> Self {
16 let mut p = super::ansi::Parser::default();
17 p.bs_is_ctrl_char = true;
18
19 Self {
20 ansi_parser: p,
21 pcb_code: Default::default(),
22 pcb_color: Default::default(),
23 pcb_value: Default::default(),
24 pcb_pos: Default::default(),
25 }
26 }
27 }
28
29 impl BufferParser for Parser {
30 fn print_char(&mut self, buf: &mut Buffer, current_layer: usize, caret: &mut Caret, ch: char) -> EngineResult<CallbackAction> {
31 if self.pcb_color {
32 self.pcb_pos += 1;
33 if self.pcb_pos < 3 {
34 match self.pcb_pos {
35 1 => {
36 self.pcb_value = conv_ch(ch);
37 return Ok(CallbackAction::NoUpdate);
38 }
39 2 => {
40 self.pcb_value = (self.pcb_value << 4) + conv_ch(ch);
41 caret.attribute = TextAttribute::from_u8(self.pcb_value, buf.ice_mode);
42 }
43 _ => {}
44 }
45 }
46 self.pcb_color = false;
47 self.pcb_code = false;
48 return Ok(CallbackAction::NoUpdate);
49 }
50
51 if self.pcb_code {
52 match ch {
53 '@' => {
54 self.pcb_code = false;
55 }
56 'X' => {
57 self.pcb_color = true;
58 self.pcb_pos = 0;
59 }
60 _ => {}
61 }
62 return Ok(CallbackAction::NoUpdate);
63 }
64 match ch {
65 '@' => {
66 self.pcb_code = true;
67 Ok(CallbackAction::NoUpdate)
68 }
69 _ => self.ansi_parser.print_char(buf, current_layer, caret, ch),
70 }
71 }
72 }
73
74 fn conv_ch(ch: char) -> u8 {
75 if ch.is_ascii_digit() {
76 return ch as u8 - b'0';
77 }
78 if ('a'..='f').contains(&ch) {
79 return 10 + ch as u8 - b'a';
80 }
81 if ('A'..='F').contains(&ch) {
82 return 10 + ch as u8 - b'A';
83 }
84 0
85 }