From 115bc83b8ebf4bf0cc338106d708ffcc9d20d2b5 Mon Sep 17 00:00:00 2001 From: Brooks J Rady Date: Tue, 17 Nov 2020 21:40:01 +0000 Subject: [PATCH 01/12] Initial Commit --- .gitignore | 2 ++ Cargo.toml | 9 +++++++++ src/keys.rs | 37 +++++++++++++++++++++++++++++++++++++ src/lib.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/shim.rs | 18 ++++++++++++++++++ 5 files changed, 109 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/keys.rs create mode 100644 src/lib.rs create mode 100644 src/shim.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..44709884 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target/ +Cargo.lock \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 00000000..90fc26cf --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "mosaic-tile" +version = "0.1.0" +authors = ["Brooks J Rady "] +edition = "2018" + +[dependencies] +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" \ No newline at end of file diff --git a/src/keys.rs b/src/keys.rs new file mode 100644 index 00000000..ed44554a --- /dev/null +++ b/src/keys.rs @@ -0,0 +1,37 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash, Serialize, Deserialize)] +pub struct KeyEvent { + pub code: KeyCode, + pub modifiers: KeyModifiers, +} + +#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash, Serialize, Deserialize)] +pub struct KeyModifiers { + pub bits: u8, + //pub shift: bool, + //pub ctrl: bool, + //pub alt: bool, +} + +#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash, Serialize, Deserialize)] +pub enum KeyCode { + Backspace, + Enter, + Left, + Right, + Up, + Down, + Home, + End, + PageUp, + PageDown, + Tab, + BackTab, + Delete, + Insert, + F(u8), + Char(char), + Null, + Esc, +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 00000000..536504ff --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,43 @@ +mod keys; +mod shim; + +pub use keys::*; +pub use shim::*; + +pub trait MosaicPlugin { + fn init(&mut self); + fn draw(&mut self, rows: usize, cols: usize); + fn handle_key(&mut self, key: KeyEvent); +} + +#[macro_export] +macro_rules! register_plugin { + ($t:ty) => { + use mosaic_plugin::*; + + use std::cell::RefCell; + thread_local! { + static STATE: RefCell<$t> = RefCell::new(Default::default()); + } + + fn main() { + STATE.with(|state| { + state.borrow_mut().init(); + }); + } + + #[no_mangle] + pub fn draw(rows: i32, cols: i32) { + STATE.with(|state| { + state.borrow_mut().draw(rows as usize, cols as usize); + }); + } + + #[no_mangle] + pub fn handle_key() { + STATE.with(|state| { + state.borrow_mut().handle_key(get_key()); + }); + } + }; +} diff --git a/src/shim.rs b/src/shim.rs new file mode 100644 index 00000000..850d1668 --- /dev/null +++ b/src/shim.rs @@ -0,0 +1,18 @@ +use crate::keys::KeyEvent; +use std::{io, path::Path}; + +pub fn get_key() -> KeyEvent { + let mut json = String::new(); + io::stdin().read_line(&mut json).unwrap(); + serde_json::from_str(&json).unwrap() +} + +pub fn open_file(path: &Path) { + println!("{}", path.to_string_lossy()); + unsafe { host_open_file() }; +} + +#[link(wasm_import_module = "mosaic")] +extern "C" { + fn host_open_file(); +} From e0bc330205322d045f2b83f3cd5a6cbb3d273d88 Mon Sep 17 00:00:00 2001 From: Brooks J Rady Date: Tue, 17 Nov 2020 21:53:12 +0000 Subject: [PATCH 02/12] Add some metadata --- Cargo.toml | 3 +++ LICENSE.md | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 LICENSE.md diff --git a/Cargo.toml b/Cargo.toml index 90fc26cf..3c009e9b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,9 @@ name = "mosaic-tile" version = "0.1.0" authors = ["Brooks J Rady "] edition = "2018" +description = "A small client-side library for writing mosaic plugins (tiles)" +repository = "https://github.com/mosaic-org/mosaic-tile" +license = "MIT" [dependencies] serde = { version = "1.0", features = ["derive"] } diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 00000000..0b264c31 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Mosaic contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From 842e668b4204f549683025ef70ec24ccbc7e9fa0 Mon Sep 17 00:00:00 2001 From: Brooks J Rady Date: Tue, 17 Nov 2020 22:02:35 +0000 Subject: [PATCH 03/12] Rename plugin -> tile --- src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 536504ff..1d1673fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,16 +4,16 @@ mod shim; pub use keys::*; pub use shim::*; -pub trait MosaicPlugin { +pub trait MosaicTile { fn init(&mut self); fn draw(&mut self, rows: usize, cols: usize); fn handle_key(&mut self, key: KeyEvent); } #[macro_export] -macro_rules! register_plugin { +macro_rules! register_tile { ($t:ty) => { - use mosaic_plugin::*; + use mosaic_tile::*; use std::cell::RefCell; thread_local! { From b39ec4c0b70648773698330f485aa11c7f62ab53 Mon Sep 17 00:00:00 2001 From: Brooks J Rady Date: Tue, 17 Nov 2020 22:04:04 +0000 Subject: [PATCH 04/12] Bump version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 3c009e9b..91756a7f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mosaic-tile" -version = "0.1.0" +version = "0.1.1" authors = ["Brooks J Rady "] edition = "2018" description = "A small client-side library for writing mosaic plugins (tiles)" From 25356a592c0c989b4f4087dad84b382b412018a4 Mon Sep 17 00:00:00 2001 From: Brooks J Rady Date: Thu, 7 Jan 2021 10:49:43 +0000 Subject: [PATCH 05/12] Change to the Termion Key enum --- src/keys.rs | 37 ------------------------------------- src/lib.rs | 4 +--- src/shim.rs | 26 ++++++++++++++++++++++++-- 3 files changed, 25 insertions(+), 42 deletions(-) delete mode 100644 src/keys.rs diff --git a/src/keys.rs b/src/keys.rs deleted file mode 100644 index ed44554a..00000000 --- a/src/keys.rs +++ /dev/null @@ -1,37 +0,0 @@ -use serde::{Deserialize, Serialize}; - -#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash, Serialize, Deserialize)] -pub struct KeyEvent { - pub code: KeyCode, - pub modifiers: KeyModifiers, -} - -#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash, Serialize, Deserialize)] -pub struct KeyModifiers { - pub bits: u8, - //pub shift: bool, - //pub ctrl: bool, - //pub alt: bool, -} - -#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash, Serialize, Deserialize)] -pub enum KeyCode { - Backspace, - Enter, - Left, - Right, - Up, - Down, - Home, - End, - PageUp, - PageDown, - Tab, - BackTab, - Delete, - Insert, - F(u8), - Char(char), - Null, - Esc, -} diff --git a/src/lib.rs b/src/lib.rs index 1d1673fc..a1e4f1fd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,13 +1,11 @@ -mod keys; mod shim; -pub use keys::*; pub use shim::*; pub trait MosaicTile { fn init(&mut self); fn draw(&mut self, rows: usize, cols: usize); - fn handle_key(&mut self, key: KeyEvent); + fn handle_key(&mut self, key: Key); } #[macro_export] diff --git a/src/shim.rs b/src/shim.rs index 850d1668..070b6d2b 100644 --- a/src/shim.rs +++ b/src/shim.rs @@ -1,7 +1,29 @@ -use crate::keys::KeyEvent; +use serde::{Deserialize, Serialize}; use std::{io, path::Path}; -pub fn get_key() -> KeyEvent { +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum Key { + Backspace, + Left, + Right, + Up, + Down, + Home, + End, + PageUp, + PageDown, + BackTab, + Delete, + Insert, + F(u8), + Char(char), + Alt(char), + Ctrl(char), + Null, + Esc, +} + +pub fn get_key() -> Key { let mut json = String::new(); io::stdin().read_line(&mut json).unwrap(); serde_json::from_str(&json).unwrap() From c6ecbfb129690bfba1efb0f2ea787db0ee78618b Mon Sep 17 00:00:00 2001 From: Brooks J Rady Date: Thu, 7 Jan 2021 10:50:19 +0000 Subject: [PATCH 06/12] Version bump! --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 91756a7f..6ecbe6c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mosaic-tile" -version = "0.1.1" +version = "0.2.1" authors = ["Brooks J Rady "] edition = "2018" description = "A small client-side library for writing mosaic plugins (tiles)" From d79db4acb33f14c93c4f4b1e18c09ffb702212e1 Mon Sep 17 00:00:00 2001 From: Brooks J Rady Date: Sun, 10 Jan 2021 22:58:48 +0000 Subject: [PATCH 07/12] Add a global key callback --- Cargo.toml | 2 +- src/lib.rs | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6ecbe6c9..24ca614e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mosaic-tile" -version = "0.2.1" +version = "0.2.2" authors = ["Brooks J Rady "] edition = "2018" description = "A small client-side library for writing mosaic plugins (tiles)" diff --git a/src/lib.rs b/src/lib.rs index a1e4f1fd..9d373a02 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,11 +1,12 @@ mod shim; pub use shim::*; - +#[allow(unused_variables)] pub trait MosaicTile { - fn init(&mut self); - fn draw(&mut self, rows: usize, cols: usize); - fn handle_key(&mut self, key: Key); + fn init(&mut self) {} + fn draw(&mut self, rows: usize, cols: usize) {} + fn handle_key(&mut self, key: Key) {} + fn handle_global_key(&mut self, key: Key) {} } #[macro_export] @@ -37,5 +38,12 @@ macro_rules! register_tile { state.borrow_mut().handle_key(get_key()); }); } + + #[no_mangle] + pub fn handle_global_key() { + STATE.with(|state| { + state.borrow_mut().handle_global_key(get_key()); + }); + } }; } From 4592f12349cbfc703f3f00e5b0d753ef225e25de Mon Sep 17 00:00:00 2001 From: Brooks J Rady Date: Mon, 11 Jan 2021 06:02:59 +0000 Subject: [PATCH 08/12] Add a new host function --- Cargo.toml | 2 +- src/shim.rs | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 24ca614e..3fa27a26 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mosaic-tile" -version = "0.2.2" +version = "0.3.0" authors = ["Brooks J Rady "] edition = "2018" description = "A small client-side library for writing mosaic plugins (tiles)" diff --git a/src/shim.rs b/src/shim.rs index 070b6d2b..60bdfbd5 100644 --- a/src/shim.rs +++ b/src/shim.rs @@ -34,7 +34,13 @@ pub fn open_file(path: &Path) { unsafe { host_open_file() }; } +pub fn set_selectable(selectable: bool) { + let selectable = if selectable { 1 } else { 0 }; + unsafe { host_set_selectable(selectable) }; +} + #[link(wasm_import_module = "mosaic")] extern "C" { fn host_open_file(); + fn host_set_selectable(selectable: i32); } From 1275a9e73a1098fec7c984f017c07d5349781ac3 Mon Sep 17 00:00:00 2001 From: Brooks J Rady Date: Mon, 11 Jan 2021 16:05:55 +0000 Subject: [PATCH 09/12] No more glob imports in the register_tile! macro --- Cargo.toml | 2 +- src/lib.rs | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3fa27a26..dd7a6636 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mosaic-tile" -version = "0.3.0" +version = "0.3.1" authors = ["Brooks J Rady "] edition = "2018" description = "A small client-side library for writing mosaic plugins (tiles)" diff --git a/src/lib.rs b/src/lib.rs index 9d373a02..5279520b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,11 +12,8 @@ pub trait MosaicTile { #[macro_export] macro_rules! register_tile { ($t:ty) => { - use mosaic_tile::*; - - use std::cell::RefCell; thread_local! { - static STATE: RefCell<$t> = RefCell::new(Default::default()); + static STATE: std::cell::RefCell<$t> = std::cell::RefCell::new(Default::default()); } fn main() { @@ -35,14 +32,14 @@ macro_rules! register_tile { #[no_mangle] pub fn handle_key() { STATE.with(|state| { - state.borrow_mut().handle_key(get_key()); + state.borrow_mut().handle_key($crate::get_key()); }); } #[no_mangle] pub fn handle_global_key() { STATE.with(|state| { - state.borrow_mut().handle_global_key(get_key()); + state.borrow_mut().handle_global_key($crate::get_key()); }); } }; From 680a986cf66ad14a4a0d147ed34b8aa3d1ffaacd Mon Sep 17 00:00:00 2001 From: Brooks J Rady Date: Mon, 11 Jan 2021 21:37:47 +0000 Subject: [PATCH 10/12] Add get_help function --- Cargo.toml | 2 +- src/shim.rs | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dd7a6636..b9fbe0a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mosaic-tile" -version = "0.3.1" +version = "0.4.0" authors = ["Brooks J Rady "] edition = "2018" description = "A small client-side library for writing mosaic plugins (tiles)" diff --git a/src/shim.rs b/src/shim.rs index 60bdfbd5..6fa8b0e6 100644 --- a/src/shim.rs +++ b/src/shim.rs @@ -1,4 +1,4 @@ -use serde::{Deserialize, Serialize}; +use serde::{de::DeserializeOwned, Deserialize, Serialize}; use std::{io, path::Path}; #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] @@ -24,9 +24,7 @@ pub enum Key { } pub fn get_key() -> Key { - let mut json = String::new(); - io::stdin().read_line(&mut json).unwrap(); - serde_json::from_str(&json).unwrap() + deserialize_from_stdin().unwrap() } pub fn open_file(path: &Path) { @@ -39,8 +37,20 @@ pub fn set_selectable(selectable: bool) { unsafe { host_set_selectable(selectable) }; } +pub fn get_help() -> Vec { + unsafe { host_get_help() }; + deserialize_from_stdin().unwrap_or_default() +} + +fn deserialize_from_stdin() -> Option { + let mut json = String::new(); + io::stdin().read_line(&mut json).unwrap(); + serde_json::from_str(&json).ok() +} + #[link(wasm_import_module = "mosaic")] extern "C" { fn host_open_file(); fn host_set_selectable(selectable: i32); + fn host_get_help(); } From 2ed46c5fe6e148f4acb42482da0db674f0aad41a Mon Sep 17 00:00:00 2001 From: Aram Drevekenin Date: Thu, 28 Jan 2021 18:19:27 +0100 Subject: [PATCH 11/12] feat(api): set max height --- src/shim.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/shim.rs b/src/shim.rs index 6fa8b0e6..452d69a9 100644 --- a/src/shim.rs +++ b/src/shim.rs @@ -32,6 +32,10 @@ pub fn open_file(path: &Path) { unsafe { host_open_file() }; } +pub fn set_max_height(max_height: i32) { + unsafe { host_set_max_height(max_height) }; +} + pub fn set_selectable(selectable: bool) { let selectable = if selectable { 1 } else { 0 }; unsafe { host_set_selectable(selectable) }; @@ -51,6 +55,7 @@ fn deserialize_from_stdin() -> Option { #[link(wasm_import_module = "mosaic")] extern "C" { fn host_open_file(); + fn host_set_max_height(max_height: i32); fn host_set_selectable(selectable: i32); fn host_get_help(); } From 36938c504a655c5ae4cde10cf9870ce910fd9c79 Mon Sep 17 00:00:00 2001 From: Aram Drevekenin Date: Tue, 9 Feb 2021 17:12:04 +0100 Subject: [PATCH 12/12] feat(api): set invisible borders --- src/shim.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/shim.rs b/src/shim.rs index 452d69a9..5de8f393 100644 --- a/src/shim.rs +++ b/src/shim.rs @@ -23,6 +23,33 @@ pub enum Key { Esc, } +// TODO: use same struct from main crate? +#[derive(Default, Debug, Clone, Serialize, Deserialize)] +pub struct Help { + pub mode: InputMode, + pub mode_is_persistent: bool, + pub keybinds: Vec<(String, String)>, +} + +// TODO: use same struct from main crate? +#[derive(Debug, Clone, Deserialize, Serialize)] +pub enum InputMode { + Normal, + Command, + CommandPersistent, + Resize, + Pane, + Tab, + Scroll, + Exiting, +} + +impl Default for InputMode { + fn default() -> InputMode { + InputMode::Normal + } +} + pub fn get_key() -> Key { deserialize_from_stdin().unwrap() } @@ -36,12 +63,17 @@ pub fn set_max_height(max_height: i32) { unsafe { host_set_max_height(max_height) }; } +pub fn set_invisible_borders(invisible_borders: bool) { + let invisible_borders = if invisible_borders { 1 } else { 0 }; + unsafe { host_set_invisible_borders(invisible_borders) }; +} + pub fn set_selectable(selectable: bool) { let selectable = if selectable { 1 } else { 0 }; unsafe { host_set_selectable(selectable) }; } -pub fn get_help() -> Vec { +pub fn get_help() -> Help { unsafe { host_get_help() }; deserialize_from_stdin().unwrap_or_default() } @@ -57,5 +89,6 @@ extern "C" { fn host_open_file(); fn host_set_max_height(max_height: i32); fn host_set_selectable(selectable: i32); + fn host_set_invisible_borders(invisible_borders: i32); fn host_get_help(); }