Simplify trans API
This commit is contained in:
parent
4d035075ca
commit
a6f5cd6268
@ -9,28 +9,22 @@ import (
|
|||||||
|
|
||||||
|
|
||||||
type Translation struct {
|
type Translation struct {
|
||||||
job TransJob
|
|
||||||
Executor func(TransJob) ([]byte, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type TransJob struct {
|
|
||||||
word string
|
word string
|
||||||
fromLang string
|
fromLang string
|
||||||
toLang string
|
toLang string
|
||||||
|
Executor func(*Translation) ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(word, fromLang, toLang string) Translation {
|
func New(word, fromLang, toLang string) Translation {
|
||||||
return Translation{
|
return Translation{
|
||||||
job: TransJob{
|
word: word,
|
||||||
word: word,
|
fromLang: fromLang,
|
||||||
fromLang: fromLang,
|
toLang: toLang,
|
||||||
toLang: toLang,
|
|
||||||
},
|
|
||||||
Executor: executeTransShell,
|
Executor: executeTransShell,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func executeTransShell(tj TransJob) ([]byte, error) {
|
func executeTransShell(t *Translation) ([]byte, error) {
|
||||||
config := []string{
|
config := []string{
|
||||||
"-no-ansi",
|
"-no-ansi",
|
||||||
"-show-original", "n",
|
"-show-original", "n",
|
||||||
@ -38,14 +32,14 @@ func executeTransShell(tj TransJob) ([]byte, error) {
|
|||||||
"-show-dictionary", "y",
|
"-show-dictionary", "y",
|
||||||
"-show-languages", "n",
|
"-show-languages", "n",
|
||||||
"-show-prompt-message", "n",
|
"-show-prompt-message", "n",
|
||||||
fmt.Sprintf("%s:%s", tj.fromLang, tj.toLang),
|
fmt.Sprintf("%s:%s", t.fromLang, t.toLang),
|
||||||
tj.word,
|
t.word,
|
||||||
}
|
}
|
||||||
return exec.Command("trans", config...).Output()
|
return exec.Command("trans", config...).Output()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Translation) Translate() []string {
|
func (t Translation) Translate() []string {
|
||||||
outBytes, err := t.Executor(t.job)
|
outBytes, err := t.Executor(&t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -77,7 +77,7 @@ méltányosság
|
|||||||
|
|
||||||
for _, data := range cases {
|
for _, data := range cases {
|
||||||
j := trans.New(data.job[0], data.job[1], data.job[2])
|
j := trans.New(data.job[0], data.job[1], data.job[2])
|
||||||
j.Executor = func(tj trans.TransJob) ([]byte, error) {
|
j.Executor = func(t *trans.Translation) ([]byte, error) {
|
||||||
return data.transOutput, nil
|
return data.transOutput, nil
|
||||||
}
|
}
|
||||||
r := j.Translate()
|
r := j.Translate()
|
||||||
|
Loading…
Reference in New Issue
Block a user