diff --git a/cmd/cmd_test.go b/cmd/cmd_test.go index 1ad19f9..ae87d1f 100644 --- a/cmd/cmd_test.go +++ b/cmd/cmd_test.go @@ -14,6 +14,7 @@ var td string type testConfig struct { Entries scoring.Entries Search config.Search + Pins map[string]string Pin string } @@ -36,6 +37,10 @@ func (c *testConfig) WriteSearch(term string, index int) error { return nil } +func (c *testConfig) ReadPins() (map[string]string, error) { + return c.Pins, nil +} + func (c *testConfig) FindPin(term string) (string, bool) { return c.Pin, c.Pin != "" } diff --git a/cmd/help_test.go b/cmd/help_test.go index e262cf6..0027c60 100644 --- a/cmd/help_test.go +++ b/cmd/help_test.go @@ -19,8 +19,9 @@ func Example_helpCmd() { // forget Removes the current directory from the database. // hint Hints relevant paths for jumping. // pin Pin a directory to a search term. + // pins Lists all the pinned search terms. // shell Display a shell integration script. - // top List the directories as they are scored. + // top Lists the directories as they are scored. // unpin Unpin a search term. // // Options: diff --git a/cmd/pins.go b/cmd/pins.go new file mode 100644 index 0000000..722fc02 --- /dev/null +++ b/cmd/pins.go @@ -0,0 +1,23 @@ +package cmd + +import ( + "github.com/gsamokovarov/jump/cli" + "github.com/gsamokovarov/jump/config" +) + +func pinsCmd(args cli.Args, conf config.Config) error { + pins, err := conf.ReadPins() + if err != nil { + return err + } + + for term, dir := range pins { + cli.Outf("%s\t%s\n", term, dir) + } + + return nil +} + +func init() { + cli.RegisterCommand("pins", "Lists all the pinned search terms.", pinsCmd) +} diff --git a/cmd/pins_test.go b/cmd/pins_test.go new file mode 100644 index 0000000..489b128 --- /dev/null +++ b/cmd/pins_test.go @@ -0,0 +1,22 @@ +package cmd + +import ( + "os" + "testing" + + "github.com/gsamokovarov/assert" +) + +func Test_pinsCmd(t *testing.T) { + conf := &testConfig{ + Pins: map[string]string{ + "r": "/home/user/projects/rails", + }, + } + + output := capture(&os.Stdout, func() { + assert.Nil(t, pinsCmd(nil, conf)) + }) + + assert.Equal(t, "r\t/home/user/projects/rails\n", output) +} diff --git a/cmd/top.go b/cmd/top.go index bc36386..4836589 100644 --- a/cmd/top.go +++ b/cmd/top.go @@ -24,5 +24,5 @@ func topCmd(_ cli.Args, conf config.Config) error { } func init() { - cli.RegisterCommand("top", "List the directories as they are scored.", topCmd) + cli.RegisterCommand("top", "Lists the directories as they are scored.", topCmd) } diff --git a/config/config.go b/config/config.go index 4c897a1..cc7c5d5 100644 --- a/config/config.go +++ b/config/config.go @@ -24,6 +24,7 @@ type Config interface { ReadSearch() Search WriteSearch(string, int) error + ReadPins() (map[string]string, error) FindPin(string) (string, bool) WritePin(string, string) error RemovePin(string) error diff --git a/config/pin.go b/config/pin.go index b8f78df..15b2f31 100644 --- a/config/pin.go +++ b/config/pin.go @@ -5,7 +5,22 @@ import ( "github.com/gsamokovarov/jump/config/jsonio" ) -// FindPin tries to the directory from a pinned search term. +// ReadPins tries to the directory from a pinned search term. +// +// If no search pinned search term is found. +func (c *fileConfig) ReadPins() (map[string]string, error) { + file, err := atom.Open(c.Pins) + if err != nil { + return nil, err + } + defer file.Close() + + pins := map[string]string{} + + return pins, jsonio.Decode(file, &pins) +} + +// FindPin tries to find a directory for a pinned search term. // // If no search pinned search term is found. func (c *fileConfig) FindPin(term string) (dir string, found bool) { @@ -15,11 +30,13 @@ func (c *fileConfig) FindPin(term string) (dir string, found bool) { } defer file.Close() - pins := map[string]string{} - if err := jsonio.Decode(file, &pins); err == nil { - dir, found = pins[term] + pins, err := c.ReadPins() + if err != nil { + return } + dir, found = pins[term] + return } @@ -31,8 +48,8 @@ func (c *fileConfig) WritePin(pin, value string) error { } defer file.Close() - pins := map[string]string{} - if err := jsonio.Decode(file, &pins); err != nil { + pins, err := c.ReadPins() + if err != nil { return err }