mirror of
https://github.com/ddworken/hishtory.git
synced 2025-02-02 11:39:24 +01:00
Add code to build the custom columns from whatever is configured
This commit is contained in:
parent
12163483dd
commit
07e4c781e4
@ -38,7 +38,7 @@ type HistoryEntry struct {
|
||||
CustomColumns CustomColumns `json:"custom_columns"`
|
||||
}
|
||||
|
||||
type CustomColumns []CustomColumns
|
||||
type CustomColumns []CustomColumn
|
||||
|
||||
type CustomColumn struct {
|
||||
Name string `json:"name"`
|
||||
|
@ -152,9 +152,42 @@ func BuildHistoryEntry(ctx *context.Context, args []string) (*data.HistoryEntry,
|
||||
config := hctx.GetConf(ctx)
|
||||
entry.DeviceId = config.DeviceId
|
||||
|
||||
// custom columns
|
||||
cc, err := buildCustomColumns(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entry.CustomColumns = cc
|
||||
|
||||
return &entry, nil
|
||||
}
|
||||
|
||||
func buildCustomColumns(ctx *context.Context) (data.CustomColumns, error) {
|
||||
ccs := data.CustomColumns{}
|
||||
config := hctx.GetConf(ctx)
|
||||
for _, cc := range config.CustomColumns {
|
||||
cmd := exec.Command("bash", "-c", cc.ColumnCommand)
|
||||
var stdout bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stderr = &stderr
|
||||
err := cmd.Start()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute custom command named %v (stdout=%#v, stderr=%#v)", cc.ColumnName, stdout.String(), stderr.String())
|
||||
}
|
||||
err = cmd.Wait()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute custom command named %v (stdout=%#v, stderr=%#v)", cc.ColumnName, stdout.String(), stderr.String())
|
||||
}
|
||||
ccv := data.CustomColumn{
|
||||
Name: cc.ColumnName,
|
||||
Val: strings.TrimSpace(stdout.String()),
|
||||
}
|
||||
ccs = append(ccs, ccv)
|
||||
}
|
||||
return ccs, nil
|
||||
}
|
||||
|
||||
func isZshWeirdness(cmd string) bool {
|
||||
// Zsh has this weird behavior where the currently running command is persisted to
|
||||
// the history file with a weird prefix. This only matters to us when running
|
||||
|
Loading…
Reference in New Issue
Block a user