mirror of
https://github.com/ddworken/hishtory.git
synced 2024-12-23 23:39:02 +01:00
Add custom columns to config + use them in the non-TUI query
This commit is contained in:
parent
ffa256e9cc
commit
51b41aa171
@ -300,6 +300,7 @@ func testBasicUserFlow(t *testing.T, tester shellTester) string {
|
||||
|
||||
// Test the banner
|
||||
os.Setenv("FORCED_BANNER", "HELLO_FROM_SERVER")
|
||||
defer os.Setenv("FORCED_BANNER", "")
|
||||
out = hishtoryQuery(t, tester, "")
|
||||
if !strings.Contains(out, "HELLO_FROM_SERVER\nHostname") {
|
||||
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"`
|
||||
// Whether control-r bindings are enabled
|
||||
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) {
|
||||
@ -191,6 +193,9 @@ func GetConfig() (ClientConfig, error) {
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
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)
|
||||
|
||||
for _, result := range results {
|
||||
timestamp := result.StartTime.Format("Jan 2 2006 15:04:05 MST")
|
||||
duration := result.EndTime.Sub(result.StartTime).Round(time.Millisecond).String()
|
||||
tbl.AddRow(result.Hostname, result.CurrentWorkingDirectory, timestamp, duration, result.ExitCode, result.Command)
|
||||
row := make([]any, 0)
|
||||
for _, header := range config.DisplayedColumns {
|
||||
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()
|
||||
return nil
|
||||
}
|
||||
|
||||
func IsEnabled(ctx *context.Context) (bool, error) {
|
||||
|
@ -214,7 +214,7 @@ func query(ctx *context.Context, query string) {
|
||||
lib.CheckFatalError(displayBannerIfSet(ctx))
|
||||
data, err := data.Search(db, query, 25)
|
||||
lib.CheckFatalError(err)
|
||||
lib.DisplayResults(data)
|
||||
lib.CheckFatalError(lib.DisplayResults(ctx, data))
|
||||
}
|
||||
|
||||
func displayBannerIfSet(ctx *context.Context) error {
|
||||
|
Loading…
Reference in New Issue
Block a user