Skip to content

Instantly share code, notes, and snippets.

@fullmated
Last active January 5, 2020 17:12
Show Gist options
  • Save fullmated/0e456686f23558f6cb3b9f767cfcdb67 to your computer and use it in GitHub Desktop.
Save fullmated/0e456686f23558f6cb3b9f767cfcdb67 to your computer and use it in GitHub Desktop.
historyコマンドの標準出力結果から.zsh_historyに変換する(stdoutに出力)
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
"time"
)
var sc = bufio.NewScanner(os.Stdin)
func main() {
stdin := bufio.NewScanner(os.Stdin)
for stdin.Scan() {
text := stdin.Text()
rep := regexp.MustCompile(`\s+`)
result := rep.Split(text, -1)
hyphen := regexp.MustCompile(`-`)
timeSplited := hyphen.Split(result[2], -1)
colon := regexp.MustCompile(`:`)
hourMinuteSplited := colon.Split(result[3], -1)
var command string
for i := 4; i < len(result); i++ {
command += result[i]
if i == len(result)-1 {
break
}
command += " "
}
year, _ := strconv.Atoi(timeSplited[0])
month, _ := strconv.Atoi(timeSplited[1])
day, _ := strconv.Atoi(timeSplited[2])
hour, _ := strconv.Atoi(hourMinuteSplited[0])
min, _ := strconv.Atoi(hourMinuteSplited[1])
t := time.Date(year, time.Month(month), day, hour, min, 0, 0, time.Local)
unixTimeStamp := t.Unix()
fmt.Printf(": %d:0;%s\n", unixTimeStamp, command)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment