Merge pull request 'file-input' (#2) from file-input into master

Reviewed-on: #2
master
Aaron Johnson 1 year ago
commit 9a25ad6a5d

@ -7,6 +7,8 @@ import (
"strings"
"time"
"flag"
"bufio"
"os"
"github.com/chzyer/readline"
"github.com/gopxl/beep"
@ -24,7 +26,7 @@ const (
func main() {
// Define and parse the -r flag
repeat := flag.Bool("r", false, "Repeat each section separated by space, '-', or '_'")
repeat := flag.Bool("r", false, "Repeat each section separated by space, '-', '_', or a new line")
flag.Parse()
// Create a map to store sound data for each character
@ -39,7 +41,32 @@ func main() {
}
}
// Configure readline settings
var input string
if *repeat {
fmt.Println("Repeat mode is ON")
}
// Check if input is being redirected from a file
if isInputFromPipe() {
// Read from stdin (file input)
scanner := bufio.NewScanner(os.Stdin)
var inputBuilder strings.Builder
for scanner.Scan() {
line := scanner.Text()
inputBuilder.WriteString(line + "\n")
}
if err := scanner.Err(); err != nil {
fmt.Printf("Error reading input: %s\n", err)
return
}
input = inputBuilder.String()
// Display the input, filtered to uppercase, before playback
fmt.Println("Input:\n")
fmt.Println(strings.ToUpper(input))
} else {
// Use readline for interactive input
rl, err := readline.NewEx(&readline.Config{
Prompt: "> ", // Use a simple prompt for each line input
InterruptPrompt: "^C",
@ -76,18 +103,18 @@ func main() {
inputBuilder.WriteString(line + "\n")
}
// Get the complete input as a string
input := inputBuilder.String()
input = strings.ToLower(strings.TrimSpace(input))
input = inputBuilder.String()
}
// Split the input into sections and process each section
// Process the input
input = strings.ToLower(strings.TrimSpace(input))
sections := strings.FieldsFunc(input, func(r rune) bool {
return r == ' ' || r == '-' || r == '_'
return r == ' ' || r == '-' || r == '_' || r == '\n' || r == '\r'
})
fmt.Println("Playback starting...")
fmt.Print("Beginning playback... ")
for _, section := range sections {
// Determine the number of times to read each section
numRepeats := 1
if *repeat {
numRepeats = 2
@ -115,6 +142,12 @@ func main() {
fmt.Println("Playback finished. Exiting.")
}
// Function to check if input is from a pipe or file
func isInputFromPipe() bool {
fileInfo, _ := os.Stdin.Stat()
return (fileInfo.Mode() & os.ModeCharDevice) == 0
}
// Function to play sound
func playSound(soundData []byte) {
streamer, format, err := wav.Decode(bytes.NewReader(soundData))

Loading…
Cancel
Save