Просмотр исходного кода

Support deleting command history with store:del-cmd.

This addresses #554.

However, the command sequence number shown in histlist mode is not
correct for session-local entries and when there are deleted entries.
Qi Xiao 6 лет назад
Родитель
Сommit
89bc1e5fa6

+ 8 - 1
daemon/api.go

@@ -9,7 +9,7 @@ const (
 	ServiceName = "Daemon"
 
 	// Version is the API version. It should be bumped any time the API changes.
-	Version = -96
+	Version = -94
 )
 
 // Basic requests.
@@ -42,6 +42,13 @@ type AddCmdResponse struct {
 	Seq int
 }
 
+type DelCmdRequest struct {
+	Seq int
+}
+
+type DelCmdResponse struct {
+}
+
 type CmdRequest struct {
 	Seq int
 }

+ 7 - 0
daemon/client.go

@@ -123,6 +123,13 @@ func (c *Client) AddCmd(text string) (int, error) {
 	return res.Seq, err
 }
 
+func (c *Client) DelCmd(seq int) error {
+	req := &DelCmdRequest{seq}
+	res := &DelCmdResponse{}
+	err := c.call("DelCmd", req, res)
+	return err
+}
+
 func (c *Client) Cmd(seq int) (string, error) {
 	req := &CmdRequest{seq}
 	res := &CmdResponse{}

+ 8 - 0
daemon/service.go

@@ -132,6 +132,14 @@ func (s *Service) AddCmd(req *AddCmdRequest, res *AddCmdResponse) error {
 	return err
 }
 
+func (s *Service) DelCmd(req *DelCmdRequest, res *DelCmdResponse) error {
+	if s.err != nil {
+		return s.err
+	}
+	err := s.store.DelCmd(req.Seq)
+	return err
+}
+
 func (s *Service) Cmd(req *CmdRequest, res *CmdResponse) error {
 	if s.err != nil {
 		return s.err

+ 1 - 1
edit/history/histlist.go

@@ -64,7 +64,7 @@ func (hl *histlist) Len() int {
 }
 
 func (hl *histlist) Show(i int) (string, ui.Styled) {
-	return fmt.Sprintf("%d", hl.index[i]), ui.Unstyled(hl.shown[i])
+	return fmt.Sprintf("%d", hl.index[i]+1), ui.Unstyled(hl.shown[i])
 }
 
 func (hl *histlist) Filter(filter string) int {

+ 10 - 10
edit/history/histlist_test.go

@@ -12,22 +12,22 @@ var (
 
 	histlistDedupFilterTests = []eddefs.ListingProviderFilterTest{
 		{"", []eddefs.ListingShown{
-			{"1", ui.Unstyled("echo lalala")},
-			{"2", ui.Unstyled("ls")}}},
+			{"2", ui.Unstyled("echo lalala")},
+			{"3", ui.Unstyled("ls")}}},
 		{"l", []eddefs.ListingShown{
-			{"1", ui.Unstyled("echo lalala")},
-			{"2", ui.Unstyled("ls")}}},
+			{"2", ui.Unstyled("echo lalala")},
+			{"3", ui.Unstyled("ls")}}},
 	}
 
 	histlistNoDedupFilterTests = []eddefs.ListingProviderFilterTest{
 		{"", []eddefs.ListingShown{
-			{"0", ui.Unstyled("ls")},
-			{"1", ui.Unstyled("echo lalala")},
-			{"2", ui.Unstyled("ls")}}},
+			{"1", ui.Unstyled("ls")},
+			{"2", ui.Unstyled("echo lalala")},
+			{"3", ui.Unstyled("ls")}}},
 		{"l", []eddefs.ListingShown{
-			{"0", ui.Unstyled("ls")},
-			{"1", ui.Unstyled("echo lalala")},
-			{"2", ui.Unstyled("ls")}}},
+			{"1", ui.Unstyled("ls")},
+			{"2", ui.Unstyled("echo lalala")},
+			{"3", ui.Unstyled("ls")}}},
 	}
 )
 

+ 1 - 0
eval/store/store.go

@@ -8,5 +8,6 @@ import (
 func Ns(s storedefs.Store) eval.Ns {
 	return eval.NewNs().AddBuiltinFns("store:", map[string]interface{}{
 		"del-dir": s.DelDir,
+		"del-cmd": s.DelCmd,
 	})
 }

+ 1 - 0
store/storedefs/interface.go

@@ -4,6 +4,7 @@ package storedefs
 type Store interface {
 	NextCmdSeq() (int, error)
 	AddCmd(text string) (int, error)
+	DelCmd(seq int) error
 	Cmd(seq int) (string, error)
 	Cmds(from, upto int) ([]string, error)
 	NextCmd(from int, prefix string) (int, string, error)