reading line by line

This commit is contained in:
potassium 2023-10-18 11:20:47 +03:00
parent fd6c5d76c4
commit e35b0158d7

39
main.go
View File

@ -2,6 +2,7 @@
package main package main
import ( import (
"bufio"
"io" "io"
"os" "os"
@ -14,28 +15,42 @@ import (
func main() { func main() {
english := flag.Bool("en", false, "translate from english") english := flag.Bool("en", false, "translate from english")
var source, into string = "ru", "en" var source, into string = "ru", "en"
var text interface{} var text string
var err error var err error
flag.Parse() flag.Parse()
if *english {
source, into = into, source
}
Args := flag.Args() Args := flag.Args()
if len(Args) > 0 { if len(Args) > 0 {
text = strings.Join(Args[:], " ") text = strings.Join(Args[:], " ")
} else { } else {
text, err = io.ReadAll(os.Stdin) r := bufio.NewReader(os.Stdin)
if err != nil { for {
panic(err) text, err = r.ReadString('\n')
if err != nil {
switch err {
case io.EOF:
return
default:
panic(err)
}
}
if len(text) == 0 {
os.Exit(1)
}
result, err := gt.Translate(text, source, into)
if err != nil {
panic(err)
}
fmt.Println(result)
} }
if len(text.([]byte)) == 0 {
os.Exit(1)
}
text = string(text.([]byte)) // looks awful but it works
} }
if *english { result, err := gt.Translate(text, source, into)
source, into = into, source
}
result, err := gt.Translate(text.(string), source, into)
if err != nil { if err != nil {
panic(err) panic(err)
} }