github.com/aeternity/aepp-sdk-go/v6@v6.0.0/cmd/text_functions.go (about)

     1  package cmd
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  	"syscall"
     9  
    10  	"golang.org/x/crypto/ssh/terminal"
    11  )
    12  
    13  // IsEqStr tells if two strings a and b are equals after trimming spaces and lowercasing
    14  func IsEqStr(a, b string) bool {
    15  	return strings.ToLower(strings.TrimSpace(a)) == strings.ToLower(strings.TrimSpace(b))
    16  }
    17  
    18  // IsEmptyStr tells if a string is empty or not
    19  func IsEmptyStr(s string) bool {
    20  	return len(strings.TrimSpace(s)) == 0
    21  }
    22  
    23  // DefaultIfEmptyStr set a default for a string if it is nulled
    24  func DefaultIfEmptyStr(s *string, defaultS string) {
    25  	if IsEmptyStr(*s) {
    26  		*s = defaultS
    27  	}
    28  }
    29  
    30  // AskYes prompt a yes/no question to the prompt
    31  func AskYes(question string, defaultYes bool) (isYes bool) {
    32  	fmt.Print(question)
    33  	if defaultYes {
    34  		fmt.Print(" [yes]: ")
    35  	} else {
    36  		fmt.Print(" [no]: ")
    37  	}
    38  	reader := bufio.NewReader(os.Stdin)
    39  	reply, _ := reader.ReadString('\n')
    40  	DefaultIfEmptyStr(&reply, "yes")
    41  	if IsEqStr(reply, "yes") {
    42  		return true
    43  	}
    44  	return
    45  }
    46  
    47  // AskPassword ask a password
    48  func AskPassword(question string) (password string, err error) {
    49  	fmt.Println(question)
    50  	bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
    51  	if err != nil {
    52  		return
    53  	}
    54  	password = string(bytePassword)
    55  	return
    56  }