Repository navigation
Expand file tree
/
Copy pathrows.go
More file actions
146 lines (135 loc) · 4.38 KB
/
Copy pathrows.go
File metadata and controls
146 lines (135 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
// Reading a history that is kept in a database rather than in a file.
//
// Four of the records in the list are SQLite, and until there was a reader
// for it they were handled by pulling the printable stretches out of the
// bytes. That is not nothing and it is not reading. It cannot tell a
// command from a column name or a table name, it throws away the times,
// and it hands back rows that were deleted and are still lying in the free
// space as though they were live ones.
//
// With the format read properly, a row is a row: this is the command, this
// is when it was run, and a row that is not there any more is not there.
import (
"fmt"
"strings"
"time"
)
// Which column holds the command, and which holds the time, differ between
// the four of them and nothing in the file says which is which. So they are
// named. A name not on either list is skipped rather than guessed at.
var (
commandColumns = []string{
"command", "command_line", "commandline", "cmd", "source", "source_raw",
"statement", "query", "argv", "line", "text", "input", "entry",
}
timeColumns = []string{
"timestamp", "start_timestamp", "started", "start_time", "when",
"when_", "time", "created", "created_at", "date", "run_at", "atime",
}
)
// readBaseHistory reads the commands out of a database.
//
// It returns false when the file is not a database at all, so that the
// caller can fall back to the old way rather than reporting nothing for a
// record whose format changed under it.
func readBaseHistory(path string) ([]Entry, bool, error) {
base, err := OpenBase(path)
if err != nil {
return nil, false, nil // not a database; the caller will pull runs out
}
var out []Entry
for table, _ := range base.Tables() {
columns := base.Columns(table)
text := pick(columns, commandColumns)
if text < 0 {
continue
}
when := pick(columns, timeColumns)
for _, row := range base.Rows(table) {
if text >= len(row.Values) {
continue
}
line := strings.TrimSpace(row.Values[text].Text)
if line == "" {
continue
}
entry := Entry{Text: line, Line: int(row.Row), Kind: kindCommand}
if when >= 0 && when < len(row.Values) {
entry.When = stamp(row.Values[when])
}
out = append(out, entry)
if len(out) >= mostEntries {
break
}
}
}
var err2 error
if base.Ahead {
// The rows written since the last checkpoint are in the log beside
// the file, and this reads the file. Saying so beats handing back
// a history that stops before the interesting part.
err2 = fmt.Errorf("there is a write ahead log beside this, so anything " +
"added since it was last checkpointed is in that and was not read")
}
for _, t := range base.Trouble {
err2 = fmt.Errorf("%s", t)
}
return out, true, err2
}
// pick finds the first column whose name is one of the wanted ones.
func pick(columns []string, wanted []string) int {
for i, name := range columns {
lower := strings.ToLower(strings.TrimSpace(name))
for _, want := range wanted {
if lower == want {
return i
}
}
}
return -1
}
// stamp turns whatever a database put in a time column into a time.
//
// There is no one way. Atuin writes nanoseconds since the epoch, the AWS
// command line writes milliseconds, IPython writes text, and Windows writes
// its own count of ticks since 1601. Guessing by size is what everything
// that reads these files does, because the column gives no other clue.
func stamp(f Field) time.Time {
switch f.Kind {
case 'i':
return fromNumber(float64(f.Int))
case 'f':
return fromNumber(f.Float)
case 't':
for _, layout := range []string{
time.RFC3339Nano, time.RFC3339, "2006-01-02 15:04:05.999999",
"2006-01-02 15:04:05", "2006-01-02T15:04:05.999999",
} {
if when, err := time.Parse(layout, strings.TrimSpace(f.Text)); err == nil {
return when.UTC()
}
}
}
return time.Time{}
}
// The boundaries between the units, each one the point past which a count
// in the smaller unit would be a date nobody has reached.
const (
secondsUntil2100 = 4_102_444_800
millisUntil2100 = secondsUntil2100 * 1e3
microsUntil2100 = secondsUntil2100 * 1e6
)
func fromNumber(v float64) time.Time {
switch {
case v <= 0:
return time.Time{}
case v < secondsUntil2100:
return time.Unix(int64(v), 0).UTC()
case v < millisUntil2100:
return time.UnixMilli(int64(v)).UTC()
case v < microsUntil2100:
return time.UnixMicro(int64(v)).UTC()
}
return time.Unix(0, int64(v)).UTC()
}