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 202 203 204 205 206 207 208 209 210
extern crate proc_macro; use proc_macro::TokenStream; use quote::quote; use syn::{parse_macro_input, DeriveInput}; #[proc_macro_derive(Object)] pub fn object_derive(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); let name = input.ident; let gen = quote! { impl Object for #name { fn position(&self) -> Location { self.position } fn set_position(&mut self, location: &Location) { self.position.0 = location.0; self.position.1 = location.1; } fn size(&self) -> Size { self.size } fn set_size(&mut self, size: &Location) { self.size.0 = size.0; self.size.1 = size.1; } } }; gen.into() } #[proc_macro_derive(Paint)] pub fn paint_derive(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); let name = input.ident; let gen = quote! { impl Paint for #name { fn paint_ascii_for(&self, _location: &Location) -> Option<char> { Some(' ') } fn paint_style_for(&self, _location: &Location) -> ansi_term::Style { ansi_term::Style::default().reverse() } } }; gen.into() } #[proc_macro_derive(Clear)] pub fn clear_derive(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); let name = input.ident; let gen = quote! { impl Clear for #name { fn clear_ascii_for(&self, _location: &Location) -> Option<char> { Some(' ') } fn clear_style_for(&self, _location: &Location) -> ansi_term::Style { ansi_term::Style::default() } } }; gen.into() } #[proc_macro_derive(View)] pub fn view_derive(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); let name = input.ident; let gen = quote! { impl View for #name { fn view(&self) -> Vec<TermPixel> { let mut termpixels: Vec<TermPixel> = Vec::new(); for y in self.top_boundary()..(self.bottom_boundary() + 1) { for x in self.left_boundary()..(self.right_boundary() + 1) { let location = (x, y); if let Some(ascii) = self.paint_ascii_for(&location) { let style = self.paint_style_for(&location); termpixels.push((location, ascii, style)); } else if let Some(ascii) = self.clear_ascii_for(&location) { let style = self.clear_style_for(&location); termpixels.push((location, ascii, style)); } } } termpixels } } }; gen.into() } #[proc_macro_derive(Update)] pub fn update_derive(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); let name = input.ident; let gen = quote! { impl Update for #name { fn update(&mut self) -> io::Result<()> { Ok(()) } } }; gen.into() } #[proc_macro_derive(EventHandler)] pub fn event_handler_derive(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); let name = input.ident; let gen = quote! { impl EventHandler for #name { fn on_event(&mut self, event: termion::event::Event) -> io::Result<()> { match event { termion::event::Event::Key(termion::event::Key::Ctrl('c')) => { Err(io::Error::from(io::ErrorKind::Interrupted)) } _ => Ok(()), } } } }; gen.into() } #[proc_macro_derive(Render)] pub fn render_derive(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); let name = input.ident; let gen = quote! { impl Render for #name { fn render<R: Read>( &mut self, stdout: &mut termion::raw::RawTerminal<io::Stdout>, events: &mut termion::input::Events<R>, refresh_interval: Option<std::time::Duration>, ) -> io::Result<()> { let mut view: std::collections::HashMap<Location, (char, ansi_term::Style)> = std::collections::HashMap::new(); let mut updates = self.view(); write!(stdout, "{}", termion::cursor::Hide)?; loop { for (location, ascii, style) in updates.iter() { write!( stdout, "{}{}", termion::cursor::Goto(location.0, location.1), style.paint(ascii.to_string()) )?; } stdout.flush()?; if let Some(result) = events.next() { let event = result?; if let Err(err) = self.on_event(event) { match err.kind() { io::ErrorKind::Interrupted => { write!(stdout, "{}", termion::cursor::Show)?; break Ok(()); } _ => { write!(stdout, "{}", termion::cursor::Show)?; break Err(err); } } } }; if let Some(interval) = refresh_interval { std::thread::sleep(interval); } updates.clear(); self.update()?; for (location, ascii, style) in self.view() { if let Some((curr_ascii, curr_style)) = view.get(&location) { if curr_ascii != &ascii || curr_style != &style { updates.push((location, ascii, style)); view.insert(location, (ascii, style)); } } else { updates.push((location, ascii, style)); view.insert(location, (ascii, style)); } } } } } }; gen.into() } #[cfg(test)] mod tests { #[test] fn test_add() { assert_eq!(2 + 2, 4); } }