| 1 |
package screen |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"fmt" |
| 6 |
"os" |
| 7 |
"strconv" |
| 8 |
"strings" |
| 9 |
) |
| 10 |
|
| 11 |
// TermMessage sends a message to the user in the terminal. This usually occurs before |
| 12 |
// micro has been fully initialized -- ie if there is an error in the syntax highlighting |
| 13 |
// regular expressions |
| 14 |
// The function must be called when the Screen is not initialized |
| 15 |
// This will write the message, and wait for the user |
| 16 |
// to press and key to continue |
| 17 |
func TermMessage(msg ...any) { |
| 18 |
screenb := TempFini() |
| 19 |
|
| 20 |
fmt.Println(msg...) |
| 21 |
fmt.Print("\nPress enter to continue") |
| 22 |
|
| 23 |
reader := bufio.NewReader(os.Stdin) |
| 24 |
reader.ReadString('\n') |
| 25 |
|
| 26 |
TempStart(screenb) |
| 27 |
} |
| 28 |
|
| 29 |
// TermPrompt prints a prompt and requests the user for a response |
| 30 |
// The result is matched against a list of options and the index of |
| 31 |
// the match is returned |
| 32 |
// If wait is true, the prompt re-prompts until a valid option is |
| 33 |
// chosen, otherwise if wait is false, -1 is returned for no match |
| 34 |
func TermPrompt(prompt string, options []string, wait bool) int { |
| 35 |
screenb := TempFini() |
| 36 |
|
| 37 |
idx := -1 |
| 38 |
// same behavior as do { ... } while (wait && idx == -1) |
| 39 |
for ok := true; ok; ok = wait && idx == -1 { |
| 40 |
reader := bufio.NewReader(os.Stdin) |
| 41 |
fmt.Print(prompt) |
| 42 |
resp, _ := reader.ReadString('\n') |
| 43 |
resp = strings.TrimSpace(resp) |
| 44 |
|
| 45 |
for i, opt := range options { |
| 46 |
if resp == opt { |
| 47 |
idx = i |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
if wait && idx == -1 { |
| 52 |
fmt.Println("\nInvalid choice.") |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
TempStart(screenb) |
| 57 |
|
| 58 |
return idx |
| 59 |
} |
| 60 |
|
| 61 |
// TermError sends an error to the user in the terminal. Like TermMessage except formatted |
| 62 |
// as an error |
| 63 |
func TermError(filename string, lineNum int, err string) { |
| 64 |
TermMessage(filename + ", " + strconv.Itoa(lineNum) + ": " + err) |
| 65 |
} |