Skip to content

Commit

Permalink
Merge pull request #660 from rsteube/files-tag
Browse files Browse the repository at this point in the history
separate tags for files and directories
  • Loading branch information
rsteube authored Jan 2, 2023
2 parents cff823f + 7b88e25 commit ad88199
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
14 changes: 7 additions & 7 deletions action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestActionDirectories(t *testing.T) {
"internal/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
"pkg/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
"third_party/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
).NoSpace('/').Invoke(Context{}),
).NoSpace('/').Tag("directories").Invoke(Context{}),
ActionDirectories().Invoke(Context{CallbackValue: ""}).Filter([]string{"vendor/"}),
)

Expand All @@ -132,22 +132,22 @@ func TestActionDirectories(t *testing.T) {
"internal/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
"pkg/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
"third_party/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
).NoSpace('/').Invoke(Context{}).Prefix("./"),
).NoSpace('/').Tag("directories").Invoke(Context{}).Prefix("./"),
ActionDirectories().Invoke(Context{CallbackValue: "./"}).Filter([]string{"./vendor/"}),
)

assertEqual(t,
ActionStyledValues(
"_test/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
"cmd/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
).NoSpace('/').Invoke(Context{}).Prefix("example/"),
).NoSpace('/').Tag("directories").Invoke(Context{}).Prefix("example/"),
ActionDirectories().Invoke(Context{CallbackValue: "example/"}),
)

assertEqual(t,
ActionStyledValues(
"cmd/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
).NoSpace('/').Invoke(Context{}).Prefix("example/"),
).NoSpace('/').Tag("directories").Invoke(Context{}).Prefix("example/"),
ActionDirectories().Invoke(Context{CallbackValue: "example/cm"}),
)
}
Expand All @@ -161,7 +161,7 @@ func TestActionFiles(t *testing.T) {
"internal/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
"pkg/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
"third_party/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
).NoSpace('/').Invoke(Context{}),
).NoSpace('/').Tag("files").Invoke(Context{}),
ActionFiles(".md").Invoke(Context{CallbackValue: ""}).Filter([]string{"vendor/"}),
)

Expand All @@ -172,7 +172,7 @@ func TestActionFiles(t *testing.T) {
"cmd/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
"main.go", style.Of("fg-default", "bg-default"),
"main_test.go", style.Of("fg-default", "bg-default"),
).NoSpace('/').Invoke(Context{}).Prefix("example/"),
).NoSpace('/').Tag("files").Invoke(Context{}).Prefix("example/"),
ActionFiles().Invoke(Context{CallbackValue: "example/"}).Filter([]string{"example/example"}),
)
}
Expand All @@ -194,7 +194,7 @@ func TestActionFilesChdir(t *testing.T) {
ActionStyledValues(
"action.go", style.Of("fg-default", "bg-default"),
"snippet.go", style.Of("fg-default", "bg-default"),
).NoSpace('/').Invoke(Context{}).Prefix("elvish/"),
).NoSpace('/').Tag("files").Invoke(Context{}).Prefix("elvish/"),
ActionFiles().Chdir("internal/shell").Invoke(Context{CallbackValue: "elvish/"}),
)

Expand Down
4 changes: 2 additions & 2 deletions defaultActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func ActionDirectories() Action {
}
return ""
})
})
}).Tag("directories")
}

// ActionFiles completes files with optional suffix filtering.
Expand All @@ -118,7 +118,7 @@ func ActionFiles(suffix ...string) Action {
}
return ""
})
})
}).Tag("files")
}

// ActionValues completes arbitrary keywords (values).
Expand Down
12 changes: 6 additions & 6 deletions example/cmd/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ func TestAction(t *testing.T) {

s.Run("action", "--callback", "").Expect(carapace.ActionMessage("values flag is not set"))
s.Run("action", "--values", "first", "--callback", "").Expect(carapace.ActionMessage("values flag is set to: 'first'"))
s.Run("action", "--directories", "").Expect(carapace.ActionValues("dirA/", "dirB/").StyleF(style.ForPath))
s.Run("action", "--directories", "dirB/").Expect(carapace.ActionValues("dirC/").Prefix("dirB/").StyleF(style.ForPath))
s.Run("action", "--directories", "").Expect(carapace.ActionValues("dirA/", "dirB/").Tag("directories").StyleF(style.ForPath))
s.Run("action", "--directories", "dirB/").Expect(carapace.ActionValues("dirC/").Prefix("dirB/").Tag("directories").StyleF(style.ForPath))
s.Run("action", "--exec-command", "").Expect(carapace.ActionValues("origin", "fork"))
s.Run("action", "--files", "").Expect(carapace.ActionValues("dirA/", "dirB/", "file5.go").StyleF(style.ForPath))
s.Run("action", "--files-filtered", "").Expect(carapace.ActionValues("dirA/", "dirB/").StyleF(style.ForPath))
s.Run("action", "--files-filtered", "dirB/").Expect(carapace.ActionValues("dirC/", "file4.md").Prefix("dirB/").StyleF(style.ForPath))
s.Run("action", "--files", "").Expect(carapace.ActionValues("dirA/", "dirB/", "file5.go").Tag("files").StyleF(style.ForPath))
s.Run("action", "--files-filtered", "").Expect(carapace.ActionValues("dirA/", "dirB/").Tag("files").StyleF(style.ForPath))
s.Run("action", "--files-filtered", "dirB/").Expect(carapace.ActionValues("dirC/", "file4.md").Tag("files").Prefix("dirB/").StyleF(style.ForPath))
s.Run("action", "--import", "").Expect(carapace.ActionValues("first", "second", "third"))
s.Run("action", "--import", "s").Expect(carapace.ActionValues("second"))
s.Run("action", "--message", "").Expect(carapace.ActionMessage("example message"))
Expand All @@ -37,7 +37,7 @@ func TestAction(t *testing.T) {
s.Run("action", "--multiparts-nested", "").Expect(carapace.ActionValues("DIRECTORY", "FILE", "VALUE").Suffix("="))
s.Run("action", "--multiparts-nested", "VALUE=").Expect(carapace.ActionValues("one", "two", "three").Prefix("VALUE="))
s.Run("action", "--multiparts-nested", "VALUE=two,").Expect(carapace.ActionValues("DIRECTORY", "FILE").Prefix("VALUE=two,").Suffix("="))
s.Run("action", "--multiparts-nested", "VALUE=two,DIRECTORY=").Expect(carapace.ActionValues("dirA/", "dirB/").StyleF(style.ForPath).Prefix("VALUE=two,DIRECTORY="))
s.Run("action", "--multiparts-nested", "VALUE=two,DIRECTORY=").Expect(carapace.ActionValues("dirA/", "dirB/").Tag("directories").StyleF(style.ForPath).Prefix("VALUE=two,DIRECTORY="))
s.Run("action", "--styled-values", "s").Expect(carapace.ActionStyledValues("second", style.Blue))
s.Run("action", "--styled-values-described", "t").Expect(carapace.ActionStyledValuesDescribed("third", "description of third", style.Of("#112233", style.Italic), "thirdalias", "description of third", style.BgBrightMagenta))
s.Run("action", "--values", "sec").Expect(carapace.ActionValues("second"))
Expand Down
6 changes: 3 additions & 3 deletions example/cmd/multiparts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ func TestMultiparts(t *testing.T) {
s.Run("multiparts", "").Expect(carapace.ActionValues("DIRECTORY", "FILE", "VALUE").Suffix("="))
s.Run("multiparts", "D").Expect(carapace.ActionValues("DIRECTORY").Suffix("="))
s.Run("multiparts", "DIRECTORY").Expect(carapace.ActionValues("DIRECTORY").Suffix("="))
s.Run("multiparts", "DIRECTORY=").Expect(carapace.ActionValues("dirA/", "dirB/").StyleF(style.ForPath).Prefix("DIRECTORY="))
s.Run("multiparts", "DIRECTORY=").Expect(carapace.ActionValues("dirA/", "dirB/").Tag("directories").StyleF(style.ForPath).Prefix("DIRECTORY="))
s.Run("multiparts", "VALUE=").Expect(carapace.ActionValues("one", "two", "three").Prefix("VALUE="))
s.Run("multiparts", "VALUE=o").Expect(carapace.ActionValues("one").Prefix("VALUE="))
s.Run("multiparts", "VALUE=one,").Expect(carapace.ActionValues("DIRECTORY", "FILE").Prefix("VALUE=one,").Suffix("="))
s.Run("multiparts", "VALUE=one,F").Expect(carapace.ActionValues("FILE").Prefix("VALUE=one,").Suffix("="))
s.Run("multiparts", "VALUE=one,FILE=").Expect(carapace.ActionValues("dirA/", "dirB/", "file5.go").StyleF(style.ForPath).Prefix("VALUE=one,FILE="))
s.Run("multiparts", "VALUE=one,FILE=dirB/").Expect(carapace.ActionValues("dirC/", "file4.md").Prefix("dirB/").StyleF(style.ForPath).Prefix("VALUE=one,FILE="))
s.Run("multiparts", "VALUE=one,FILE=").Expect(carapace.ActionValues("dirA/", "dirB/", "file5.go").Tag("files").StyleF(style.ForPath).Prefix("VALUE=one,FILE="))
s.Run("multiparts", "VALUE=one,FILE=dirB/").Expect(carapace.ActionValues("dirC/", "file4.md").Tag("files").Prefix("dirB/").StyleF(style.ForPath).Prefix("VALUE=one,FILE="))
})
}
2 changes: 1 addition & 1 deletion internalActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func actionPath(fileSuffixes []string, dirOnly bool) Action {
return ActionStyledValues(vals...).Invoke(Context{}).Prefix("./").ToA()
}
return ActionStyledValues(vals...)
}).Tag("files")
})
}

func actionFlags(cmd *cobra.Command) Action {
Expand Down

0 comments on commit ad88199

Please sign in to comment.