Skip to content

Commit

Permalink
added support for notifications
Browse files Browse the repository at this point in the history
Signed-off-by: wadeking98 <wkingnumber2@gmail.com>
  • Loading branch information
wadeking98 committed Oct 25, 2023
1 parent c766b6b commit 0457805
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
14 changes: 10 additions & 4 deletions src/input/input.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{io::stdin, sync::Arc};

use rustyline::{history::MemHistory, Editor};
use rustyline::{
history::History,
Editor,
};
use termion::{
event::{Event, Key},
input::TermReadEventsAndRaw,
Expand All @@ -13,10 +16,13 @@ use tokio::{
task,
};

pub async fn read_line(
rl: Arc<Mutex<Editor<(), MemHistory>>>,
pub async fn read_line<H>(
rl: Arc<Mutex<Editor<(), H>>>,
prompt: Option<&str>,
) -> Result<String, RecvError> {
) -> Result<String, RecvError>
where
H: History + Send + 'static,
{
let (tx, rx) = oneshot::channel::<String>();
let input_prompt = match prompt {
Some(s) => String::from(s),
Expand Down
55 changes: 40 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ use std::collections::HashMap;
use std::env::{self, set_current_dir};
use std::sync::Arc;

use input::input::read_line;
use menu::menu_list::clear;
use rustyline::DefaultEditor;
use std::process::{Command, exit};
use std::process::{exit, Command};
use termion::raw::IntoRawMode;

use crate::menu::menu_list;
use crate::socket::{connection, listener};
use connection::{handle_new_shell, Handle};
use futures_util::pin_mut;
use futures_util::stream::StreamExt;
use std::io::stdout;
use termion::{self, color};
use std::io::{stdout, Write};
use termion::{self, clear, color, cursor};
use tokio::sync::Mutex;
use connection::{Handle, handle_new_shell};

mod input;
mod menu;
Expand Down Expand Up @@ -44,7 +45,7 @@ fn input_loop(
init_message: Option<String>,
) {
tokio::spawn(async move {
let mut menu_rl = DefaultEditor::new().unwrap();
let menu_rl = Arc::new(Mutex::new(DefaultEditor::new().unwrap()));
clear();
if init_message.is_some() {
let msg = init_message.unwrap();
Expand All @@ -56,11 +57,8 @@ fn input_loop(
let stdout = stdout().into_raw_mode().unwrap();
stdout.suspend_raw_mode().unwrap();
let (prompt, home) = get_prompt();
let content = match menu_rl.readline(prompt.as_str()) {
Ok(line) => {
menu_rl.add_history_entry(line.as_str()).unwrap_or_default();
line
}
let content = match read_line(menu_rl.clone(), Some(&prompt)).await {
Ok(line) => line,
Err(_) => continue,
};

Expand Down Expand Up @@ -125,9 +123,7 @@ async fn main() {
println!("Usage: {0} OR {0} <address> <port>", args[0]);
return;
}
let connected_shells = Arc::new(Mutex::new(
HashMap::<String, Handle>::new(),
));
let connected_shells = Arc::new(Mutex::new(HashMap::<String, Handle>::new()));
let menu = menu_list::new();

// get user input
Expand All @@ -142,12 +138,42 @@ async fn main() {

loop {
let soc = match socket_stream.next().await.unwrap() {
Ok(val)=>val,
Ok(val) => val,
Err(_) => {
eprintln!("\nError address already in use {bound_addr}:{bound_port}");
exit(1)
}
};
// display notification, I know this is gross but it's the best I can do with rustyline getting in the way :(
let mut stdout = stdout();
let notification = format!(
"{goto}{clear}{success}new shell received from!{reset}",
goto = cursor::Goto(1, 1),
clear = clear::CurrentLine,
success = color::Fg(color::LightGreen),
addr = soc.peer_addr().unwrap().to_string(),
reset = color::Fg(color::Reset),
);
// save cursor position
stdout
.write_all(&"\x1B7".as_bytes())
.unwrap();
stdout.flush().unwrap();

stdout
.write_all(
&notification
.as_bytes(),
)
.unwrap();
stdout.flush().unwrap();

// restore cursor position
stdout
.write_all(&"\x1B8".as_bytes())
.unwrap();
stdout.flush().unwrap();

handle_new_shell(soc, connected_shells.clone(), None).await;
}
}
Expand All @@ -163,5 +189,4 @@ mod tests {
.starts_with(format!("{red}crab_trap 🦀:", red = color::Fg(color::LightRed)).as_str()));
assert_ne!(home, "");
}

}
4 changes: 2 additions & 2 deletions src/menu/menu_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use tokio::task::JoinHandle;
use tokio::{join, select};
use tokio_util::sync::CancellationToken;

use crate::input::input;
use crate::input::input::{self, read_line};
use crate::socket::connection;

pub type MenuListValue = Box<
Expand Down Expand Up @@ -91,7 +91,7 @@ async fn soc_write(handle: Handle, cancel_token: CancellationToken) {
};
} else {
let cancel_fut = cancel_token.cancelled();
let input_future = input::read_line(handle.readline.clone(), None);
let input_future = read_line(handle.readline.clone(), None);
select! {
res = input_future =>{
if res.is_err(){
Expand Down

0 comments on commit 0457805

Please sign in to comment.