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]
name = "contego"
description = "CLI tool for file transfer"
description = "CLI tool for quick file transfers"
version = "0.3.0"
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,
parser::{addr_parser, dirpath_parser, filepath_parser},
server::Server,
util::{filepaths, handle_exit, metadata, Ip},
util::{ascii, filepaths, metadata, Ip},
};
use env_logger::Env;
use log::error;
use tokio::sync::mpsc;
use log::{error, info};
use tokio::{signal, sync::mpsc};
#[derive(Debug, Parser)]
#[command(about, version)]
@ -60,6 +60,8 @@ enum Commands {
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
ascii();
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
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 } => {
let client = Client::new(addr, key, out);

View File

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