Add basic support for stripping out HISTTIMEFORMAT prefixes

This commit is contained in:
David Dworken
2022-06-12 21:28:19 -07:00
parent 3f32891469
commit e8f001c78b
4 changed files with 119 additions and 1 deletions

View File

@ -121,7 +121,7 @@ func BuildHistoryEntry(args []string) (*data.HistoryEntry, error) {
// Don't save commands that start with a space
return nil, nil
}
entry.Command = cmd
entry.Command = maybeSkipBashHistTimePrefix(cmd)
} else if shell == "zsh" {
cmd := strings.TrimSuffix(strings.TrimSuffix(args[4], "\n"), " ")
if strings.HasPrefix(cmd, " ") {
@ -150,6 +150,23 @@ func BuildHistoryEntry(args []string) (*data.HistoryEntry, error) {
return &entry, nil
}
func maybeSkipBashHistTimePrefix(cmdLine string) string {
format := os.Getenv("HISTTIMEFORMAT")
if format == "" {
return cmdLine
}
if !strings.HasSuffix(format, " ") {
GetLogger().Printf("bash has HISTTIMEFORMAT set, but it doesn't end in a space so we can't strip the timestamp")
return cmdLine
}
// TODO: could we handle things like `export HISTTIMEFORMAT='%c:'`?
numSpaces := strings.Count(format, " ")
numC := strings.Count(format, "%c")
numSpaces += (4 * numC)
split := strings.SplitN(cmdLine, " ", numSpaces+1)
return split[len(split)-1]
}
func parseCrossPlatformInt(data string) (int64, error) {
data = strings.TrimSuffix(data, "N")
return strconv.ParseInt(data, 10, 64)