From d5ddbf521c6e252023b5a8d8a1a89ccb359e1792 Mon Sep 17 00:00:00 2001 From: Aaron Johnon Date: Wed, 28 Aug 2024 18:35:19 -0500 Subject: [PATCH 1/3] Added file input ability --- numberstation.go | 93 ++++++++++++++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 34 deletions(-) diff --git a/numberstation.go b/numberstation.go index 4111c44..ae0145f 100644 --- a/numberstation.go +++ b/numberstation.go @@ -7,6 +7,8 @@ import ( "strings" "time" "flag" + "bufio" + "os" "github.com/chzyer/readline" "github.com/gopxl/beep" @@ -39,55 +41,72 @@ func main() { } } - // Configure readline settings - rl, err := readline.NewEx(&readline.Config{ - Prompt: "> ", // Use a simple prompt for each line input - InterruptPrompt: "^C", - EOFPrompt: "exit", - FuncFilterInputRune: uppercaseFilter, // Filter to show uppercase - }) - if err != nil { - fmt.Printf("Error initializing readline: %s\n", err) - return - } - defer rl.Close() + var input string - // Inform the user about how to enter input - fmt.Println("Enter text to be read (A-Z and 0-9 only). Press Enter on a blank line to process:") - - var inputBuilder strings.Builder - for { - line, err := rl.Readline() - if err != nil { - if err.Error() == "EOF" { - fmt.Println("Exiting.") - return - } + // 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() + } else { + // Use readline for interactive input + rl, err := readline.NewEx(&readline.Config{ + Prompt: "> ", // Use a simple prompt for each line input + InterruptPrompt: "^C", + EOFPrompt: "exit", + FuncFilterInputRune: uppercaseFilter, // Filter to show uppercase + }) + if err != nil { + fmt.Printf("Error initializing readline: %s\n", err) + return + } + defer rl.Close() - // Check if the user entered a blank line to finish input - if strings.TrimSpace(line) == "" { - break + // Inform the user about how to enter input + fmt.Println("Enter text to be read (A-Z and 0-9 only). Press Enter on a blank line to process:") + + var inputBuilder strings.Builder + for { + line, err := rl.Readline() + if err != nil { + if err.Error() == "EOF" { + fmt.Println("Exiting.") + return + } + fmt.Printf("Error reading input: %s\n", err) + return + } + + // Check if the user entered a blank line to finish input + if strings.TrimSpace(line) == "" { + break + } + + // Append the line to the input builder + inputBuilder.WriteString(line + "\n") } - // Append the line to the input builder - inputBuilder.WriteString(line + "\n") + input = inputBuilder.String() } - // Get the complete input as a string - input := inputBuilder.String() + // Process the input input = strings.ToLower(strings.TrimSpace(input)) - - // Split the input into sections and process each section sections := strings.FieldsFunc(input, func(r rune) bool { return r == ' ' || r == '-' || r == '_' }) - fmt.Println("Playback starting...") + fmt.Println("Beginning playback...") + for _, section := range sections { - // Determine the number of times to read each section numRepeats := 1 if *repeat { numRepeats = 2 @@ -115,6 +134,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)) From 57cbc993792a0d3d216c67f5bbd2d066f53015e4 Mon Sep 17 00:00:00 2001 From: Aaron Johnon Date: Wed, 28 Aug 2024 18:48:33 -0500 Subject: [PATCH 2/3] Fixed new line parsing error --- numberstation.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/numberstation.go b/numberstation.go index ae0145f..fe0f9c8 100644 --- a/numberstation.go +++ b/numberstation.go @@ -26,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 @@ -101,7 +101,7 @@ func main() { // 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("Beginning playback...") From c7ba043dec74b64ed899d9d866a6e3a44df9e7a2 Mon Sep 17 00:00:00 2001 From: Aaron Johnon Date: Wed, 28 Aug 2024 19:06:35 -0500 Subject: [PATCH 3/3] Added functionalities to show input from pipe and if repeat mode is on --- numberstation.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/numberstation.go b/numberstation.go index fe0f9c8..58e812f 100644 --- a/numberstation.go +++ b/numberstation.go @@ -43,6 +43,10 @@ func main() { 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) @@ -57,6 +61,10 @@ func main() { 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{ @@ -104,7 +112,7 @@ func main() { return r == ' ' || r == '-' || r == '_' || r == '\n' || r == '\r' }) - fmt.Println("Beginning playback...") + fmt.Print("Beginning playback... ") for _, section := range sections { numRepeats := 1