Skip to content

Commit 3cfd455

Browse files
committed
update pt choice
1 parent 34e070d commit 3cfd455

File tree

4 files changed

+209
-6
lines changed

4 files changed

+209
-6
lines changed

cmd/cmd.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ var (
2323
twoWeeks = 2 * 7 * 24 * time.Hour
2424
)
2525

26-
func initPlatform() string {
27-
return tldr.PlatformOSX.String()
28-
}
26+
var defaultPlatform = tldr.PlatformOSX
2927

3028
const (
3129
platformFlag = "platform"
@@ -79,7 +77,7 @@ func NewRootCmd() *cobra.Command {
7977
rootCmd.PersistentFlags().BoolVarP(&cfg.version, versionFlag, string(versionFlag[0]), false, "show the client version")
8078
rootCmd.PersistentFlags().BoolVarP(&cfg.update, updateFlag, string(updateFlag[0]), false, "update tldr database")
8179
rootCmd.PersistentFlags().StringVarP(&ptString, platformFlag, string(platformFlag[0]),
82-
initPlatform(), "select from linux/osx/sunos/windows")
80+
defaultPlatform.String(), "select from linux/osx/sunos/windows")
8381
rootCmd.PersistentFlags().StringVarP(&cfg.language, languageFlag, "L", "", "select language e.g.) en")
8482

8583
// internal flag
@@ -252,8 +250,8 @@ func (cfg *config) printFuzzyPages(cmds []string) error {
252250
suggestions := index.Commands.Search(cmds)
253251
for _, cmd := range suggestions {
254252
complete := cmd.Name
255-
// TODO when multi pt, computer pt must be used
256-
if pt := cmd.Platforms[0]; pt != tldr.PlatformCommon && pt != cfg.platform {
253+
pt := choicePlatform(cmd.Platforms, cfg.platform)
254+
if pt != tldr.PlatformCommon && pt != defaultPlatform {
257255
complete = fmt.Sprintf("-%s %s %s",
258256
string(platformFlag[0]),
259257
pt,
@@ -307,6 +305,27 @@ func (cfg *config) updateDB() error {
307305
return nil
308306
}
309307

308+
func choicePlatform(pts []tldr.Platform, selected tldr.Platform) tldr.Platform {
309+
if len(pts) >= 2 {
310+
// if there are more than two platforms,
311+
// priority are follow
312+
// selected pt, common, others
313+
for _, pt := range pts {
314+
if pt == selected {
315+
return selected
316+
}
317+
}
318+
319+
for _, pt := range pts {
320+
if pt == tldr.PlatformCommon {
321+
return tldr.PlatformCommon
322+
}
323+
}
324+
}
325+
326+
return pts[0]
327+
}
328+
310329
// Execute Execute root cmd
311330
func Execute(rootCmd *cobra.Command) {
312331
rootCmd.SetOut(outStream)

cmd/cmd_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"encoding/json"
66
"io/ioutil"
77
"path/filepath"
8+
"reflect"
89
"testing"
910

11+
"github.com/konoui/alfred-tldr/pkg/tldr"
1012
"github.com/konoui/go-alfred"
1113
"github.com/mattn/go-shellwords"
1214
)
@@ -53,6 +55,20 @@ func TestExecute(t *testing.T) {
5355
filepath: testdataPath("test_output_pstree_with_fuzzy.json"),
5456
},
5557
},
58+
{
59+
name: "multi platform command yank without -p flag return computer pt OSX",
60+
args: args{
61+
command: "yan --fuzzy",
62+
filepath: testdataPath("test_output_yank_without_p-flag_fuzzy.json"),
63+
},
64+
},
65+
{
66+
name: "multi platform command yank with -p flag return specified pt",
67+
args: args{
68+
command: "yan -p linux --fuzzy",
69+
filepath: testdataPath("test_output_yank_with_p-flag_with_fuzzy.json"),
70+
},
71+
},
5672
{
5773
name: "show no error when cache expired",
5874
args: args{
@@ -198,3 +214,55 @@ func writeFile(filename string, data []byte) error {
198214
}
199215
return nil
200216
}
217+
218+
func Test_choicePlatform(t *testing.T) {
219+
type args struct {
220+
pts []tldr.Platform
221+
selected tldr.Platform
222+
}
223+
tests := []struct {
224+
name string
225+
args args
226+
want tldr.Platform
227+
}{
228+
{
229+
name: "common return linux",
230+
args: args{
231+
pts: []tldr.Platform{
232+
tldr.PlatformCommon,
233+
},
234+
selected: tldr.PlatformLinux,
235+
},
236+
want: tldr.PlatformCommon,
237+
},
238+
{
239+
name: "common,linux return linux",
240+
args: args{
241+
pts: []tldr.Platform{
242+
tldr.PlatformCommon,
243+
tldr.PlatformLinux,
244+
},
245+
selected: tldr.PlatformLinux,
246+
},
247+
want: tldr.PlatformLinux,
248+
},
249+
{
250+
name: "common,linux return common",
251+
args: args{
252+
pts: []tldr.Platform{
253+
tldr.PlatformCommon,
254+
tldr.PlatformLinux,
255+
},
256+
selected: tldr.PlatformOSX,
257+
},
258+
want: tldr.PlatformCommon,
259+
},
260+
}
261+
for _, tt := range tests {
262+
t.Run(tt.name, func(t *testing.T) {
263+
if got := choicePlatform(tt.args.pts, tt.args.selected); !reflect.DeepEqual(got, tt.want) {
264+
t.Errorf("choicePlatform() = %v, want %v", got, tt.want)
265+
}
266+
})
267+
}
268+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"items": [
3+
{
4+
"title": "yank",
5+
"subtitle": "Platforms: [osx linux]",
6+
"icon": {
7+
"path": "candidate.png"
8+
},
9+
"autocomplete": "-p linux yank",
10+
"valid": false
11+
},
12+
{
13+
"title": "yarn",
14+
"subtitle": "Platforms: [common]",
15+
"icon": {
16+
"path": "candidate.png"
17+
},
18+
"autocomplete": "yarn",
19+
"valid": false
20+
},
21+
{
22+
"title": "yarn why",
23+
"subtitle": "Platforms: [common]",
24+
"icon": {
25+
"path": "candidate.png"
26+
},
27+
"autocomplete": "yarn why",
28+
"valid": false
29+
},
30+
{
31+
"title": "systemd analyze",
32+
"subtitle": "Platforms: [linux]",
33+
"icon": {
34+
"path": "candidate.png"
35+
},
36+
"autocomplete": "-p linux systemd analyze",
37+
"valid": false
38+
},
39+
{
40+
"title": "ssh keyscan",
41+
"subtitle": "Platforms: [common]",
42+
"icon": {
43+
"path": "candidate.png"
44+
},
45+
"autocomplete": "ssh keyscan",
46+
"valid": false
47+
},
48+
{
49+
"title": "pyenv virtualenv",
50+
"subtitle": "Platforms: [common]",
51+
"icon": {
52+
"path": "candidate.png"
53+
},
54+
"autocomplete": "pyenv virtualenv",
55+
"valid": false
56+
}
57+
]
58+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"items": [
3+
{
4+
"title": "yank",
5+
"subtitle": "Platforms: [osx linux]",
6+
"icon": {
7+
"path": "candidate.png"
8+
},
9+
"autocomplete": "yank",
10+
"valid": false
11+
},
12+
{
13+
"title": "yarn",
14+
"subtitle": "Platforms: [common]",
15+
"icon": {
16+
"path": "candidate.png"
17+
},
18+
"autocomplete": "yarn",
19+
"valid": false
20+
},
21+
{
22+
"title": "yarn why",
23+
"subtitle": "Platforms: [common]",
24+
"icon": {
25+
"path": "candidate.png"
26+
},
27+
"autocomplete": "yarn why",
28+
"valid": false
29+
},
30+
{
31+
"title": "systemd analyze",
32+
"subtitle": "Platforms: [linux]",
33+
"icon": {
34+
"path": "candidate.png"
35+
},
36+
"autocomplete": "-p linux systemd analyze",
37+
"valid": false
38+
},
39+
{
40+
"title": "ssh keyscan",
41+
"subtitle": "Platforms: [common]",
42+
"icon": {
43+
"path": "candidate.png"
44+
},
45+
"autocomplete": "ssh keyscan",
46+
"valid": false
47+
},
48+
{
49+
"title": "pyenv virtualenv",
50+
"subtitle": "Platforms: [common]",
51+
"icon": {
52+
"path": "candidate.png"
53+
},
54+
"autocomplete": "pyenv virtualenv",
55+
"valid": false
56+
}
57+
]
58+
}

0 commit comments

Comments
 (0)