1- use std:: env;
21mod database_loader;
32mod utils;
43mod argument_functions;
54mod otp_helper;
65mod cryptograpy;
76mod importers;
87mod otp;
8+ use std:: env;
99use sodiumoxide;
1010use std:: thread:: sleep;
1111use std:: time:: Duration ;
12+ use ctrlc;
1213
1314const 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+
2845fn init ( ) -> Result < ( ) , String > {
2946 match sodiumoxide:: init ( ) {
3047 Err ( ( ) ) => {
@@ -65,22 +82,22 @@ fn main() {
6582}
6683
6784fn 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
0 commit comments