mirror of
https://github.com/ddworken/hishtory.git
synced 2025-08-18 18:58:54 +02:00
Add custom columns to config + use them in the non-TUI query
This commit is contained in:
@@ -300,6 +300,7 @@ func testBasicUserFlow(t *testing.T, tester shellTester) string {
|
|||||||
|
|
||||||
// Test the banner
|
// Test the banner
|
||||||
os.Setenv("FORCED_BANNER", "HELLO_FROM_SERVER")
|
os.Setenv("FORCED_BANNER", "HELLO_FROM_SERVER")
|
||||||
|
defer os.Setenv("FORCED_BANNER", "")
|
||||||
out = hishtoryQuery(t, tester, "")
|
out = hishtoryQuery(t, tester, "")
|
||||||
if !strings.Contains(out, "HELLO_FROM_SERVER\nHostname") {
|
if !strings.Contains(out, "HELLO_FROM_SERVER\nHostname") {
|
||||||
t.Fatalf("hishtory query didn't show the banner message! out=%#v", out)
|
t.Fatalf("hishtory query didn't show the banner message! out=%#v", out)
|
||||||
|
@@ -158,6 +158,8 @@ type ClientConfig struct {
|
|||||||
HaveCompletedInitialImport bool `json:"have_completed_initial_import"`
|
HaveCompletedInitialImport bool `json:"have_completed_initial_import"`
|
||||||
// Whether control-r bindings are enabled
|
// Whether control-r bindings are enabled
|
||||||
ControlRSearchEnabled bool `json:"enable_control_r_search"`
|
ControlRSearchEnabled bool `json:"enable_control_r_search"`
|
||||||
|
// The set of columns that the user wants to be displayed
|
||||||
|
DisplayedColumns []string `json:"displayed_columns"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetConfigContents() ([]byte, error) {
|
func GetConfigContents() ([]byte, error) {
|
||||||
@@ -191,6 +193,9 @@ func GetConfig() (ClientConfig, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return ClientConfig{}, fmt.Errorf("failed to parse config file: %v", err)
|
return ClientConfig{}, fmt.Errorf("failed to parse config file: %v", err)
|
||||||
}
|
}
|
||||||
|
if config.DisplayedColumns == nil || len(config.DisplayedColumns) == 0 {
|
||||||
|
config.DisplayedColumns = []string{"Hostname", "CWD", "Timestamp", "Runtime", "Exit Code", "Command"}
|
||||||
|
}
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -364,18 +364,42 @@ func AddToDbIfNew(db *gorm.DB, entry data.HistoryEntry) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func DisplayResults(results []*data.HistoryEntry) {
|
func DisplayResults(ctx *context.Context, results []*data.HistoryEntry) error {
|
||||||
|
config := hctx.GetConf(ctx)
|
||||||
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
|
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
|
||||||
tbl := table.New("Hostname", "CWD", "Timestamp", "Runtime", "Exit Code", "Command")
|
|
||||||
|
columns := make([]any, 0)
|
||||||
|
for _, c := range config.DisplayedColumns {
|
||||||
|
columns = append(columns, c)
|
||||||
|
}
|
||||||
|
tbl := table.New(columns...)
|
||||||
tbl.WithHeaderFormatter(headerFmt)
|
tbl.WithHeaderFormatter(headerFmt)
|
||||||
|
|
||||||
for _, result := range results {
|
for _, result := range results {
|
||||||
timestamp := result.StartTime.Format("Jan 2 2006 15:04:05 MST")
|
row := make([]any, 0)
|
||||||
duration := result.EndTime.Sub(result.StartTime).Round(time.Millisecond).String()
|
for _, header := range config.DisplayedColumns {
|
||||||
tbl.AddRow(result.Hostname, result.CurrentWorkingDirectory, timestamp, duration, result.ExitCode, result.Command)
|
switch header {
|
||||||
|
case "Hostname":
|
||||||
|
row = append(row, result.Hostname)
|
||||||
|
case "CWD":
|
||||||
|
row = append(row, result.CurrentWorkingDirectory)
|
||||||
|
case "Timestamp":
|
||||||
|
row = append(row, result.StartTime.Format("Jan 2 2006 15:04:05 MST"))
|
||||||
|
case "Runtime":
|
||||||
|
row = append(row, result.EndTime.Sub(result.StartTime).Round(time.Millisecond).String())
|
||||||
|
case "Exit Code":
|
||||||
|
row = append(row, fmt.Sprintf("%d", result.ExitCode))
|
||||||
|
case "Command":
|
||||||
|
row = append(row, result.Command)
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("failed to map column %#v to a value", header)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tbl.AddRow(row...)
|
||||||
}
|
}
|
||||||
|
|
||||||
tbl.Print()
|
tbl.Print()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsEnabled(ctx *context.Context) (bool, error) {
|
func IsEnabled(ctx *context.Context) (bool, error) {
|
||||||
|
@@ -214,7 +214,7 @@ func query(ctx *context.Context, query string) {
|
|||||||
lib.CheckFatalError(displayBannerIfSet(ctx))
|
lib.CheckFatalError(displayBannerIfSet(ctx))
|
||||||
data, err := data.Search(db, query, 25)
|
data, err := data.Search(db, query, 25)
|
||||||
lib.CheckFatalError(err)
|
lib.CheckFatalError(err)
|
||||||
lib.DisplayResults(data)
|
lib.CheckFatalError(lib.DisplayResults(ctx, data))
|
||||||
}
|
}
|
||||||
|
|
||||||
func displayBannerIfSet(ctx *context.Context) error {
|
func displayBannerIfSet(ctx *context.Context) error {
|
||||||
|
Reference in New Issue
Block a user