Skip to content
This repository was archived by the owner on May 3, 2024. It is now read-only.

Commit 9643808

Browse files
committed
fix history key bind
1 parent 13566b0 commit 9643808

File tree

4 files changed

+61
-14
lines changed

4 files changed

+61
-14
lines changed

komanda/command/help.go

+8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ func (e *HelpCmd) Exec(args []string) error {
2828
metadata := command.Metadata()
2929
client.StatusMessage(v, fmt.Sprintf("/%s %s - %s",
3030
metadata.Name(), metadata.Args(), metadata.Description()))
31+
32+
// client.StatusMessage(v,
33+
// fmt.Sprintf("\t Aliases: %s", strings.Join(metadata.Aliases(), ", ")),
34+
// )
35+
36+
// for _, a := range metadata.Aliases() {
37+
// client.StatusMessage(v, fmt.Sprintf("\t - /%s", a))
38+
// }
3139
}
3240

3341
client.StatusMessage(v, "==================== HELP COMMANDS ====================\n")

komanda/share/history/history.go

+41-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package history
22

3-
import "github.com/mephux/komanda-cli/komanda/logger"
3+
import (
4+
"sync"
5+
6+
"github.com/davecgh/go-spew/spew"
7+
"github.com/mephux/komanda-cli/komanda/logger"
8+
)
49

510
// History struct
611
type History struct {
712
Max int
813
Data []string
914
Index int
15+
16+
mu sync.Mutex
1017
}
1118

1219
// New history struct
@@ -20,11 +27,19 @@ func New() *History {
2027

2128
// Add line to history
2229
func (h *History) Add(line string) {
30+
h.mu.Lock()
31+
defer h.mu.Unlock()
32+
2333
logger.Logger.Print("in Add\n")
2434

25-
if len(h.Data) >= h.Max {
35+
prev := h.Prev()
36+
37+
if line == prev {
38+
return
39+
}
40+
41+
if len(h.Data) > h.Max {
2642
h.Data = append(h.Data[:0], h.Data[1:]...)
27-
h.Index = len(h.Data) - 1
2843
}
2944

3045
h.Data = append(h.Data, line)
@@ -33,6 +48,17 @@ func (h *History) Add(line string) {
3348
logger.Logger.Printf("ADD %s %d\n", h.Data, h.Index)
3449
}
3550

51+
// HasLine will check is the history buffer contains the given string
52+
func (h *History) HasLine(line string) bool {
53+
for _, l := range h.Data {
54+
if line == l {
55+
return true
56+
}
57+
}
58+
59+
return false
60+
}
61+
3662
// Get history at index
3763
func (h *History) Get(index int) string {
3864
return h.Data[index]
@@ -49,21 +75,27 @@ func (h *History) Empty() bool {
4975

5076
// Prev returns the previous history line fvrom the current index
5177
func (h *History) Prev() string {
52-
logger.Logger.Print("Prev\n")
78+
logger.Logger.Printf("Prev from %d %d\n", h.Index, len(h.Data)-1)
5379

5480
h.Index--
5581

5682
if h.Index < 0 {
57-
h.Index = len(h.Data) - 1
83+
h.Index = 0
5884
}
5985

86+
// if h.Index == -1 {
87+
// h.Index = 0
88+
// } else if h.Index < -1 {
89+
// h.Index = len(h.Data) - 1
90+
// }
91+
6092
logger.Logger.Printf("Set Prev Index %d\n", h.Index)
6193

6294
if h.Empty() {
6395
return ""
6496
}
6597

66-
logger.Logger.Printf("PREV %s\n", h.Data[h.Index])
98+
logger.Logger.Printf("PREV %s\n", spew.Sdump(h.Data))
6799

68100
return h.Data[h.Index]
69101
}
@@ -75,7 +107,7 @@ func (h *History) Next() string {
75107
h.Index++
76108

77109
if h.Index >= len(h.Data) {
78-
h.Index = 0
110+
h.Index = len(h.Data) - 1
79111
}
80112

81113
logger.Logger.Printf("Set Next Index %d\n", h.Index)
@@ -90,8 +122,6 @@ func (h *History) Next() string {
90122
}
91123

92124
// Current history line
93-
func (h *History) Current() string {
94-
95-
h.Index = len(h.Data) - 1
96-
return h.Data[h.Index]
125+
func (h *History) Current() {
126+
h.Index = len(h.Data)
97127
}

komanda/ui/handlers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func BindHandlers() {
7878
})
7979

8080
Server.Client.HandleFunc("NOTICE", func(conn *irc.Conn, line *irc.Line) {
81-
logger.Logger.Println("NOTICE -----------------------------", spew.Sdump(line))
81+
// logger.Logger.Println("NOTICE -----------------------------", spew.Sdump(line))
8282

8383
var channel = line.Nick
8484
var noticeChannel bool

komanda/util.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ func simpleEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
166166
c.Autoscroll = true
167167
}
168168
}
169+
170+
InputHistory.Current()
171+
169172
// v.EditNewLine()
170173
// v.Rewind()
171174

@@ -178,16 +181,22 @@ func simpleEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
178181

179182
if line := InputHistory.Next(); len(line) > 0 {
180183
v.Clear()
184+
v.SetCursor(0, 0)
185+
v.SetOrigin(0, 0)
186+
181187
fmt.Fprint(v, line)
182-
v.SetCursor(len(v.Buffer()), 0)
188+
v.SetCursor(len(v.Buffer())-1, 0)
183189
}
184190
case key == gocui.KeyArrowUp:
185191
inHistroy = true
186192

187193
if line := InputHistory.Prev(); len(line) > 0 {
188194
v.Clear()
195+
v.SetCursor(0, 0)
196+
v.SetOrigin(0, 0)
197+
189198
fmt.Fprint(v, line)
190-
v.SetCursor(len(v.Buffer()), 0)
199+
v.SetCursor(len(v.Buffer())-1, 0)
191200
}
192201
case key == gocui.KeyArrowLeft:
193202
v.MoveCursor(-1, 0, false)

0 commit comments

Comments
 (0)