/
util.go
115 lines (92 loc) · 2.42 KB
/
util.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package app
import (
"encoding/csv"
"errors"
"fmt"
"math"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"github.com/arrow2nd/nekomata/app/layout"
"github.com/mattn/go-runewidth"
)
// openExternalEditor : 外部エディタを開く
func (a *App) openExternalEditor(editor string, args ...string) error {
if editor == "" {
return errors.New("please specify which editor to use")
}
cmd := exec.Command(editor, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
var err error
if global.isCLI {
err = cmd.Run()
} else {
global.app.Suspend(func() {
err = cmd.Run()
})
}
if err != nil {
return fmt.Errorf("failed to open editor (%s) : %w", editor, err)
}
return nil
}
// getStringDisplayRow : 文字列の表示行数を取得
func getStringDisplayRow(s string, w int) int {
row := 0
// タグを削除
tagDeleted := replaceAll(s, `\[(.+?:.+?:.+?|".*?")\]`, "")
for _, s := range strings.Split(tagDeleted, "\n") {
r := int(math.Ceil(float64(runewidth.StringWidth(s)) / float64(w)))
row += r
}
return row
}
// getHighlightId : ハイライト一覧からIDを取得(見つからない場合 -1 が返る)
func getHighlightId(ids []string) int {
if ids == nil {
return -1
}
i := strings.Index(ids[0], "_")
if i == -1 || i+1 >= len(ids[0]) {
return -1
}
id, err := strconv.Atoi(ids[0][i+1:])
if err != nil {
return -1
}
return id
}
// find : スライス内から任意の条件を満たす値を探す
func find[T any](s []T, f func(T) bool) (int, bool) {
for i, v := range s {
if f(v) {
return i, true
}
}
return -1, false
}
// truncate : 文字列を指定幅で丸める
func truncate(s string, w int) string {
return runewidth.Truncate(s, w, "…")
}
// split : 文字列をスペースで分割(ダブルクオートで囲まれた部分は残す)
func split(s string) ([]string, error) {
r := csv.NewReader(strings.NewReader(s))
r.Comma = ' '
return r.Read()
}
// replaceAll : 正規表現にマッチした文字列を一斉置換
func replaceAll(str, reg, rep string) string {
replace := regexp.MustCompile(reg)
return replace.ReplaceAllString(str, rep)
}
// createStatusMessage : ラベル付きステータスメッセージを作成
func createStatusMessage(label, status string) string {
width, _ := layout.GetWindowSize()
status = strings.ReplaceAll(status, "\n", " ")
return truncate(fmt.Sprintf("[%s] %s", label, status), width)
}