Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/bin/taskboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl TaskBoard {
self.load_terminal_dimnsions();
self.clear_screen();

let orig_termios = change_to_raw_mode();
let mut orig_termios = change_to_raw_mode();

let mut buf: [u8; 1] = [0; 1];

Expand All @@ -72,7 +72,7 @@ impl TaskBoard {
self.display_content();
}
self.clear_screen();
reset_to_termios(orig_termios);
reset_to_termios(&mut orig_termios);
}

fn display_content(&mut self) {
Expand Down
22 changes: 15 additions & 7 deletions src/bin/taskshell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use tasklib::{
self,
args::{Args, EngineSubcommand, ShellCommand, help},
},
termios::{change_to_raw_mode, reset_to_termios},
};

use tasklib::jsonrpc::request::Request;
Expand Down Expand Up @@ -182,7 +183,11 @@ fn build_request(command: &ShellCommand) -> BuildRequestResult {
}
}

async fn attach(name: &str, socket_path: &str, to: &str) -> String {
async fn attach(name: &str, socket_path: &str, to: &str, mut orig: Option<&mut libc::termios>) -> String {
if let Some(o) = orig.as_mut() {
reset_to_termios(o);
}

let (tx, mut rx) = tokio::sync::mpsc::channel::<()>(1);
let tx_clone = tx.clone();

Expand Down Expand Up @@ -219,10 +224,13 @@ async fn attach(name: &str, socket_path: &str, to: &str) -> String {
}
}
}
if let Some(orig) = orig {
*orig = change_to_raw_mode();
}
"".to_string()
}

async fn response_to_str(response: &Response) -> String {
async fn response_to_str(response: &Response, orig: Option<&mut libc::termios>) -> String {
match response.response_type() {
ResponseType::Result(res) => {
use tasklib::jsonrpc::response::ResponseResult::*;
Expand All @@ -238,14 +246,14 @@ async fn response_to_str(response: &Response) -> String {
Restart(name) => format!("restarting: {name}"),
Reload => "reloading configuration".to_string(),
Halt => "shutting down taskmaster".to_string(),
Attach { name, socketpath, to } => attach(name, socketpath, to).await,
Attach { name, socketpath, to } => attach(name, socketpath, to, orig).await,
}
}
ResponseType::Error(err) => err.message.to_string(),
}
}

async fn handle_input(input: Vec<String>) -> Result<String, String> {
async fn handle_input(input: Vec<String>, orig: Option<&mut libc::termios>) -> Result<String, String> {
let arguments = Args::try_from(input)?;

let request = match build_request(arguments.command()) {
Expand Down Expand Up @@ -282,7 +290,7 @@ async fn handle_input(input: Vec<String>) -> Result<String, String> {
};
response.set_response_result(request.request_type());

Ok(response_to_str(&response).await)
Ok(response_to_str(&response, orig).await)
}

fn print_raw_mode(string: &str) {
Expand All @@ -297,7 +305,7 @@ fn print_raw_mode(string: &str) {
async fn shell() {
let mut shell = shell::Shell::new("taskshell> ");
while let Some(line) = shell.next_line() {
let msg = match handle_input(line.split_ascii_whitespace().map(String::from).collect::<Vec<String>>()).await {
let msg = match handle_input(line.split_ascii_whitespace().map(String::from).collect::<Vec<String>>(), Some(shell.orig_mut())).await {
Ok(s) => s,
Err(s) => s,
};
Expand All @@ -322,7 +330,7 @@ async fn main() {

match args.len() {
0 => shell().await,
_ => match handle_input(args).await {
_ => match handle_input(args, None).await {
Ok(data) => print_raw_mode(&format!("{data}\n")),
Err(e) => {
print_raw_mode(&e.to_string());
Expand Down
6 changes: 5 additions & 1 deletion src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct Shell {

impl Drop for Shell {
fn drop(&mut self) {
crate::termios::reset_to_termios(self.orig);
crate::termios::reset_to_termios(&mut self.orig);
}
}

Expand Down Expand Up @@ -61,6 +61,10 @@ impl Shell {
}
}

pub fn orig_mut(&mut self) -> &mut libc::termios {
&mut self.orig
}

pub fn next_line(&mut self) -> Option<String> {
let mut waiting_for_arrow_command = false;
let mut history_index = 0;
Expand Down
23 changes: 21 additions & 2 deletions src/termios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@ unsafe extern "C" {
unsafe fn cfmakeraw(termios: *mut termios);
}

pub fn get_current_termios() -> libc::termios {
let mut orig: libc::termios = libc::termios {
c_iflag: 0,
c_oflag: 0,
c_cflag: 0,
c_lflag: 0,
c_line: 0,
c_cc: [0; NCCS],
c_ispeed: 0,
c_ospeed: 0,
};

unsafe {
tcgetattr(0, &raw mut orig);
}

orig
}

pub fn change_to_raw_mode() -> libc::termios {
let mut orig: libc::termios = libc::termios {
c_iflag: 0,
Expand Down Expand Up @@ -34,8 +53,8 @@ pub fn change_to_raw_mode() -> libc::termios {
orig
}

pub fn reset_to_termios(orig: libc::termios) {
pub fn reset_to_termios(orig: &mut libc::termios) {
unsafe {
tcsetattr(0, TCSAFLUSH, &orig);
tcsetattr(0, TCSAFLUSH, orig);
}
}