Skip to content

Commit

Permalink
chore: Update all versions of dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
AMythicDev committed Jul 27, 2023
1 parent 76a9ad0 commit 39224c3
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 28 deletions.
22 changes: 17 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ categories = ["Text processing", "Command-line interface", "Asynchronous"]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[lib]
name = "minus"
path = "src/lib.rs"
crate-type = ["lib"]

[dependencies]
crossterm = "0.22.1"
textwrap = { version = "~0.13", default-features = false, features = ["unicode-width"] }
crossterm = "^0.26"
textwrap = { version = "~0.16", default-features = false, features = ["unicode-width"] }
thiserror = "^1"
regex = { version = ">=1.5.5", optional = true }
crossbeam-channel = "0.5.1"
regex = { version = "^1", optional = true }
crossbeam-channel = "^0.5"
parking_lot = "0.12.1"
once_cell = { version = "1.15.0", features = ["parking_lot"] }
once_cell = { version = "^1.18", features = ["parking_lot"] }

[features]
search = [ "regex" ]
Expand All @@ -34,28 +39,35 @@ tokio = { version = "^1.0", features = ["rt", "macros", "rt-multi-thread", "time

[[example]]
name = "dyn_tokio"
path = "examples/dyn_tokio.rs"
required-features = ["dynamic_output"]

[[example]]
name = "less-rs"
path = "examples/less-rs.rs"
required-features = ["dynamic_output"]

[[example]]
name = "static"
path = "examples/static.rs"
required-features = ["static_output"]

[[example]]
name = "large_lines"
path = "examples/large-lines.rs"
required-features = ["static_output"]

[[example]]
name = "color-output"
path = "examples/color-output.rs"
required-features = ["static_output"]

[[example]]
name = "static-no-overflow"
path = "examples/static-no-overflow.rs"
required-features = ["static_output"]

[[example]]
name = "msg-tokio"
path = "examples/msg-tokio.rs"
required-features = ["dynamic_output"]
3 changes: 3 additions & 0 deletions src/core/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub fn fetch_input(
Event::Key(KeyEvent {
code: KeyCode::Esc,
modifiers: KeyModifiers::NONE,
..
}) => {
write!(out, "{}", cursor::Hide)?;
return Ok(String::new());
Expand All @@ -88,6 +89,7 @@ pub fn fetch_input(
Event::Key(KeyEvent {
code: KeyCode::Backspace,
modifiers: KeyModifiers::NONE,
..
}) => {
string.pop();
// Update the line
Expand All @@ -97,6 +99,7 @@ pub fn fetch_input(
Event::Key(KeyEvent {
code: KeyCode::Enter,
modifiers: KeyModifiers::NONE,
..
}) => {
write!(out, "{}", cursor::Hide)?;
// Return the string when enter is pressed
Expand Down
115 changes: 115 additions & 0 deletions src/data_store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//! Provides [DataStore] for storing variable data
//!
//! While building complex applications with minus, it is sometimes necessory to store variable data
//! that should be available while configuring minus. Lets say you want to know when the user presses `G`
//! and maybe use this information to call some other part of your application, you may be tempted use
//! something like this
//! ```rust,compile_fail
//! # use minus::input::{HashedEventRegister, InputEvent};
//!
//! let mut pressed_G = false;
//!
//! let mut event_register = HashedEventRegister::default();
//! event_register.add_key_events(&["G"], |_, ps|{
//! pressed_G = true;
//! // ...
//! # InputEvent::NextMatch
//! });
//! ```
//! But this will cause a compilation error and that's quite obvious. The `pressed_G` function may go out of scope
//! whenever the function goes out of scope, but `add_key_events` requies all variables to be `'static` i.e. they
//! should'nt go out of scope till these callbacks go out of scope.
//!
//! You can use other workaround for this problem but minus provides the [DataStore] type that can easily solve this
//! problem for you. Lets see how you can solve this easily with the help of [DataStore]
//! ```rust
//! # use minus::input::{HashedEventRegister, InputEvent};
//!
//! let mut event_register = HashedEventRegister::default();
//! event_register.add_key_events(&["G"], |_, ps|{
//! ps.store.push("pressed_G", Box::new(true));
//! // ...
//! # InputEvent::NextMatch
//! });
//! ```
//! Out of everything the most important line is this one.
//! ```text
//! ps.store.push("pressed_G", Box::new(true));
//! ```
//! Lets talk about it in detail.
//!
//! Any data that you store in the store must be associated with a name. In this case, the first argument `pressed_G`
//! is that name. It can be anything that can be converted to a [String]. In other words, it must implement
//! `Into<String>`. The name will be useful when you want to access that data again.
//! The next argument of course is the value
//! that we want to store. The data must be wrapped inside a [Box]. This might seem a bit annoying but it allows the
//! [DataStore] to store all sorts of data inside it. Do note that whatever data you store must implement [Hash],
//! [Send], [Sync] and must have a `'static` lifetime.
//!
//! But how do you access that data in other parts of your application? Well, for this you need to do some
use std::collections::HashMap;
use std::hash::Hash;
use std::sync::Arc;
use parking_lot::Mutex;

pub trait Hashable {}

impl<T> Hashable for T where T: Hash + Send + Sync + 'static {
}


pub trait HashableType: Hashable + Send + Sync + 'static {

}

// pub trait HashableTypeExit: Sized + DynClone {
// fn into_box(&self) -> Box<dyn HashableType>;
// }
//
// impl HashableTypeExit for &dyn HashableType {
// fn into_box(&self) -> Box<dyn HashableType> {
// Box::new(*self.clone())
// }
// }


pub struct DataStore(Arc<Mutex<HashMap<String, Arc<dyn HashableType>>>>);

impl DataStore {
pub fn new() -> Self {
Self(Arc::new(Mutex::new(HashMap::new())))
}

pub fn push(&self, k: impl Into<String>, v: Box<dyn HashableType>) {
let mut map = self.0.lock();

let v = *v.as_ref();


let v = Arc::from(v);
map.insert(k.into(), v);
}

pub fn remove(&self, k: impl Into<String>) {
let mut map = self.0.lock();
map.remove(&k.into());
}

pub fn get<K>(&self, k: impl Into<String>) -> Option<Box<dyn HashableType>>
where K: Into<String> {
let map = self.0.lock();
map.get(&k.into()).map(|v| {
v.data.clone().as_ref()
// let t = v.as_ref().clone();
// t.into_box()
})
}
}

impl Default for DataStore {
fn default() -> Self {
let ds = Self::new();
ds
}
}
Loading

0 comments on commit 39224c3

Please sign in to comment.