pkg.re/essentialkaos/ek@v12.36.0+incompatible/terminal/example_test.go (about)

     1  package terminal
     2  
     3  // ////////////////////////////////////////////////////////////////////////////////// //
     4  //                                                                                    //
     5  //                         Copyright (c) 2021 ESSENTIAL KAOS                          //
     6  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     7  //                                                                                    //
     8  // ////////////////////////////////////////////////////////////////////////////////// //
     9  
    10  import (
    11  	"fmt"
    12  )
    13  
    14  // ////////////////////////////////////////////////////////////////////////////////// //
    15  
    16  func ExampleReadUI() {
    17  	// User must enter name
    18  	input, err := ReadUI("Please enter user name", true)
    19  
    20  	if err != nil {
    21  		fmt.Printf("Error: %v", err)
    22  	}
    23  
    24  	fmt.Printf("User name: %s\v", input)
    25  
    26  	// You can read user input without providing any title
    27  	fmt.Println("Please enter user name")
    28  	input, err = ReadUI("", true)
    29  
    30  	if err != nil {
    31  		fmt.Printf("Error: %v", err)
    32  	}
    33  
    34  	fmt.Printf("User name: %s\v", input)
    35  }
    36  
    37  func ExampleReadPassword() {
    38  	Prompt = "› "
    39  	MaskSymbol = "•"
    40  	MaskSymbolColorTag = "{s}"
    41  
    42  	// User must enter password
    43  	input, err := ReadUI("Please enter password", true)
    44  
    45  	if err != nil {
    46  		fmt.Printf("Error: %v", err)
    47  	}
    48  
    49  	fmt.Printf("User password: %s\v", input)
    50  }
    51  
    52  func ExampleReadAnswer() {
    53  	// If the user doesn't enter any value, we will use the default
    54  	// value (Y in this case)
    55  	ok, err := ReadAnswer("Remove this file?", "Y")
    56  
    57  	if !ok || err != nil {
    58  		return
    59  	}
    60  
    61  	if ok {
    62  		fmt.Println("File removed")
    63  	}
    64  }
    65  
    66  func ExamplePrintActionMessage() {
    67  	statusOk := true
    68  
    69  	PrintActionMessage("Starting service my-service")
    70  
    71  	switch statusOk {
    72  	case true:
    73  		PrintActionStatus(0) // Print OK
    74  	case false:
    75  		PrintActionStatus(1) // Print ERROR
    76  	}
    77  }
    78  
    79  func ExamplePrintActionStatus() {
    80  	statusOk := true
    81  
    82  	PrintActionMessage("Starting service my-service")
    83  
    84  	switch statusOk {
    85  	case true:
    86  		PrintActionStatus(0) // Print OK
    87  	case false:
    88  		PrintActionStatus(1) // Print ERROR
    89  	}
    90  }
    91  
    92  func ExamplePrintErrorMessage() {
    93  	// Print red text to stderr
    94  	PrintErrorMessage("Error while sending data")
    95  }
    96  
    97  func ExamplePrintWarnMessage() {
    98  	// Print yellow text to stderr
    99  	PrintWarnMessage("Warning file is not found")
   100  }