This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
81 lines (68 loc) · 1.54 KB
/
main.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// CUI client for Frea Search
package main
import (
"errors"
"flag"
"fmt"
"os"
"github.com/ktr0731/go-fuzzyfinder"
"github.com/mattn/go-runewidth"
)
type exitCode int
const (
exitCodeOK exitCode = iota
exitCodeErrArgs
exitCodeErrRequest
exitCodeErrFuzzyFinder
)
var (
isURL bool
word string
)
func init() {
flag.BoolVar(&isURL, "u", false, "ブラウザを開かずURLを表示するか指定します。")
flag.StringVar(&word, "w", "", "検索するキーワードを指定します。")
}
func main() {
flag.Parse()
if word == "" {
fmt.Println("検索キーワードを指定して下さい。\n詳しくはfreasearcher --helpを実行してください。")
os.Exit(int(exitCodeErrArgs))
}
param := param{
Language: "ja",
SafeSearch: 0,
Query: word,
}
ctns, err := getResp(param)
if err != nil {
fmt.Fprintf(os.Stderr, "検索結果の取得に失敗しました: %s\n", err)
os.Exit(int(exitCodeErrRequest))
}
idx, err := fuzzyfinder.FindMulti(
ctns,
func(i int) string {
return ctns[i].title
},
fuzzyfinder.WithPreviewWindow(func(i, w, h int) string {
if i == -1 {
return ""
}
return runewidth.Wrap(ctns[i].content, setWidth())
}))
if err != nil {
if errors.Is(fuzzyfinder.ErrAbort, err) {
// 正常終了
os.Exit(int(exitCodeOK))
}
fmt.Fprintf(os.Stderr, "fuzzyfinderにてエラーが発生しました: %s\n", err)
os.Exit(int(exitCodeErrFuzzyFinder))
}
url := ctns[idx[0]].url
if isURL {
fmt.Println(url)
} else {
openBrowser(url)
}
os.Exit(int(exitCodeOK))
}