ctrl+c handler & ascii

This commit is contained in:
17ms 2023-05-29 01:50:34 +03:00
parent 9d232bed81
commit e461aa962f
5 changed files with 23 additions and 31 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "contego" name = "contego"
description = "CLI tool for file transfer" description = "CLI tool for quick file transfers"
version = "0.3.0" version = "0.3.0"
edition = "2021" edition = "2021"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

View File

@ -6,11 +6,11 @@ use contego::{
client::Client, client::Client,
parser::{addr_parser, dirpath_parser, filepath_parser}, parser::{addr_parser, dirpath_parser, filepath_parser},
server::Server, server::Server,
util::{filepaths, handle_exit, metadata, Ip}, util::{ascii, filepaths, metadata, Ip},
}; };
use env_logger::Env; use env_logger::Env;
use log::error; use log::{error, info};
use tokio::sync::mpsc; use tokio::{signal, sync::mpsc};
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
#[command(about, version)] #[command(about, version)]
@ -60,6 +60,8 @@ enum Commands {
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> { async fn main() -> Result<(), Box<dyn Error>> {
ascii();
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init(); env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let cli = Cli::parse(); let cli = Cli::parse();
@ -92,7 +94,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
}; };
}); });
handle_exit(tx).await?; match signal::ctrl_c().await {
Ok(_) => {
tx.send(()).await?;
info!("Captured Ctrl+C, shutting down");
}
Err(_) => error!("Failed to listen for a Ctrl+C event"),
};
} }
Commands::Connect { addr, out, key } => { Commands::Connect { addr, out, key } => {
let client = Client::new(addr, key, out); let client = Client::new(addr, key, out);

View File

@ -1,15 +1,7 @@
use std::{ use std::{collections::HashMap, env, error::Error, fs, net::SocketAddr, path::PathBuf};
collections::HashMap,
env,
error::Error,
fs,
io::{stdin, Read},
net::SocketAddr,
path::PathBuf,
};
use log::{debug, info}; use log::{debug, info};
use tokio::{fs::File, io::BufWriter, sync::mpsc::Sender}; use tokio::{fs::File, io::BufWriter};
use crate::crypto; use crate::crypto;
@ -127,20 +119,12 @@ pub async fn new_file(
Ok((BufWriter::new(handle), path)) Ok((BufWriter::new(handle), path))
} }
pub async fn handle_exit(tx: Sender<()>) -> Result<(), Box<dyn Error>> { pub fn ascii() {
let mut stdin = stdin().lock().bytes(); let ascii = " __
_________ ____ / /____ ____ _____
loop { / ___/ __ \\/ __ \\/ __/ _ \\/ __ `/ __ \\
let k = match stdin.next() { / /__/ /_/ / / / / /_/ __/ /_/ / /_/ /
None => continue, \\___/\\____/_/ /_/\\__/\\___/\\__, /\\____/
Some(k) => k?, /____/ ";
}; println!("{}\n", ascii);
if k == b'q' {
tx.send(()).await?;
break;
}
}
Ok(())
} }