1use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
2use crossterm::style::Color;
3use printer::{
4 Result,
5 buffer::Buffer,
6 printer::{PrintQueue, Printer, PrinterItem, default_process_fn},
7};
8
9fn main() -> Result<()> {
10 let mut printer = Printer::new(std::io::stdout(), "In: ".into());
11 printer.print_prompt_if_set()?;
12 std::io::Write::flush(&mut printer.writer.raw)?;
13
14 let mut buffer = Buffer::new();
15
16 loop {
17 let inp = crossterm::event::read()?;
18 if let crossterm::event::Event::Key(key) = inp {
19 match key {
20 KeyEvent {
21 kind: KeyEventKind::Release,
22 ..
23 } => (),
24 KeyEvent {
25 code: KeyCode::Char(c),
26 modifiers: KeyModifiers::NONE,
27 ..
28 } => {
29 buffer.insert(c);
30 printer.print_input(&default_process_fn, &buffer)?;
31 printer.cursor.move_right_unbounded();
32 }
33 KeyEvent {
34 code: KeyCode::Backspace,
35 ..
36 } => {
37 if !buffer.is_at_start() {
38 buffer.move_backward();
39 printer.cursor.move_left();
40 buffer.remove_current_char();
41 printer.print_input(&default_process_fn, &buffer)?;
42 }
43 }
44 KeyEvent {
45 code: KeyCode::Enter,
46 ..
47 } => {
48 if let Some(mut output) = eval(buffer.to_string()) {
49 output.push_front(PrinterItem::NewLine);
50
51 printer.print_output(output)?;
52 }
53 buffer.clear();
54 printer.print_prompt_if_set()?;
55 }
56 KeyEvent {
57 code: KeyCode::Char('c'),
58 modifiers: KeyModifiers::CONTROL,
59 ..
60 } => break,
61 _ => (),
62 }
63 }
64 std::io::Write::flush(&mut printer.writer.raw)?;
65 }
66 Ok(())
67}
68
69fn eval(buffer: String) -> Option<PrintQueue> {
70 let mut buffer = buffer.split_whitespace();
71 let cmd = buffer.next()?;
72 let args: Vec<&str> = buffer.collect();
73
74 #[allow(clippy::blocks_in_conditions)]
75 match (|| -> Result<PrinterItem> {
76 let output = std::process::Command::new(cmd).args(args).output()?;
77 if output.status.success() {
78 Ok(PrinterItem::String(
79 String::from_utf8(output.stdout)?,
80 Color::Blue,
81 ))
82 } else {
83 Ok(PrinterItem::String(
84 String::from_utf8(output.stderr)?,
85 Color::Red,
86 ))
87 }
88 })() {
89 Ok(result) => Some(result.into()),
90 Err(e) => Some(PrinterItem::String(e.to_string(), Color::Red).into()),
91 }
92}