-
Notifications
You must be signed in to change notification settings - Fork 1
/
history.go
65 lines (59 loc) · 1.23 KB
/
history.go
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
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"os"
"path/filepath"
"strconv"
"strings"
)
var (
DBPath = "/Library/Application Support/Orion/Defaults/history"
QUERY = `
SELECT history_items.id, title, url
FROM history_items
INNER JOIN visits
ON visits.history_item_id = history_items.id
WHERE url LIKE ? OR title LIKE ?
GROUP BY url
ORDER BY count(visits.visit_time) DESC
`
)
func searchHistory() error {
showUpdateStatus()
home, err := os.UserHomeDir()
if err != nil {
return err
}
dbPath := filepath.Join(home, DBPath)
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
return err
}
defer db.Close()
query := fmt.Sprintf("%%%s%%", strings.Join(strings.Split(os.Args[3], " "), "%"))
rows, err := db.Query(QUERY, query, query)
if err != nil {
return err
}
for rows.Next() {
var id int
var title, url sql.NullString
err := rows.Scan(&id, &title, &url)
if err != nil {
return err
}
if !title.Valid || len(title.String) == 0 {
title.String = url.String
}
wf.NewItem(title.String).
Valid(true).
UID(strconv.Itoa(id)).
Subtitle(url.String).
Arg(url.String)
}
wf.WarnEmpty("No matching history found", "Try another search?")
wf.SendFeedback()
return nil
}