@@ -7,52 +7,40 @@ const dirPath = new URL("./../database/task-3", import.meta.url).pathname;
7
7
8
8
console . log ( c . yellow . bold ( "LOGIN: " ) ) ;
9
9
let rl = readline . createInterface ( { input : process . stdin , output : process . stdout } ) ;
10
- rl . _writeToOutput = function _writeToOutput ( stringToWrite ) {
11
- if ( rl . stdoutMuted ) rl . output . write ( "*" ) ;
12
- else rl . output . write ( stringToWrite ) ;
13
- } ;
10
+ rl . on ( "SIGINT" , closeProgram ) ; //doesn't work on process.on('SIGINT') SIGINT is emitted on ctrl+c press
11
+
14
12
let username = await rl . question ( c . bgBlue . bold ( "login:" ) + " " ) ;
15
13
let password = await rl . question ( c . bgBlue . bold ( "password:" ) + " " ) ;
16
14
if ( ! validateLogin ( username , password ) ) {
17
- rl . close ( ) ;
18
- console . clear ( ) ;
19
- console . log ( c . red ( "Neteisingi prisijungimo duomenys" ) ) ;
20
- process . exit ( ) ;
15
+ closeProgram ( c . red ( "Neteisingi prisijungimo duomenys" ) ) ;
21
16
}
22
- chooseAction ( ) ;
23
17
24
- process . on ( "exit" , ( ) => {
25
- rl . close ( ) ;
26
- console . clear ( ) ; //to clear password from history. Does it really clears?
27
- process . exit ( ) ;
28
- } ) ;
18
+ chooseAction ( ) ;
29
19
30
20
function validateLogin ( username , password ) {
31
21
if ( username . trim ( ) !== "admin" || password . trim ( ) !== "1234" ) {
32
22
return false ;
33
23
}
34
24
return true ;
35
25
}
36
- function validateChoice ( action ) {
37
- action = action . trim ( ) . toLowerCase ( ) ;
38
- if ( action === "r" || action === "read" ) return "r" ;
39
- if ( action === "w" || action === "write" ) return "w" ;
40
- return "" ;
41
- }
42
26
43
- async function runAction ( actionFunc , callback ) {
44
- await actionFunc ( ) ;
45
- await callback ( ) ;
46
- }
47
27
async function chooseAction ( ) {
28
+ /// do not quit after action, just keep cycling between prompt for action and action until ctrl+c
48
29
let action = "" ;
49
30
while ( ! validateChoice ( action ) ) {
50
31
console . log ( `Exit: ${ c . bgBlue ( "Ctrl + C" ) } ` ) ;
51
32
console . log ( `Read: ${ c . bgBlue ( "R" ) } ` ) ;
52
33
console . log ( `Write: ${ c . bgBlue ( "W" ) } ` ) ;
53
34
action = await rl . question ( `` ) ;
54
35
}
55
- action === "w" ? runAction ( writeDataToFile , chooseAction ) : runAction ( readFileData , chooseAction ) ;
36
+ action === "w" ? await writeDataToFile ( ) : await readFileData ( ) ;
37
+ await chooseAction ( ) ;
38
+ }
39
+ function validateChoice ( action ) {
40
+ action = action . trim ( ) . toLowerCase ( ) ;
41
+ if ( action === "r" || action === "read" ) return "r" ;
42
+ if ( action === "w" || action === "write" ) return "w" ;
43
+ return "" ;
56
44
}
57
45
58
46
async function writeDataToFile ( ) {
@@ -74,3 +62,11 @@ async function readFileData() {
74
62
console . log ( c . red ( err . message ) ) ;
75
63
}
76
64
}
65
+ function closeProgram ( msg = "" ) {
66
+ // console.clear(); //to clear password and read history from logs. doesn't clear all console, only visible area
67
+ // https://stackoverflow.com/questions/8813142/clear-terminal-window-in-node-js-readline-shell
68
+ process . stdout . write ( "\u001b[H\u001b[2J\u001b[3J" ) ; //clear terminal with the scrollback, ansi escape code sequence
69
+ rl . close ( ) ;
70
+ console . log ( msg ) ;
71
+ process . exit ( 0 ) ;
72
+ }
0 commit comments