Skip to content

Commit a58d246

Browse files
author
replydev
committed
Clear interface on exit
1 parent 512c8b5 commit a58d246

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ ascii = "1.0"
2323
data-encoding = "2.3.1"
2424
err-derive = "0.3.0"
2525
ring = "0.16.19"
26+
ctrlc = "3.1.7"

src/main.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
use std::env;
21
mod database_loader;
32
mod utils;
43
mod argument_functions;
54
mod otp_helper;
65
mod cryptograpy;
76
mod importers;
87
mod otp;
8+
use std::env;
99
use sodiumoxide;
1010
use std::thread::sleep;
1111
use std::time::Duration;
12+
use ctrlc;
1213

1314
const VERSION: &str = "0.1.3";
1415

@@ -25,6 +26,22 @@ fn print_title(){
2526
println!("written by @replydev\n");
2627
}
2728

29+
#[cfg(debug_assertions)]
30+
fn init_ctrlc_handler(lines: usize){
31+
ctrlc::set_handler(move || {
32+
utils::clear_lines(lines + 7);
33+
std::process::exit(0);
34+
}).expect("Failed to initialize ctrl-c handler");
35+
}
36+
37+
#[cfg(not(debug_assertions))]
38+
fn init_ctrlc_handler(lines: usize){
39+
ctrlc::set_handler(move || {
40+
utils::clear_lines(lines + 6);
41+
std::process::exit(0);
42+
}).expect("Failed to initialize ctrl-c handler");
43+
}
44+
2845
fn init() -> Result<(), String>{
2946
match sodiumoxide::init(){
3047
Err(()) => {
@@ -65,22 +82,22 @@ fn main() {
6582
}
6683

6784
fn dashboard(){
68-
let mut lines;
6985
match otp_helper::read_codes(){
7086
Ok(elements) => {
7187
if elements.len() == 0{
7288
println!("No codes, type \"cotp -h\" to get help");
7389
}
7490
else{
91+
init_ctrlc_handler(elements.len());
7592
loop{
7693
utils::print_progress_bar();
77-
lines = otp_helper::show_codes(&elements);
94+
otp_helper::show_codes(&elements);
7895
sleep(Duration::from_millis(1000));
79-
print!("\x1B[{}A", lines + 1);
96+
utils::clear_lines(elements.len() + 1);
8097
}
8198
}
8299
},
83-
Err(e) => println!("An error occurred: {}",e),
100+
Err(e) => eprintln!("An error occurred: {}",e),
84101
}
85102
}
86103

src/otp_helper.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@ pub fn read_codes() -> Result<Vec<database_loader::OTPElement>,String>{
2929
}
3030
}
3131

32-
pub fn show_codes(elements: &Vec<database_loader::OTPElement>) -> usize{
33-
//let elements;
32+
pub fn show_codes(elements: &Vec<database_loader::OTPElement>){
3433
for i in 0..elements.len() {
3534
print_totp(i,&elements[i]);
3635
}
37-
elements.len()
3836
}
3937

4038
fn print_totp(i: usize,element: &database_loader::OTPElement){

src/utils.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ pub fn print_progress_bar(){
5858
println!("[{:60}]", "=".repeat(idx as usize));
5959
}
6060

61+
pub fn clear_lines(lines: usize){
62+
// \x1B[{}A does not work during ctrl clear
63+
print!("\x1B[{}A\x1B[0G\x1B[0J", lines);
64+
}
65+
6166
#[cfg(test)]
6267
mod tests{
6368
use super::create_db_if_needed;

0 commit comments

Comments
 (0)