Skip to content

Commit

Permalink
Introduce jump pins, a command that lists all the pins
Browse files Browse the repository at this point in the history
  • Loading branch information
gsamokovarov committed Jul 12, 2018
1 parent dd82ccf commit 00652ba
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 8 deletions.
5 changes: 5 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var td string
type testConfig struct {
Entries scoring.Entries
Search config.Search
Pins map[string]string
Pin string
}

Expand All @@ -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 != ""
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 23 additions & 0 deletions cmd/pins.go
Original file line number Diff line number Diff line change
@@ -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)
}
22 changes: 22 additions & 0 deletions cmd/pins_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion cmd/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 23 additions & 6 deletions config/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}

Expand All @@ -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
}

Expand Down

0 comments on commit 00652ba

Please sign in to comment.