2022-11-14 03:26:23 +01:00
package cmd
import (
2022-11-18 08:14:33 +01:00
"fmt"
2022-11-14 03:26:23 +01:00
"log"
2023-12-19 05:32:11 +01:00
"os"
"strings"
2022-11-14 03:26:23 +01:00
"github.com/ddworken/hishtory/client/hctx"
"github.com/ddworken/hishtory/client/lib"
"github.com/spf13/cobra"
)
var configSetCmd = & cobra . Command {
2022-11-15 05:18:22 +01:00
Use : "config-set" ,
Short : "Set the value of a config option" ,
GroupID : GROUP_ID_CONFIG ,
2022-11-14 03:26:23 +01:00
Run : func ( cmd * cobra . Command , args [ ] string ) {
2022-11-27 20:59:06 +01:00
lib . CheckFatalError ( cmd . Help ( ) )
2023-12-19 05:32:11 +01:00
os . Exit ( 1 )
2022-11-14 03:26:23 +01:00
} ,
}
var setEnableControlRCmd = & cobra . Command {
Use : "enable-control-r" ,
Short : "Whether hishtory replaces your shell's default control-r" ,
Args : cobra . MatchAll ( cobra . ExactArgs ( 1 ) , cobra . OnlyValidArgs ) ,
ValidArgs : [ ] string { "true" , "false" } ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
val := args [ 0 ]
if val != "true" && val != "false" {
log . Fatalf ( "Unexpected config value %s, must be one of: true, false" , val )
}
ctx := hctx . MakeContext ( )
config := hctx . GetConf ( ctx )
config . ControlRSearchEnabled = ( val == "true" )
lib . CheckFatalError ( hctx . SetConfig ( config ) )
2022-11-18 08:14:33 +01:00
fmt . Println ( "Updated the control-r integration, please restart your shell for this to take effect..." )
2022-11-14 03:26:23 +01:00
} ,
}
var setFilterDuplicateCommandsCmd = & cobra . Command {
Use : "filter-duplicate-commands" ,
Short : "Whether hishtory filters out duplicate commands when displaying your history" ,
Args : cobra . MatchAll ( cobra . ExactArgs ( 1 ) , cobra . OnlyValidArgs ) ,
ValidArgs : [ ] string { "true" , "false" } ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
val := args [ 0 ]
if val != "true" && val != "false" {
log . Fatalf ( "Unexpected config value %s, must be one of: true, false" , val )
}
ctx := hctx . MakeContext ( )
config := hctx . GetConf ( ctx )
config . FilterDuplicateCommands = ( val == "true" )
lib . CheckFatalError ( hctx . SetConfig ( config ) )
} ,
}
2023-08-27 23:24:59 +02:00
var setBetaModeCommand = & cobra . Command {
Use : "beta-mode" ,
Short : "Enable beta-mode to opt-in to unreleased features" ,
Args : cobra . MatchAll ( cobra . ExactArgs ( 1 ) , cobra . OnlyValidArgs ) ,
ValidArgs : [ ] string { "true" , "false" } ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
val := args [ 0 ]
if val != "true" && val != "false" {
log . Fatalf ( "Unexpected config value %s, must be one of: true, false" , val )
}
ctx := hctx . MakeContext ( )
config := hctx . GetConf ( ctx )
config . BetaMode = ( val == "true" )
lib . CheckFatalError ( hctx . SetConfig ( config ) )
} ,
}
2024-01-08 06:06:22 +01:00
var setDefaultFilterCommand = & cobra . Command {
Use : "default-filter" ,
Short : "Add a default filter that will be applied to all search queries (e.g. `exit_code:0` to filter to only commands that executed successfully)" ,
Args : cobra . ExactArgs ( 1 ) ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
ctx := hctx . MakeContext ( )
config := hctx . GetConf ( ctx )
config . DefaultFilter = args [ 0 ]
lib . CheckFatalError ( hctx . SetConfig ( config ) )
} ,
}
2023-11-12 12:09:56 +01:00
var setEnableAiCompletionCmd = & cobra . Command {
Use : "ai-completion" ,
Short : "Enable AI completion for searches starting with '?'" ,
Long : "Note that AI completion requests are sent to the shared hiSHtory backend and then to OpenAI. Requests are not logged, but still be careful not to put anything sensitive in queries." ,
Args : cobra . MatchAll ( cobra . ExactArgs ( 1 ) , cobra . OnlyValidArgs ) ,
ValidArgs : [ ] string { "true" , "false" } ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
val := args [ 0 ]
if val != "true" && val != "false" {
log . Fatalf ( "Unexpected config value %s, must be one of: true, false" , val )
}
ctx := hctx . MakeContext ( )
config := hctx . GetConf ( ctx )
config . AiCompletion = ( val == "true" )
lib . CheckFatalError ( hctx . SetConfig ( config ) )
} ,
}
2023-11-12 14:19:31 +01:00
var setPresavingCmd = & cobra . Command {
Use : "presaving" ,
Short : "Enable 'presaving' of shell entries that never finish running" ,
Long : "If enabled, there is a slight risk of duplicate history entries. If disabled, non-terminating history entries will not be recorded." ,
Args : cobra . MatchAll ( cobra . ExactArgs ( 1 ) , cobra . OnlyValidArgs ) ,
ValidArgs : [ ] string { "true" , "false" } ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
val := args [ 0 ]
if val != "true" && val != "false" {
log . Fatalf ( "Unexpected config value %s, must be one of: true, false" , val )
}
ctx := hctx . MakeContext ( )
config := hctx . GetConf ( ctx )
config . EnablePresaving = ( val == "true" )
lib . CheckFatalError ( hctx . SetConfig ( config ) )
} ,
}
2023-11-12 12:09:56 +01:00
2023-10-12 03:18:56 +02:00
var setHighlightMatchesCmd = & cobra . Command {
Use : "highlight-matches" ,
Short : "Enable highlight-matches to enable highlighting of matches in the search results" ,
Args : cobra . MatchAll ( cobra . ExactArgs ( 1 ) , cobra . OnlyValidArgs ) ,
ValidArgs : [ ] string { "true" , "false" } ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
val := args [ 0 ]
if val != "true" && val != "false" {
log . Fatalf ( "Unexpected config value %s, must be one of: true, false" , val )
}
ctx := hctx . MakeContext ( )
config := hctx . GetConf ( ctx )
config . HighlightMatches = ( val == "true" )
lib . CheckFatalError ( hctx . SetConfig ( config ) )
} ,
}
2022-11-14 03:26:23 +01:00
var setDisplayedColumnsCmd = & cobra . Command {
2023-12-19 18:34:12 +01:00
Use : "displayed-columns" ,
Aliases : [ ] string { "displayed-column" } ,
Short : "The list of columns that hishtory displays" ,
Args : cobra . MinimumNArgs ( 1 ) ,
2022-11-14 03:26:23 +01:00
Run : func ( cmd * cobra . Command , args [ ] string ) {
ctx := hctx . MakeContext ( )
config := hctx . GetConf ( ctx )
config . DisplayedColumns = args
lib . CheckFatalError ( hctx . SetConfig ( config ) )
} ,
}
var setTimestampFormatCmd = & cobra . Command {
Use : "timestamp-format" ,
Short : "The go format string to use for formatting the timestamp" ,
Args : cobra . ExactArgs ( 1 ) ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
ctx := hctx . MakeContext ( )
config := hctx . GetConf ( ctx )
config . TimestampFormat = args [ 0 ]
lib . CheckFatalError ( hctx . SetConfig ( config ) )
} ,
}
2023-12-19 05:32:11 +01:00
var setColorSchemeCmd = & cobra . Command {
Use : "color-scheme" ,
Short : "Set a custom color scheme" ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
lib . CheckFatalError ( cmd . Help ( ) )
os . Exit ( 1 )
} ,
}
var setColorSchemeSelectedText = & cobra . Command {
Use : "selected-text" ,
Short : "Set the color of the selected text to the given hexadecimal color" ,
Args : cobra . ExactArgs ( 1 ) ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
lib . CheckFatalError ( validateColor ( args [ 0 ] ) )
ctx := hctx . MakeContext ( )
config := hctx . GetConf ( ctx )
config . ColorScheme . SelectedText = args [ 0 ]
lib . CheckFatalError ( hctx . SetConfig ( config ) )
} ,
}
var setColorSchemeSelectedBackground = & cobra . Command {
Use : "selected-background" ,
Short : "Set the background color of the selected row to the given hexadecimal color" ,
Args : cobra . ExactArgs ( 1 ) ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
lib . CheckFatalError ( validateColor ( args [ 0 ] ) )
ctx := hctx . MakeContext ( )
config := hctx . GetConf ( ctx )
config . ColorScheme . SelectedBackground = args [ 0 ]
lib . CheckFatalError ( hctx . SetConfig ( config ) )
} ,
}
var setColorSchemeBorderColor = & cobra . Command {
Use : "border-color" ,
Short : "Set the color of the table borders" ,
Args : cobra . ExactArgs ( 1 ) ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
lib . CheckFatalError ( validateColor ( args [ 0 ] ) )
ctx := hctx . MakeContext ( )
config := hctx . GetConf ( ctx )
config . ColorScheme . BorderColor = args [ 0 ]
lib . CheckFatalError ( hctx . SetConfig ( config ) )
} ,
}
2024-08-11 20:37:48 +02:00
var toggleCompactMode = & cobra . Command {
Use : "toggle-compact-mode" ,
Short : "Toggle compact mode switching it on or off" ,
Args : cobra . ExactArgs ( 0 ) ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
ctx := hctx . MakeContext ( )
config := hctx . GetConf ( ctx )
config . ForceCompactMode = ! config . ForceCompactMode
lib . CheckFatalError ( hctx . SetConfig ( config ) )
} ,
}
2023-12-19 05:32:11 +01:00
func validateColor ( color string ) error {
if ! strings . HasPrefix ( color , "#" ) || len ( color ) != 7 {
return fmt . Errorf ( "color %q is invalid, it should be a hexadecimal color like #663399" , color )
}
return nil
}
2024-03-27 06:13:57 +01:00
var setAiCompletionEndpoint = & cobra . Command {
Use : "ai-completion-endpoint" ,
Short : "The AI endpoint to use for AI completions" ,
Args : cobra . ExactArgs ( 1 ) ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
ctx := hctx . MakeContext ( )
config := hctx . GetConf ( ctx )
config . AiCompletionEndpoint = args [ 0 ]
lib . CheckFatalError ( hctx . SetConfig ( config ) )
} ,
}
2022-11-14 03:26:23 +01:00
func init ( ) {
rootCmd . AddCommand ( configSetCmd )
configSetCmd . AddCommand ( setEnableControlRCmd )
configSetCmd . AddCommand ( setFilterDuplicateCommandsCmd )
configSetCmd . AddCommand ( setDisplayedColumnsCmd )
configSetCmd . AddCommand ( setTimestampFormatCmd )
2023-08-27 23:24:59 +02:00
configSetCmd . AddCommand ( setBetaModeCommand )
2023-10-12 03:18:56 +02:00
configSetCmd . AddCommand ( setHighlightMatchesCmd )
2023-11-12 12:09:56 +01:00
configSetCmd . AddCommand ( setEnableAiCompletionCmd )
2023-11-12 14:19:31 +01:00
configSetCmd . AddCommand ( setPresavingCmd )
2023-12-19 05:32:11 +01:00
configSetCmd . AddCommand ( setColorSchemeCmd )
2024-01-08 06:06:22 +01:00
configSetCmd . AddCommand ( setDefaultFilterCommand )
2024-03-27 06:13:57 +01:00
configSetCmd . AddCommand ( setAiCompletionEndpoint )
2024-08-11 20:37:48 +02:00
configSetCmd . AddCommand ( toggleCompactMode )
2023-12-19 05:32:11 +01:00
setColorSchemeCmd . AddCommand ( setColorSchemeSelectedText )
setColorSchemeCmd . AddCommand ( setColorSchemeSelectedBackground )
setColorSchemeCmd . AddCommand ( setColorSchemeBorderColor )
2022-11-14 03:26:23 +01:00
}