2015-07-28 10:09:26 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2019-10-06 17:10:57 +02:00
|
|
|
"net/url"
|
2015-07-28 10:09:26 +02:00
|
|
|
"os"
|
2015-07-28 10:34:56 +02:00
|
|
|
"regexp"
|
2015-07-28 10:09:26 +02:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
BASE = "dict.cc/?s="
|
|
|
|
VAR1 = "var c1Arr = new Array("
|
|
|
|
VAR2 = "var c2Arr = new Array("
|
|
|
|
SUFF = ");"
|
|
|
|
)
|
|
|
|
|
2015-07-28 17:12:26 +02:00
|
|
|
// is going to be set in winsize.go, if run on unix
|
|
|
|
var Width = 80
|
|
|
|
|
2015-07-28 10:09:26 +02:00
|
|
|
func main() {
|
2015-07-28 10:45:44 +02:00
|
|
|
if len(os.Args) < 2 || os.Args[1][0] == '-' {
|
2015-07-28 10:09:26 +02:00
|
|
|
println("usage:", os.Args[0], "word [{de|es|fr|en...}{de|es|fr|en|...}]")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fromto := ""
|
|
|
|
if len(os.Args) > 2 {
|
|
|
|
fromto = os.Args[2] + "."
|
|
|
|
}
|
|
|
|
|
2024-06-06 14:18:33 +02:00
|
|
|
req, err := http.NewRequest("GET", "https://"+fromto+BASE+url.QueryEscape(os.Args[1]), nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
req.Header.Add("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:126.0) Gecko/20100101 Firefox/126.0")
|
|
|
|
r, err := http.DefaultClient.Do(req)
|
|
|
|
|
2015-07-28 10:09:26 +02:00
|
|
|
if err != nil {
|
|
|
|
println("error:", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
byline := bufio.NewScanner(r.Body)
|
|
|
|
|
|
|
|
var left, right []string
|
|
|
|
|
|
|
|
for byline.Scan() {
|
|
|
|
line := byline.Text()
|
|
|
|
if strings.HasPrefix(line, VAR1) {
|
|
|
|
line = strings.TrimPrefix(line, VAR1)
|
|
|
|
line = strings.TrimSuffix(line, SUFF)
|
2015-07-28 10:25:44 +02:00
|
|
|
left = strings.Split(line, `","`)
|
2015-07-28 10:09:26 +02:00
|
|
|
} else if strings.HasPrefix(line, VAR2) {
|
|
|
|
line = strings.TrimPrefix(line, VAR2)
|
|
|
|
line = strings.TrimSuffix(line, SUFF)
|
2015-07-28 10:25:44 +02:00
|
|
|
right = strings.Split(line, `","`)
|
2015-07-28 10:09:26 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(left) != len(right) {
|
|
|
|
println("error: arrays of different length")
|
2015-07-28 10:25:44 +02:00
|
|
|
fmt.Println(strings.Join(left, "\n"), len(left))
|
|
|
|
fmt.Println(strings.Join(right, "\n"), len(right))
|
2015-07-28 10:09:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-07-28 17:12:26 +02:00
|
|
|
ml := 25
|
2015-07-28 10:18:22 +02:00
|
|
|
|
|
|
|
for _, v := range right {
|
|
|
|
if len(v) > ml {
|
|
|
|
ml = len(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 17:12:26 +02:00
|
|
|
if ml > (Width / 2) {
|
|
|
|
ml = Width / 2
|
2015-07-28 10:18:22 +02:00
|
|
|
}
|
|
|
|
|
2015-07-28 10:41:44 +02:00
|
|
|
re := regexp.MustCompile(`\\(['"„])`)
|
2015-07-28 10:18:22 +02:00
|
|
|
for i, _ := range right {
|
2015-07-28 10:25:44 +02:00
|
|
|
a := strings.Trim(right[i], `„"`)
|
|
|
|
b := strings.Trim(left[i], `„"`)
|
2015-07-28 10:34:56 +02:00
|
|
|
a = re.ReplaceAllString(a, `$1`)
|
|
|
|
b = re.ReplaceAllString(b, `$1`)
|
2015-07-28 17:48:14 +02:00
|
|
|
|
2015-07-28 10:09:26 +02:00
|
|
|
if a == "" {
|
|
|
|
continue
|
|
|
|
}
|
2015-07-28 17:48:14 +02:00
|
|
|
|
|
|
|
for len(a) > ml {
|
2015-07-28 10:41:44 +02:00
|
|
|
if i := strings.LastIndex(a[:ml], " "); i > 0 {
|
|
|
|
fmt.Printf("%*s\n", ml, a[:i])
|
|
|
|
a = a[i:]
|
2015-07-28 17:48:14 +02:00
|
|
|
} else {
|
|
|
|
break
|
2015-07-28 10:41:44 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-28 17:48:14 +02:00
|
|
|
|
|
|
|
fmt.Printf("%*s → ", ml, a)
|
|
|
|
for len(b) > ml {
|
2015-07-28 17:12:26 +02:00
|
|
|
if i := strings.LastIndex(b[:ml], " "); i > 0 {
|
2015-07-28 17:48:14 +02:00
|
|
|
fmt.Println(b[:i])
|
|
|
|
fmt.Printf("%*s ", ml, "")
|
|
|
|
b = b[i:]
|
|
|
|
} else {
|
|
|
|
break
|
2015-07-28 17:12:26 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-28 17:48:14 +02:00
|
|
|
fmt.Println(b)
|
2015-07-28 10:09:26 +02:00
|
|
|
}
|
|
|
|
}
|