github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/usage/query.go (about)

     1  package usage
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/colors"
    10  )
    11  
    12  func QueryUser(prompt, noResponse string) bool {
    13  	if os.Getenv("TB_NO_USERQUERY") == "true" {
    14  		return true
    15  	} else if len(os.Getenv("TB_NO_USERQUERY")) > 0 {
    16  		return false
    17  	}
    18  
    19  	reader := bufio.NewReader(os.Stdin)
    20  	fmt.Fprintf(os.Stderr, colors.Yellow+"%s"+colors.Off, prompt)
    21  	text, _ := reader.ReadString('\n')
    22  	text = strings.Replace(text, "\n", "", -1)
    23  	if text != "" && text != "y" && text != "Y" {
    24  		text = strings.ToLower(text)
    25  		if text == "q" || text == "quit" {
    26  			fmt.Fprintf(os.Stderr, "Quitting...\n")
    27  			os.Exit(0)
    28  		}
    29  		fmt.Fprintf(os.Stderr, "%s [%s]\n", noResponse, text)
    30  		return false
    31  	}
    32  	return true
    33  }
    34  
    35  func Wait() {
    36  	QueryUser("Press Enter to continue", "Continuing...")
    37  }