at least we're now sending a character
This commit is contained in:
parent
24710414bd
commit
f992a24719
|
@ -25,6 +25,7 @@ pub mod theme;
|
|||
use theme::*;
|
||||
|
||||
pub mod term;
|
||||
use shared::term::{TerminalDownMsg, TerminalScreen};
|
||||
|
||||
#[derive(Clone, Copy, Default)]
|
||||
enum Layout {
|
||||
|
@ -101,8 +102,11 @@ fn main() {
|
|||
.unwrap_throw()
|
||||
.set_component_text(&component_id, &text),
|
||||
}
|
||||
})
|
||||
.await
|
||||
}).await;
|
||||
platform::listen_term_update(|down_msg| {
|
||||
term::TERMINAL_STATE.set(down_msg);
|
||||
}).await;
|
||||
zoon::println!("Printing on line 106");
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -188,11 +192,15 @@ fn root() -> impl Element {
|
|||
TERM_OPEN.signal_cloned().map(
|
||||
|term_open| {
|
||||
match term_open {
|
||||
true => {El::new().child("Terminal")}
|
||||
false => {El::new().child("")}
|
||||
true =>
|
||||
El::new()
|
||||
.s(Height::fill().max(450))
|
||||
.child(term::root()),
|
||||
false =>
|
||||
El::new()
|
||||
.child("")
|
||||
}
|
||||
}
|
||||
)
|
||||
// El::new()
|
||||
)
|
||||
}
|
||||
|
|
|
@ -29,6 +29,8 @@ type DiagramConnectorPath = String;
|
|||
type DiagramConnectorName = String;
|
||||
type ComponentId = String;
|
||||
|
||||
use shared::term::{TerminalDownMsg, TerminalScreen};
|
||||
|
||||
pub async fn show_window() {
|
||||
platform::show_window().await
|
||||
}
|
||||
|
@ -72,6 +74,10 @@ pub async fn unload_signal(signal_ref: wellen::SignalRef) {
|
|||
platform::unload_signal(signal_ref).await
|
||||
}
|
||||
|
||||
pub async fn send_char() {
|
||||
platform::send_char().await
|
||||
}
|
||||
|
||||
pub async fn add_decoders(decoder_paths: Vec<DecoderPath>) -> AddedDecodersCount {
|
||||
let count = platform::add_decoders(decoder_paths).await;
|
||||
if count > 0 {
|
||||
|
@ -112,6 +118,12 @@ pub async fn listen_diagram_connectors_messages(
|
|||
platform::listen_diagram_connectors_messages(on_message).await;
|
||||
}
|
||||
|
||||
pub async fn listen_term_update(
|
||||
on_message: impl FnMut(TerminalDownMsg) + 'static,
|
||||
) {
|
||||
platform::listen_term_update(on_message).await;
|
||||
}
|
||||
|
||||
pub async fn notify_diagram_connector_text_change(
|
||||
diagram_connector: DiagramConnectorName,
|
||||
component_id: ComponentId,
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use shared::DiagramConnectorMessage;
|
||||
use shared::term::{TerminalDownMsg, TerminalScreen};
|
||||
use zoon::*;
|
||||
|
||||
pub(super) async fn show_window() {
|
||||
|
@ -57,6 +58,12 @@ pub(super) async fn unload_signal(signal_ref: wellen::SignalRef) {
|
|||
.unwrap_throw()
|
||||
}
|
||||
|
||||
pub(super) async fn send_char() {
|
||||
tauri_glue::send_char()
|
||||
.await
|
||||
.unwrap_throw()
|
||||
}
|
||||
|
||||
pub(super) async fn add_decoders(
|
||||
decoder_paths: Vec<super::DecoderPath>,
|
||||
) -> super::AddedDecodersCount {
|
||||
|
@ -97,6 +104,14 @@ pub(super) async fn listen_diagram_connectors_messages(
|
|||
tauri_glue::listen_diagram_connectors_messages(Closure::new(on_message).into_js_value()).await
|
||||
}
|
||||
|
||||
pub(super) async fn listen_term_update(
|
||||
mut on_message: impl FnMut(TerminalDownMsg) + 'static,
|
||||
) {
|
||||
let on_message =
|
||||
move |message: JsValue| on_message(serde_wasm_bindgen::from_value(message).unwrap_throw());
|
||||
tauri_glue::listen_term_update(Closure::new(on_message).into_js_value()).await
|
||||
}
|
||||
|
||||
pub(super) async fn notify_diagram_connector_text_change(
|
||||
diagram_connector: super::DiagramConnectorName,
|
||||
component_id: super::ComponentId,
|
||||
|
@ -142,6 +157,9 @@ mod tauri_glue {
|
|||
#[wasm_bindgen(catch)]
|
||||
pub async fn unload_signal(signal_ref_index: usize) -> Result<(), JsValue>;
|
||||
|
||||
#[wasm_bindgen(catch)]
|
||||
pub async fn send_char() -> Result<(), JsValue>;
|
||||
|
||||
#[wasm_bindgen(catch)]
|
||||
pub async fn add_decoders(
|
||||
decoder_paths: Vec<super::super::DecoderPath>,
|
||||
|
@ -160,6 +178,8 @@ mod tauri_glue {
|
|||
|
||||
pub async fn listen_diagram_connectors_messages(on_event: JsValue);
|
||||
|
||||
pub async fn listen_term_update(on_event: JsValue);
|
||||
|
||||
#[wasm_bindgen(catch)]
|
||||
pub async fn notify_diagram_connector_text_change(
|
||||
diagram_connector: super::super::DiagramConnectorName,
|
||||
|
|
|
@ -8,24 +8,11 @@ use shared::term::{TerminalDownMsg, TerminalScreen, TerminalUpMsg};
|
|||
// use tokio::time::timeout;
|
||||
pub static TERM_OPEN: Lazy<Mutable<bool>> = Lazy::new(|| {false.into()});
|
||||
|
||||
static TERMINAL_STATE: Lazy<Mutable<TerminalDownMsg>> =
|
||||
pub static TERMINAL_STATE: Lazy<Mutable<TerminalDownMsg>> =
|
||||
Lazy::new(|| {
|
||||
Mutable::new(TerminalDownMsg::TermNotStarted)
|
||||
});
|
||||
|
||||
// static CONNECTION: Lazy<Connection<UpMsg, DownMsg>> = Lazy::new(|| {
|
||||
// Connection::new(
|
||||
// |down_msg, _| {
|
||||
// match down_msg {
|
||||
// DownMsg::TerminalDownMsg(terminal_msg) => {
|
||||
// TERMINAL_STATE.set(terminal_msg);
|
||||
// }
|
||||
|
||||
// }
|
||||
// }
|
||||
// )
|
||||
// });
|
||||
|
||||
pub fn root() -> impl Element {
|
||||
term_request();
|
||||
let terminal =
|
||||
|
@ -39,7 +26,6 @@ pub fn root() -> impl Element {
|
|||
]))
|
||||
.update_raw_el(|raw_el| {
|
||||
raw_el.global_event_handler(|event: events::KeyDown| {
|
||||
println!("Pressed key: {}", &event.key());
|
||||
send_char(
|
||||
(&event).key().as_str(),
|
||||
(&event).ctrl_key(),
|
||||
|
@ -82,7 +68,13 @@ fn send_char(
|
|||
match process_str(s, has_control) {
|
||||
// TODO : fill this out
|
||||
Some(c) => {
|
||||
eprintln!("Sending char: {}", c);
|
||||
let send_c = c.clone();
|
||||
Task::start(async move {
|
||||
println!("Sending char: {}", &c);
|
||||
crate::platform::send_char().await;
|
||||
// crate::platform::unload_signal().await;
|
||||
println!("Sent char: {}", &c);
|
||||
});
|
||||
}
|
||||
None => {eprintln!("Not processing: {}", s)}
|
||||
}
|
||||
|
@ -94,7 +86,7 @@ fn make_grid_with_newlines(term : &TerminalScreen) -> String {
|
|||
let mut formatted = String::new();
|
||||
for (i, c) in term.content.chars().enumerate() {
|
||||
formatted.push(c);
|
||||
if (i + 1) % term.cols == 0 {
|
||||
if ((i + 1) as u16) % term.cols == 0 {
|
||||
formatted.push('\n');
|
||||
}
|
||||
}
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -59,6 +59,11 @@ export async function unload_signal(signal_ref_index: number): Promise<void> {
|
|||
return await invoke("unload_signal", { signal_ref_index });
|
||||
}
|
||||
|
||||
export async function send_char(): Promise<void> {
|
||||
const char = "a";
|
||||
return await invoke("send_char", { char });
|
||||
}
|
||||
|
||||
export async function add_decoders(decoder_paths: Array<DecoderPath>): Promise<AddedDecodersCount> {
|
||||
return await invoke("add_decoders", { decoder_paths });
|
||||
}
|
||||
|
@ -79,6 +84,10 @@ export async function listen_diagram_connectors_messages(on_message: (message: a
|
|||
return await listen("diagram_connector_message", (message) => on_message(message.payload));
|
||||
}
|
||||
|
||||
export async function listen_term_update(on_message: (message: any) => void) {
|
||||
return await listen("term_content", (message) => on_message(message.payload));
|
||||
}
|
||||
|
||||
export async function notify_diagram_connector_text_change(diagram_connector: DiagramConnectorName, component_id: ComponentId, text: string): Promise<void> {
|
||||
return await invoke("notify_diagram_connector_text_change", { diagram_connector, component_id, text });
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ pub enum TerminalDownMsg {
|
|||
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
|
||||
#[serde(crate = "serde")]
|
||||
pub struct TerminalScreen {
|
||||
pub cols : usize,
|
||||
pub rows : usize,
|
||||
pub cols : u16,
|
||||
pub rows : u16,
|
||||
pub content : String,
|
||||
}
|
||||
|
|
116
src-tauri/src/aterm.rs
Normal file
116
src-tauri/src/aterm.rs
Normal file
|
@ -0,0 +1,116 @@
|
|||
use std::result;
|
||||
use std::sync::{mpsc, Arc};
|
||||
|
||||
use alacritty_terminal::event::{Event, EventListener};
|
||||
use alacritty_terminal::event_loop::{EventLoop, Notifier};
|
||||
use alacritty_terminal::sync::FairMutex;
|
||||
use alacritty_terminal::term::{self, Term};
|
||||
use alacritty_terminal::term::cell::Cell;
|
||||
use alacritty_terminal::{tty, Grid};
|
||||
use tauri::Emitter;
|
||||
use shared::term::{TerminalDownMsg, TerminalScreen};
|
||||
|
||||
use crate::terminal_size;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct EventProxy(mpsc::Sender<Event>);
|
||||
impl EventListener for EventProxy {
|
||||
fn send_event(&self, event: Event) {
|
||||
let _ = self.0.send(event.clone());
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ATerm {
|
||||
pub term: Arc<FairMutex<Term<EventProxy>>>,
|
||||
|
||||
rows : u16,
|
||||
cols : u16,
|
||||
|
||||
/// Use tx to write things to terminal instance from outside world
|
||||
pub tx: Notifier,
|
||||
|
||||
/// Use rx to read things from terminal instance.
|
||||
/// Rx only has data when terminal state has changed,
|
||||
/// otherwise, `std::sync::mpsc::recv` will block and sleep
|
||||
/// until there is data.
|
||||
pub rx: mpsc::Receiver<(u64, Event)>,
|
||||
}
|
||||
|
||||
impl ATerm {
|
||||
pub fn new() -> result::Result<ATerm, std::io::Error> {
|
||||
let (rows, cols) = (21, 158);
|
||||
let id = 1;
|
||||
let pty_config = tty::Options {
|
||||
shell: Some(tty::Shell::new("/bin/bash".to_string(), vec![])),
|
||||
..tty::Options::default()
|
||||
};
|
||||
let config = term::Config::default();
|
||||
let terminal_size = terminal_size::TerminalSize::new(rows, cols);
|
||||
let pty = tty::new(&pty_config, terminal_size.into(), id)?;
|
||||
let (event_sender, event_receiver) = mpsc::channel();
|
||||
let event_proxy = EventProxy(event_sender);
|
||||
let term = Term::new::<terminal_size::TerminalSize>(
|
||||
config,
|
||||
&terminal_size.into(),
|
||||
event_proxy.clone(),
|
||||
);
|
||||
let term = Arc::new(FairMutex::new(term));
|
||||
let pty_event_loop = EventLoop::new(term.clone(), event_proxy, pty, false, false)?;
|
||||
let notifier = Notifier(pty_event_loop.channel());
|
||||
let (pty_proxy_sender, pty_proxy_receiver) = std::sync::mpsc::channel();
|
||||
// Start pty event loop
|
||||
pty_event_loop.spawn();
|
||||
std::thread::Builder::new()
|
||||
.name(format!("pty_event_subscription_{}", id))
|
||||
.spawn(move || loop {
|
||||
if let Ok(event) = event_receiver.recv() {
|
||||
if let Event::Exit = event {
|
||||
break;
|
||||
}
|
||||
else {
|
||||
if let Some(app_handle) = crate::APP_HANDLE.read().unwrap().clone() {
|
||||
let term = crate::TERM.lock().unwrap();
|
||||
let content = terminal_instance_to_string(&term);
|
||||
let payload = TerminalScreen {
|
||||
cols: term.cols,
|
||||
rows: term.rows,
|
||||
content: content
|
||||
};
|
||||
let payload = TerminalDownMsg::FullTermUpdate(payload);
|
||||
let payload = serde_json::json!(payload);
|
||||
app_handle.emit("term_content", payload).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
})?;
|
||||
Ok(ATerm {
|
||||
term,
|
||||
rows,
|
||||
cols,
|
||||
tx: notifier,
|
||||
rx: pty_proxy_receiver,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn terminal_instance_to_string(terminal_instance: &ATerm) -> String {
|
||||
let (rows, cols) = (terminal_instance.rows, terminal_instance.cols);
|
||||
let term = terminal_instance.term.lock();
|
||||
let grid = term.grid().clone();
|
||||
|
||||
return term_grid_to_string(&grid, rows, cols);
|
||||
}
|
||||
|
||||
fn term_grid_to_string(grid: &Grid<Cell>, rows: u16, cols: u16) -> String {
|
||||
let mut term_content = String::with_capacity((rows*cols) as usize);
|
||||
|
||||
// Populate string from grid
|
||||
for indexed in grid.display_iter() {
|
||||
let x = indexed.point.column.0 as usize;
|
||||
let y = indexed.point.line.0 as usize;
|
||||
if y < rows as usize && x < cols as usize {
|
||||
term_content.push(indexed.c);
|
||||
}
|
||||
}
|
||||
return term_content;
|
||||
}
|
|
@ -24,10 +24,17 @@ type DiagramConnectorName = String;
|
|||
type ComponentId = String;
|
||||
|
||||
mod component_manager;
|
||||
mod aterm;
|
||||
mod terminal_size;
|
||||
use std::sync::Mutex;
|
||||
|
||||
pub static APP_HANDLE: Lazy<Arc<StdRwLock<Option<AppHandle>>>> = Lazy::new(<_>::default);
|
||||
pub static WAVEFORM: Lazy<StdRwLock<Arc<RwLock<Option<Waveform>>>>> = Lazy::new(<_>::default);
|
||||
|
||||
static TERM: Lazy<Mutex<aterm::ATerm>> = Lazy::new(|| {
|
||||
Mutex::new(aterm::ATerm::new().expect("Failed to initialize ATerm"))
|
||||
});
|
||||
|
||||
#[derive(Default)]
|
||||
struct Store {
|
||||
waveform: Arc<RwLock<Option<Waveform>>>,
|
||||
|
@ -146,6 +153,12 @@ async fn unload_signal(signal_ref_index: usize, store: tauri::State<'_, Store>)
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command(rename_all = "snake_case")]
|
||||
async fn send_char() -> Result<(), ()> {
|
||||
println!("Sending char: {}", "a");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command(rename_all = "snake_case")]
|
||||
async fn add_decoders(decoder_paths: Vec<DecoderPath>) -> Result<AddedDecodersCount, ()> {
|
||||
Ok(component_manager::decoders::add_decoders(decoder_paths).await)
|
||||
|
@ -281,6 +294,7 @@ pub fn run() {
|
|||
get_hierarchy,
|
||||
load_signal_and_get_timeline,
|
||||
unload_signal,
|
||||
send_char,
|
||||
add_decoders,
|
||||
remove_all_decoders,
|
||||
add_diagram_connectors,
|
||||
|
|
Loading…
Reference in a new issue