Skip to content

Commit

Permalink
feat(clafrica): implement frontend api
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonbrad committed May 8, 2023
1 parent df0594c commit 168c018
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
2 changes: 1 addition & 1 deletion clafrica/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ authors = ["Fomegne Brady <fomegnemeudje@outlook.com>"]
[dependencies]
clafrica-lib = { version = "0.1.0", path = "../clafrica-lib" }
enigo = "0.1.2"
rdev = "0.5.2"
rdev = "0.5.2"
21 changes: 21 additions & 0 deletions clafrica/src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
pub trait Frontend {
fn update_screen(&mut self, _screen: (u64, u64)) {}
fn update_position(&mut self, _position: (f64, f64)) {}
fn update_text(&mut self, _text: Vec<char>) {}
}

pub struct Console;

impl Frontend for Console {
fn update_text(&mut self, text: Vec<char>) {
println!("{:?}", text);
}
}

#[test]
fn test_console() {
let mut console = Console;
console.update_screen((0, 0));
console.update_position((0.0, 0.0));
console.update_text(vec!['h', 'e', 'l', 'l', 'o']);
}
26 changes: 16 additions & 10 deletions clafrica/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::api::Frontend;
use clafrica_lib::{text_buffer, utils};
use enigo::{Enigo, Key, KeyboardControllable};
use rdev::{self, EventType, Key as E_Key};
use std::sync::mpsc;
use std::thread;
use std::{env, io};
use std::{env, io, sync::mpsc, thread};

pub mod api;

pub struct Config {
data_path: String,
buffer_size: usize,
pub data_path: String,
pub buffer_size: usize,
}

impl Config {
Expand All @@ -20,7 +21,7 @@ impl Config {
}
}

pub fn run(config: Config) -> Result<(), io::Error> {
pub fn run(config: Config, mut frontend: impl Frontend) -> Result<(), io::Error> {
let data = utils::load_data(&config.data_path)?;
let map = utils::build_map(
data.iter()
Expand All @@ -31,6 +32,8 @@ pub fn run(config: Config) -> Result<(), io::Error> {

let mut keyboard = Enigo::new();

frontend.update_screen(rdev::display_size().unwrap());

let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let mut idle = false;
Expand Down Expand Up @@ -74,17 +77,17 @@ pub fn run(config: Config) -> Result<(), io::Error> {
cursor.undo();
}
}

frontend.update_text(cursor.to_path());
}
EventType::KeyPress(E_Key::Unknown(_) | E_Key::ShiftLeft | E_Key::ShiftRight) => {
println!("[ignore] {:?}", event.event_type)
// println!("[ignore] {:?}", event.event_type)
}
EventType::ButtonPress(_) | EventType::KeyPress(_) if !is_valid => {
cursor.clear();
println!("Buffer cleared");
}
EventType::KeyPress(_) => {
let character = character.unwrap();
println!("Received: {:?}", character);

let (prev_out, prev_code_len, ..) = cursor.state();
let out = cursor.hit(character);
Expand All @@ -99,7 +102,10 @@ pub fn run(config: Config) -> Result<(), io::Error> {
keyboard.key_up(Key::Escape);
};

println!("{:?}", cursor.to_path());
frontend.update_text(cursor.to_path());
}
EventType::MouseMove { x, y } => {
frontend.update_position((x, y));
}
_ => (),
};
Expand Down
6 changes: 4 additions & 2 deletions clafrica/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use clafrica::{run, Config};
use clafrica::{api, run, Config};
use std::{env, process};

fn main() {
let frontend = api::Console;

let conf = Config::build(env::args()).unwrap_or_else(|err| {
eprintln!("Problem parsing arguments: {err}");
process::exit(1);
});

if let Err(e) = run(conf) {
if let Err(e) = run(conf, frontend) {
eprintln!("Application error: {e}");
process::exit(1);
}
Expand Down

0 comments on commit 168c018

Please sign in to comment.