-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.go
57 lines (42 loc) · 1 KB
/
prompt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"strings"
)
type Prompt struct {
tty *os.File
reader *bufio.Reader
}
func MakePrompt() *Prompt {
tty, err := os.Open("/dev/tty")
if err != nil {
panic(err)
}
return &Prompt{tty, bufio.NewReader(tty)}
}
func (p *Prompt) Speak(message string, voice string) {
FlushTTYin()
fmt.Print(strings.TrimSpace(message) + " ")
textToSpeech(message, voice)
p.reader.ReadLine()
}
func textToSpeech(message string, voice string) {
// Convert information in parens to comments so that they are ignored by the say command
message = strings.Replace(message, "(", "[[", -1)
message = strings.Replace(message, ")", "]]", -1)
cmd := exec.Command("/usr/bin/say", "-v", voice, message)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "\nError: There was a problem running the voice to text command: %s\n", stderr.String())
os.Exit(1)
}
cmd.Run()
}