Skip to content

Commit ad117d2

Browse files
committed
add: redeploy event;
1 parent 0d2ed64 commit ad117d2

File tree

6 files changed

+76
-22
lines changed

6 files changed

+76
-22
lines changed

core/core.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func Start(opts *Options) {
4545
listManager.SetFiles(fileNames)
4646

4747
// 添加菜单
48-
menuNameAdd := tui.Menu{Name: "Add", Cb: func(m *tui.Model) (cmd tea.Cmd) {
48+
menuNameAdd := tui.Menu{Name: "A添加", Cb: func(m *tui.Model) (cmd tea.Cmd) {
4949
if len(m.Inputs) == 0 {
5050
return tui.ExitMenuCmd
5151
}
@@ -84,7 +84,7 @@ func Start(opts *Options) {
8484
}}
8585

8686
// 删除菜单
87-
menuNameDelete := tui.Menu{Name: "Delete", Cb: func(m *tui.Model) (cmd tea.Cmd) {
87+
menuNameDelete := tui.Menu{Name: "D删除", Cb: func(m *tui.Model) (cmd tea.Cmd) {
8888
item, err := m.CurrItem()
8989
if err != nil {
9090
m.Inputs = []string{}
@@ -103,7 +103,7 @@ func Start(opts *Options) {
103103

104104
// 修改菜单
105105
var modifyingItem tui.ItemRender
106-
menuNameModify := tui.Menu{Name: "Modify", Cb: func(m *tui.Model) (cmd tea.Cmd) {
106+
menuNameModify := tui.Menu{Name: "M修改", Cb: func(m *tui.Model) (cmd tea.Cmd) {
107107
item, err := m.CurrItem()
108108
if err != nil {
109109
m.Inputs = []string{}
@@ -119,7 +119,7 @@ func Start(opts *Options) {
119119
}}
120120

121121
// 确认修改菜单
122-
menuNameConfirm := tui.Menu{Name: "Confirm", Cb: func(m *tui.Model) tea.Cmd {
122+
menuNameConfirm := tui.Menu{Name: "确认", Cb: func(m *tui.Model) tea.Cmd {
123123
m.Modifying = false
124124
raw := strings.Join(m.Inputs, "")
125125
switch item := modifyingItem.(type) {
@@ -212,11 +212,11 @@ func Start(opts *Options) {
212212
}
213213
}
214214
if key == "ctrl+up" && next != nil {
215-
currEntryData.Weight = int(math.Max(1, float64(next.Data().Weight+1)))
215+
currEntryData.Weight = int(math.Max(1, float64(next.Data().Weight-1)))
216216
changed = true
217217
}
218218
if key == "ctrl+down" && prev != nil {
219-
currEntryData.Weight = int(math.Max(1, float64(prev.Data().Weight-1)))
219+
currEntryData.Weight = int(math.Max(1, float64(prev.Data().Weight+1)))
220220
changed = true
221221
}
222222
}
@@ -240,6 +240,7 @@ func Start(opts *Options) {
240240
}
241241
}
242242
// 延迟同步到文件
243+
// log.Println("modify weight sync: ", currEntry.Raw())
243244
modifyWeightDebouncer.Do(func() {
244245
FlushAndSync(opts, dc, opts.SyncOnChange)
245246
})
@@ -256,6 +257,14 @@ func Start(opts *Options) {
256257
return m, func() tea.Msg { return 0 } // trigger bubbletea update
257258
},
258259
}
260+
// 重新部署,强制保存变更到文件,并执行rime部署指令。
261+
redeployEvent := &tui.Event{
262+
Keys: []string{"ctrl+s"},
263+
Cb: func(_ string, m *tui.Model) (tea.Model, tea.Cmd) {
264+
FlushAndSync(opts, dc, true)
265+
return m, nil
266+
},
267+
}
259268

260269
// new model
261270
events := []*tui.Event{
@@ -264,6 +273,7 @@ func Start(opts *Options) {
264273
tui.ClearInputEvent,
265274
exitEvent,
266275
exportDictEvent,
276+
redeployEvent,
267277
modifyWeightEvent,
268278
showHelpEvent,
269279
}
@@ -336,7 +346,7 @@ var lock *mutil.FLock = mutil.NewFLock()
336346
// 同步变更到文件中,如果启用了自动部署Rime的功能则调用部署指令
337347
func FlushAndSync(opts *Options, dc *dict.Dictionary, sync bool) {
338348
// 此操作的阻塞的,但可能被异步调用,因此加上防止重复调用机制
339-
if lock.Should() {
349+
if !lock.Should() {
340350
return
341351
}
342352
defer lock.Done()

dict/loader.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ type FileEntries struct {
2323
ID uint8
2424
}
2525

26+
func (fe *FileEntries) Id() int {
27+
return int(fe.ID)
28+
}
29+
2630
func (fe *FileEntries) String() string {
2731
return fe.FilePath
2832
}

dict/matcher.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ type MatchResult struct {
1111
score int
1212
}
1313

14+
func (m *MatchResult) Id() int {
15+
return int(m.Entry.FID)
16+
}
17+
1418
func (m *MatchResult) String() string {
1519
return string(m.Entry.raw)
1620
}

main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import (
44
"log"
55
"path/filepath"
66

7-
core "github.com/MapoMagpie/rimedm/core"
8-
tea "github.com/charmbracelet/bubbletea"
9-
7+
"github.com/MapoMagpie/rimedm/core"
8+
"github.com/charmbracelet/bubbletea"
109
// "net/http"
1110
// _ "net/http/pprof"
1211
)

tui/event.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var MoveEvent = &Event{
3434
Cb: func(key string, m *Model) (tea.Model, tea.Cmd) {
3535
list := m.lm.List()
3636
currIndex := &m.lm.currIndex
37-
if m.ShowMenu && m.CurrMenu().Name == "Add" {
37+
if m.ShowMenu && m.CurrMenu().Name == "A添加" {
3838
list = m.lm.files
3939
currIndex = &m.lm.fileIndex
4040
}

tui/tui.go

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import (
44
"errors"
55
"fmt"
66
"os"
7+
"path/filepath"
8+
"slices"
79
"sort"
10+
"strconv"
811
"strings"
912

1013
tea "github.com/charmbracelet/bubbletea"
@@ -24,12 +27,17 @@ func FreshListCmd() tea.Msg {
2427
}
2528

2629
type ItemRender interface {
30+
Id() int
2731
String() string
2832
Cmp(other any) bool
2933
}
3034

3135
type StringRender string
3236

37+
func (h StringRender) Id() int {
38+
return 0
39+
}
40+
3341
func (h StringRender) String() string {
3442
return string(h)
3543
}
@@ -82,15 +90,21 @@ func (l *ListManager) Files() []ItemRender {
8290

8391
func (l *ListManager) Helps() []ItemRender {
8492
list := []ItemRender{
85-
StringRender("菜单项: [Modify] 修改选择的项(高亮),回车后,输入框中的内容会被设置,修改后,再次回车确认修改"),
86-
StringRender("菜单项: [Delete] 将选择的项(高亮)从码表中删除,通过上下键选择"),
87-
StringRender("菜单项: [Add] 将输入的内容添加到码表中,上下方向键要添加到的文件"),
88-
StringRender("Enter: 显示菜单"),
93+
StringRender("Ctrl+S: 手动同步,如果没有启用自动同步,"),
94+
StringRender(" 可通过此按键手动将变更同步至文件,并部署Rime"),
8995
StringRender("Ctrl+Right: 修改权重,将当前项的权重加一"),
9096
StringRender("Ctrl+Left: 修改权重,将当前项的权重减一"),
9197
StringRender("Ctrl+Down: 修改权重,将当前项的权重增加到下一项之前"),
9298
StringRender("Ctrl+Up: 修改权重,将当前项的权重降低到上一项之后"),
99+
StringRender("Enter: 显示菜单"),
100+
StringRender("菜单项: [A添加] 将输入的内容添加到码表中,"),
101+
StringRender(" 上下方向键选择要添加到的文件"),
102+
StringRender("菜单项: [M修改] 修改选择的项(高亮),"),
103+
StringRender(" 回车后,输入框中的内容会被设置,"),
104+
StringRender(" 修改后,再次回车确认修改"),
105+
StringRender("菜单项: [D删除] 将选择的项(高亮)从码表中删除,通过上下键选择"),
93106
}
107+
slices.Reverse(list)
94108
return list
95109
}
96110

@@ -166,6 +180,19 @@ func (m *Model) CurrFile() (ItemRender, error) {
166180
return files[m.lm.fileIndex], nil
167181
}
168182

183+
func (m *Model) CurrItemFile() string {
184+
currItem, err := m.CurrItem()
185+
if err != nil {
186+
return ""
187+
}
188+
for _, file := range m.lm.Files() {
189+
if file.Id() == currItem.Id() {
190+
return filepath.Base(file.String())
191+
}
192+
}
193+
return ""
194+
}
195+
169196
func (m *Model) CurrMenu() *Menu {
170197
menus := m.menuFetcher(m.Modifying)
171198
if m.MenuIndex < len(menus) {
@@ -229,10 +256,20 @@ func (m *Model) menuCtl(key string) {
229256
}
230257
default:
231258
if len(key) == 1 && len(menus) > 0 {
232-
for i, menu := range menus {
233-
if strings.ToLower(menu.Name[:1]) == key {
234-
m.MenuIndex = i
235-
break
259+
num, err := strconv.Atoi(key)
260+
// select menu by numpad
261+
if err == nil && num > 0 && num < 10 {
262+
index := num - 1
263+
if index < len(menus) {
264+
m.MenuIndex = index
265+
}
266+
} else {
267+
// select menu by letter
268+
for i, menu := range menus {
269+
if strings.ToLower(menu.Name[:1]) == key {
270+
m.MenuIndex = i
271+
break
272+
}
236273
}
237274
}
238275
}
@@ -256,7 +293,7 @@ func (m *Model) View() string {
256293
if m.lm.ShowingHelp {
257294
list = m.lm.Helps()
258295
currIndex = 0
259-
} else if m.ShowMenu && m.CurrMenu().Name == "Add" {
296+
} else if m.ShowMenu && m.CurrMenu().Name == "A添加" {
260297
list = m.lm.Files()
261298
currIndex = m.lm.fileIndex
262299
}
@@ -282,8 +319,8 @@ func (m *Model) View() string {
282319
}
283320
}
284321
// footer
285-
sb.WriteString(fmt.Sprintf("Total: %d\n", le))
286-
sb.WriteString("Press[Enter:Menu][Ctrl+X:Clear Input][Ctrl+C|ESC:Quit][Ctrl+H:Show Help]\n")
322+
sb.WriteString(fmt.Sprintf("Total: %d; %s\n", le, m.CurrItemFile()))
323+
sb.WriteString("Press[Enter:操作][Ctrl+X:清空输入][Ctrl+S:同步][ESC:退出][Ctrl+H:帮助]\n")
287324
if m.Modifying {
288325
line = strings.Replace(line, "---------", "Modifying", 1)
289326
}

0 commit comments

Comments
 (0)