Skip to content

Commit

Permalink
feat: add input_hidden function (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
karpfediem authored Oct 1, 2024
1 parent b6df12a commit 1ab2bd9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/std/env.ab
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,16 @@ pub fun is_command(command: Text): Bool {

/// Creates a prompt and returns the value.
pub fun input(prompt: Text): Text {
unsafe $printf "\${nameof prompt}"$
unsafe $read$
unsafe $read -p "\${nameof prompt}"$
return "\$REPLY"
}

/// Creates a prompt, hides any user input and returns the value.
pub fun input_hidden(prompt: Text): Text {
unsafe {
$read -s -p "\${nameof prompt}"$
$echo "" >&2$
}
return "\$REPLY"
}

Expand Down
2 changes: 1 addition & 1 deletion src/tests/stdlib/input.ab
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * from "std/env"

// Output
// Please enter your name:Hello, Amber
// Hello, Amber

main {
unsafe $echo "Amber" >> /tmp/test_input$
Expand Down
12 changes: 12 additions & 0 deletions src/tests/stdlib/input_hidden.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * from "std/env"

// Output
// Hello, Amber

main {
unsafe $echo "Amber" >> /tmp/test_input$
unsafe $exec 0< /tmp/test_input$
let name = input_hidden("Please enter your name:")
echo "Hello, " + name
unsafe $rm /tmp/test_input$
}

0 comments on commit 1ab2bd9

Please sign in to comment.