mirror of
https://github.com/ddworken/hishtory.git
synced 2025-02-15 10:01:25 +01:00
Add easier config options for configuring default search columns (#287)
* Add easier config options for configuring default search columns * Remove TODO
This commit is contained in:
parent
567984fb6f
commit
b4ef6c963e
@ -52,8 +52,22 @@ var addDisplayedColumnsCmd = &cobra.Command{
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
ctx := hctx.MakeContext()
|
||||
config := hctx.GetConf(ctx)
|
||||
vals := args
|
||||
config.DisplayedColumns = append(config.DisplayedColumns, vals...)
|
||||
config.DisplayedColumns = append(config.DisplayedColumns, args...)
|
||||
lib.CheckFatalError(hctx.SetConfig(config))
|
||||
},
|
||||
}
|
||||
|
||||
var addDefaultSearchColumnsCmd = &cobra.Command{
|
||||
Use: "default-search-columns",
|
||||
Aliases: []string{"default-search-column"},
|
||||
Short: "Add a column that is used for \"default\" search queries that don't use any search atoms",
|
||||
Long: "By default hishtory queries are checked against `command`, `current_working_directory`, and `hostname`. This option can be used to add additional columns to the list of columns checked in default queries. E.g. to add a custom column named `git_remote` to the list of default search columns, you would run `hishtory config-add default-search-columns git_remote`",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
ctx := hctx.MakeContext()
|
||||
config := hctx.GetConf(ctx)
|
||||
lib.CheckFatalError(validateDefaultSearchColumns(ctx, args))
|
||||
config.DefaultSearchColumns = append(config.DefaultSearchColumns, args...)
|
||||
lib.CheckFatalError(hctx.SetConfig(config))
|
||||
},
|
||||
}
|
||||
@ -62,4 +76,5 @@ func init() {
|
||||
rootCmd.AddCommand(configAddCmd)
|
||||
configAddCmd.AddCommand(addCustomColumnsCmd)
|
||||
configAddCmd.AddCommand(addDisplayedColumnsCmd)
|
||||
configAddCmd.AddCommand(addDefaultSearchColumnsCmd)
|
||||
}
|
||||
|
@ -1,8 +1,10 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"slices"
|
||||
|
||||
"github.com/ddworken/hishtory/client/hctx"
|
||||
"github.com/ddworken/hishtory/client/lib"
|
||||
@ -70,13 +72,7 @@ var deleteDisplayedColumnCommand = &cobra.Command{
|
||||
deletedColumns := args
|
||||
newColumns := make([]string, 0)
|
||||
for _, c := range config.DisplayedColumns {
|
||||
isDeleted := false
|
||||
for _, d := range deletedColumns {
|
||||
if c == d {
|
||||
isDeleted = true
|
||||
}
|
||||
}
|
||||
if !isDeleted {
|
||||
if !slices.Contains(deletedColumns, c) {
|
||||
newColumns = append(newColumns, c)
|
||||
}
|
||||
}
|
||||
@ -85,8 +81,32 @@ var deleteDisplayedColumnCommand = &cobra.Command{
|
||||
},
|
||||
}
|
||||
|
||||
var deleteDefaultSearchColumnCmd = &cobra.Command{
|
||||
Use: "default-search-columns",
|
||||
Aliases: []string{"default-search-column"},
|
||||
Short: "Delete a default search column",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
ctx := hctx.MakeContext()
|
||||
config := hctx.GetConf(ctx)
|
||||
deletedColumns := args
|
||||
newColumns := make([]string, 0)
|
||||
if slices.Contains(deletedColumns, "command") {
|
||||
lib.CheckFatalError(fmt.Errorf("command is a required default search column"))
|
||||
}
|
||||
for _, c := range config.DefaultSearchColumns {
|
||||
if !slices.Contains(deletedColumns, c) {
|
||||
newColumns = append(newColumns, c)
|
||||
}
|
||||
}
|
||||
config.DefaultSearchColumns = newColumns
|
||||
lib.CheckFatalError(hctx.SetConfig(config))
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(configDeleteCmd)
|
||||
configDeleteCmd.AddCommand(deleteCustomColumnsCmd)
|
||||
configDeleteCmd.AddCommand(deleteDisplayedColumnCommand)
|
||||
configDeleteCmd.AddCommand(deleteDefaultSearchColumnCmd)
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
@ -276,27 +277,31 @@ var setFullScreenCmd = &cobra.Command{
|
||||
},
|
||||
}
|
||||
|
||||
func validateDefaultSearchColumns(ctx context.Context, columns []string) error {
|
||||
customColNames, err := lib.GetAllCustomColumnNames(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get custom column names: %v", err)
|
||||
}
|
||||
for _, col := range columns {
|
||||
if !slices.Contains(lib.SUPPORTED_DEFAULT_COLUMNS, col) && !slices.Contains(customColNames, col) {
|
||||
return fmt.Errorf("column %q is not a valid column name", col)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var setDefaultSearchColumns = &cobra.Command{
|
||||
Use: "default-search-columns",
|
||||
Short: "Get the list of columns that are used for \"default\" search queries that don't use any search atoms",
|
||||
Long: "By default hishtory queries are checked against `command`, `current_working_directory`, and `hostname`. This option can be used to exclude `current_working_directory` and/or `hostname` from default search queries. E.g. `hishtory config-set default-search-columns hostname command` would exclude `current_working_directory` from default searches.",
|
||||
Short: "Set the list of columns that are used for \"default\" search queries that don't use any search atoms",
|
||||
Long: "By default hishtory queries are checked against `command`, `current_working_directory`, and `hostname`. This option can be used to exclude `current_working_directory` and/or `hostname` from default search queries. E.g. `hishtory config-set default-search-columns hostname command` would exclude `current_working_directory` from default searches. Alternatively, it can be used to include custom columns in default searches.",
|
||||
Args: cobra.OnlyValidArgs,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// TODO: Add configAdd and configDelete support for this command
|
||||
ctx := hctx.MakeContext()
|
||||
config := hctx.GetConf(ctx)
|
||||
if !slices.Contains(args, "command") {
|
||||
lib.CheckFatalError(fmt.Errorf("command is a required default search column"))
|
||||
}
|
||||
customColNames, err := lib.GetAllCustomColumnNames(ctx)
|
||||
if err != nil {
|
||||
lib.CheckFatalError(fmt.Errorf("failed to get custom column names: %v", err))
|
||||
}
|
||||
for _, col := range args {
|
||||
if !slices.Contains(lib.SUPPORTED_DEFAULT_COLUMNS, col) && !slices.Contains(customColNames, col) {
|
||||
lib.CheckFatalError(fmt.Errorf("column %q is not a valid column name", col))
|
||||
}
|
||||
}
|
||||
lib.CheckFatalError(validateDefaultSearchColumns(ctx, args))
|
||||
config.DefaultSearchColumns = args
|
||||
lib.CheckFatalError(hctx.SetConfig(config))
|
||||
},
|
||||
|
@ -3610,4 +3610,28 @@ func TestDefaultSearchColumns(t *testing.T) {
|
||||
testutils.CompareGoldens(t, out, "TestDefaultSearchColumns-MyCol-baz")
|
||||
}
|
||||
|
||||
func TestDefaultSearchColumnsAddDelete(t *testing.T) {
|
||||
markTestForSharding(t, 21)
|
||||
defer testutils.BackupAndRestore(t)()
|
||||
tester := zshTester{}
|
||||
installHishtory(t, tester, "")
|
||||
|
||||
// Use config-set
|
||||
out := tester.RunInteractiveShell(t, ` hishtory config-get default-search-columns`)
|
||||
require.Equal(t, out, "command hostname current_working_directory \n")
|
||||
tester.RunInteractiveShell(t, ` hishtory config-set default-search-columns 'hostname' 'command'`)
|
||||
out = tester.RunInteractiveShell(t, ` hishtory config-get default-search-columns`)
|
||||
require.Equal(t, out, "hostname command \n")
|
||||
|
||||
// Use config-add
|
||||
tester.RunInteractiveShell(t, ` hishtory config-add default-search-columns 'current_working_directory'`)
|
||||
out = tester.RunInteractiveShell(t, ` hishtory config-get default-search-columns`)
|
||||
require.Equal(t, out, "hostname command current_working_directory \n")
|
||||
|
||||
// Use config-delete
|
||||
tester.RunInteractiveShell(t, ` hishtory config-delete default-search-columns 'hostname'`)
|
||||
out = tester.RunInteractiveShell(t, ` hishtory config-get default-search-columns`)
|
||||
require.Equal(t, out, "command current_working_directory \n")
|
||||
}
|
||||
|
||||
// TODO: somehow test/confirm that hishtory works even if only bash/only zsh is installed
|
||||
|
Loading…
Reference in New Issue
Block a user