Command Input
This commit is contained in:
@@ -10,6 +10,7 @@ pub struct Context {
|
|||||||
pub(crate) tx: Sender<Box<dyn Any + Send + Sync>>,
|
pub(crate) tx: Sender<Box<dyn Any + Send + Sync>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct State<T> {
|
pub struct State<T> {
|
||||||
pub value: Arc<Mutex<T>>,
|
pub value: Arc<Mutex<T>>,
|
||||||
tx: Sender<Box<dyn Any + Send + Sync>>,
|
tx: Sender<Box<dyn Any + Send + Sync>>,
|
||||||
@@ -46,6 +47,15 @@ impl<T: std::fmt::Display> State<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Clone for State<T> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
State {
|
||||||
|
value: self.value.clone(),
|
||||||
|
tx: self.tx.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a, T> std::ops::Deref for StateGuard<'a, T> {
|
impl<'a, T> std::ops::Deref for StateGuard<'a, T> {
|
||||||
type Target = T;
|
type Target = T;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,93 @@
|
|||||||
|
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
|
||||||
|
|
||||||
|
use crate::widget::Widget;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct InputState {
|
||||||
|
pub text: String,
|
||||||
|
pub cursor: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl InputState {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
text: String::new(),
|
||||||
|
cursor: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Input<'a>(pub &'a str, pub &'a InputState);
|
||||||
|
|
||||||
|
impl<'a> Widget for Input<'a> {
|
||||||
|
fn render(&self, scope: &mut crate::render::RenderScope) {
|
||||||
|
let mut text = self.1.text.clone();
|
||||||
|
text.insert_str(self.1.cursor, "❘");
|
||||||
|
scope.draw_text(0, format!("{}{text}", self.0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl InputState {
|
||||||
|
pub fn handle_event(&mut self, event: &Event) -> bool {
|
||||||
|
match event {
|
||||||
|
Event::Key(KeyEvent {
|
||||||
|
code: KeyCode::Left,
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
|
if self.cursor > 0 {
|
||||||
|
self.cursor -= 1;
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
Event::Key(KeyEvent {
|
||||||
|
code: KeyCode::Right,
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
|
if self.cursor < self.text.len() {
|
||||||
|
self.cursor += 1;
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
Event::Key(KeyEvent {
|
||||||
|
code: KeyCode::Backspace,
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
|
if self.cursor > 0 {
|
||||||
|
let prev = self.text[..self.cursor]
|
||||||
|
.char_indices()
|
||||||
|
.last()
|
||||||
|
.map(|(i, _)| i)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
self.text.remove(prev);
|
||||||
|
self.cursor = prev;
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
Event::Key(KeyEvent {
|
||||||
|
code: KeyCode::Char(c),
|
||||||
|
modifiers,
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
|
if !modifiers.contains(KeyModifiers::CONTROL)
|
||||||
|
&& !modifiers.contains(KeyModifiers::ALT)
|
||||||
|
{
|
||||||
|
self.text.insert(self.cursor, *c);
|
||||||
|
self.cursor += c.len_utf8();
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
Event::Paste(content) => {
|
||||||
|
self.text.insert_str(self.cursor, content);
|
||||||
|
self.cursor += content.len();
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ pub mod align;
|
|||||||
pub mod outline;
|
pub mod outline;
|
||||||
pub mod spaced;
|
pub mod spaced;
|
||||||
pub mod dynamic;
|
pub mod dynamic;
|
||||||
|
pub mod input;
|
||||||
|
|
||||||
use crate::render::RenderScope;
|
use crate::render::RenderScope;
|
||||||
|
|
||||||
|
|||||||
+20
-10
@@ -3,16 +3,19 @@ use std::any::Any;
|
|||||||
use pulse_ui::{
|
use pulse_ui::{
|
||||||
App,
|
App,
|
||||||
layout::{LayoutItem, layout},
|
layout::{LayoutItem, layout},
|
||||||
state::Refresh,
|
state::{Refresh, State},
|
||||||
unit::Size,
|
unit::Size,
|
||||||
widget::{
|
widget::{
|
||||||
align::Center,
|
align::Center,
|
||||||
|
input::{Input, InputState},
|
||||||
outline::{Outline, VLine},
|
outline::{Outline, VLine},
|
||||||
spaced::{SpacedColumns, SpacedRows},
|
spaced::SpacedColumns,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct PulseTradeApp {}
|
pub struct PulseTradeApp {
|
||||||
|
command: State<InputState>,
|
||||||
|
}
|
||||||
|
|
||||||
impl App for PulseTradeApp {
|
impl App for PulseTradeApp {
|
||||||
async fn init(&mut self, ctx: &pulse_ui::state::Context) {}
|
async fn init(&mut self, ctx: &pulse_ui::state::Context) {}
|
||||||
@@ -22,6 +25,12 @@ impl App for PulseTradeApp {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(event) = event.downcast_ref() {
|
||||||
|
if self.command.value.lock().await.handle_event(event) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ctx.close().await;
|
ctx.close().await;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,12 +126,8 @@ impl App for PulseTradeApp {
|
|||||||
(
|
(
|
||||||
LayoutItem::Widget(Size::Flex(1)),
|
LayoutItem::Widget(Size::Flex(1)),
|
||||||
Box::new(
|
Box::new(
|
||||||
vec![
|
vec!["SIGNALS", "BUY BTC LIM 118,800.12", "STL BTC 120,00.00"]
|
||||||
"SIGNALS",
|
.join("\n"),
|
||||||
"BUY BTC LIM 118,800.12",
|
|
||||||
"STL BTC 120,00.00",
|
|
||||||
]
|
|
||||||
.join("\n"),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
@@ -140,10 +145,15 @@ impl App for PulseTradeApp {
|
|||||||
),
|
),
|
||||||
]),
|
]),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
layout.draw(6, Input("> ", &*self.command.lock().await));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
pulse_ui::run(|_ctx| PulseTradeApp {}).await;
|
pulse_ui::run(|ctx| PulseTradeApp {
|
||||||
|
command: ctx.use_state(InputState::new()),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user