an updated and hopefully faster version of the ST Toolbox
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

124 lines
2.8 KiB

package utils
import (
"bufio"
"encoding/csv"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
)
func PromptCredentials() (string, string, error) {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter your email: ")
email, _ := reader.ReadString('\n')
email = strings.TrimSpace(email)
fmt.Print("Enter your password: ")
password, _ := reader.ReadString('\n')
password = strings.TrimSpace(password)
return email, password, nil
}
func GetUserChoice(max int) (int, error) {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("\nEnter your choice (1-%d): ", max)
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
choice, err := strconv.Atoi(input)
if err != nil || choice < 1 || choice > max {
return 0, fmt.Errorf("invalid input")
}
return choice, nil
}
func PromptForInput(prompt string) string {
reader := bufio.NewReader(os.Stdin)
fmt.Print(prompt)
input, _ := reader.ReadString('\n')
return strings.TrimSpace(input)
}
func PressEnterToContinue() {
fmt.Println("Press Enter to continue...")
bufio.NewReader(os.Stdin).ReadBytes('\n')
}
// ReadCSV reads a CSV file and returns its contents as a slice of string slices
func ReadCSV(filename string) ([][]string, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
reader := csv.NewReader(file)
return reader.ReadAll()
}
// WriteCSV writes a slice of string slices to a CSV file
func WriteCSV(filename string, data [][]string) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
return writer.WriteAll(data)
}
// ChooseFile allows the user to select a file from the current directory
func ChooseFile(extension string) (string, error) {
currentDir, err := os.Getwd()
if err != nil {
return "", err
}
files, err := filepath.Glob(filepath.Join(currentDir, "*"+extension))
if err != nil {
return "", err
}
if len(files) == 0 {
return "", fmt.Errorf("no %s files found in the current directory", extension)
}
fmt.Printf("Available %s files:\n", extension)
for i, file := range files {
fmt.Printf("%d. %s\n", i+1, filepath.Base(file))
}
var choice int
fmt.Print("Enter the number of the file you want to select: ")
_, err = fmt.Scanf("%d", &choice)
if err != nil || choice < 1 || choice > len(files) {
return "", fmt.Errorf("invalid selection")
}
return files[choice-1], nil
}
// PromptYesNo prompts the user for a yes/no response
func PromptYesNo(prompt string) bool {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("%s (y/n): ", prompt)
response, _ := reader.ReadString('\n')
response = strings.ToLower(strings.TrimSpace(response))
if response == "y" || response == "yes" {
return true
} else if response == "n" || response == "no" {
return false
}
fmt.Println("Please answer with 'y' or 'n'.")
}
}