Last active
September 30, 2018 00:14
-
-
Save f440/3938bbbee1f7ddf26ff08717f826351c to your computer and use it in GitHub Desktop.
zsh's extended history file parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"log" | |
"os" | |
"strconv" | |
"strings" | |
"time" | |
) | |
type History struct { | |
At time.Time | |
Elapsed int | |
Command string | |
} | |
func main() { | |
s := bufio.NewScanner(os.Stdin) | |
var histories []History | |
var command string | |
for s.Scan() { | |
line := s.Text() | |
runedCommand := []rune(command) | |
if []rune(runedCommand)[len(runedCommand)-1] == '\\' { | |
command = strings.Join([]string{command, line}, "\n") | |
continue | |
} else { | |
if command != "" { | |
history := parseOne(command) | |
histories = append(histories, history) | |
} | |
command = line | |
} | |
} | |
if command != "" { | |
history := parseOne(command) | |
histories = append(histories, history) | |
} | |
fmt.Println(histories) | |
} | |
func parseOne(row string) History { | |
// `: <beginning time>:<elapsed seconds>;<command>'. | |
t := strings.Split(row, ";")[0:2] | |
info, command := t[0], t[1] | |
t = strings.Split(info, ":") | |
atInt, err := strconv.Atoi(strings.TrimLeft(t[1], " ")) | |
if err != nil { | |
log.Fatal(err) | |
} | |
at := time.Unix(int64(atInt), 0) | |
elapsed, err := strconv.Atoi(t[2]) | |
if err != nil { | |
log.Fatal(err) | |
} | |
return History{ | |
At: at, | |
Elapsed: elapsed, | |
Command: command, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment