Skip to content

Commit

Permalink
Always hint a single entries
Browse files Browse the repository at this point in the history
Trying to hint more entries makes it convoluted for me to implement and
it's not always clear when you'll get more of a single entry. So, chop
it off!
  • Loading branch information
gsamokovarov committed Apr 24, 2018
1 parent cee85ea commit f26678f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 84 deletions.
30 changes: 3 additions & 27 deletions cmd/hint.go
Expand Up @@ -17,38 +17,14 @@ func hintCmd(args cli.Args, conf config.Config) error {
}

fuzzyEntries := scoring.NewFuzzyEntries(entries, term)
hints := hintSelect(fuzzyEntries.Entries, term)

for _, entry := range hints {
cli.Outf("%s\n", entry.Path)
for _, hint := range fuzzyEntries.Entries {
cli.Outf("%s\n", hint.Path)
break
}

return nil
}

func hintSelect(entries scoring.Entries, term string) scoring.Entries {
termLength := len(term)

switch {
case termLength == 0:
return hintSliceEntries(entries, 5)
case termLength < 7:
return hintSliceEntries(entries, 1)
case termLength >= 7 && termLength < 10:
return hintSliceEntries(entries, 3)
default:
return hintSliceEntries(entries, 1)
}
}

func hintSliceEntries(entries scoring.Entries, limit int) scoring.Entries {
if limit < len(entries) {
return entries[0:limit]
}

return entries
}

func init() {
cli.RegisterCommand("hint", "Hints relevant paths for jumping.", hintCmd)
}
44 changes: 1 addition & 43 deletions cmd/hint_test.go
Expand Up @@ -23,53 +23,11 @@ func Test_hintCmd_short(t *testing.T) {
}

output := capture(&os.Stdout, func() {
assert.Nil(t, hintCmd(cli.Args{}, conf))
})

lines := strings.Fields(output)
assert.Len(t, 2, lines)

assert.Equal(t, p1, lines[0])
assert.Equal(t, p2, lines[1])
}

func Test_hintCmd_long(t *testing.T) {
p1 := p.Join(td, "web-console")
p2 := p.Join(td, "/client/website")
p3 := p.Join(td, "web")

conf := &testConfig{
Entries: s.Entries{
&s.Entry{p3, &s.Score{Weight: 80, Age: s.Now}},
&s.Entry{p2, &s.Score{Weight: 90, Age: s.Now}},
&s.Entry{p1, &s.Score{Weight: 100, Age: s.Now}},
},
}

output := capture(&os.Stdout, func() {
assert.Nil(t, hintCmd(cli.Args{"wc"}, conf))
assert.Nil(t, hintCmd(cli.Args{"webcons"}, conf))
})

lines := strings.Fields(output)
assert.Len(t, 1, lines)

assert.Equal(t, p1, lines[0])

output = capture(&os.Stdout, func() {
assert.Nil(t, hintCmd(cli.Args{"webonos"}, conf))
})

// If you write more than 6 chars, maybe you need more options.
lines = strings.Fields(output)
assert.Len(t, 3, lines)

output = capture(&os.Stdout, func() {
assert.Nil(t, hintCmd(cli.Args{"client/webs"}, conf))
})

// If you wrote more than 9 chars, well, we tried.
lines = strings.Fields(output)
assert.Len(t, 1, lines)

assert.Equal(t, p2, lines[0])
}
14 changes: 7 additions & 7 deletions cmd/shell_test.go
Expand Up @@ -50,17 +50,17 @@ func Example_shellCmd_Zsh() {
// jump chdir
// }
//
// typeset -gaU chpwd_functions
// chpwd_functions+=__jump_chpwd
// jump_completion() {
// reply=(${(f)"$(jump hint $@)"})
// }
//
// j() {
// local dir="$(jump cd $@)"
// test -d "$dir" && cd "$dir"
// }
//
// jump_completion() {
// reply=($(jump hint $@))
// }
// typeset -gaU chpwd_functions
// chpwd_functions+=__jump_chpwd
//
// compctl -U -K jump_completion j
}
Expand All @@ -81,8 +81,8 @@ func Example_shellCmd_Fish() {
// end
//
// function __jump_hint
// set -l input (string replace -r '^j ' '' -- (commandline -cp))
// jump hint $input
// set -l term (string replace -r '^j ' '' -- (commandline -cp))
// jump hint $term
// end
//
// function j
Expand Down
4 changes: 2 additions & 2 deletions shell/fish.go
Expand Up @@ -13,8 +13,8 @@ function __jump_add --on-variable PWD
end
function __jump_hint
set -l input (string replace -r '^{{.Bind}} ' '' -- (commandline -cp))
jump hint $input
set -l term (string replace -r '^{{.Bind}} ' '' -- (commandline -cp))
jump hint $term
end
function {{.Bind}}
Expand Down
10 changes: 5 additions & 5 deletions shell/zsh.go
Expand Up @@ -11,17 +11,17 @@ __jump_chpwd() {
jump chdir
}
typeset -gaU chpwd_functions
chpwd_functions+=__jump_chpwd
jump_completion() {
reply=(${(f)"$(jump hint $@)"})
}
{{.Bind}}() {
local dir="$(jump cd $@)"
test -d "$dir" && cd "$dir"
}
jump_completion() {
reply=($(jump hint $@))
}
typeset -gaU chpwd_functions
chpwd_functions+=__jump_chpwd
compctl -U -K jump_completion {{.Bind}}
`)

0 comments on commit f26678f

Please sign in to comment.