spogulis no
https://github.com/quitesimpleorg/hs9001.git
synced 2025-01-09 16:03:44 +01:00
78 rindas
2.0 KiB
Go
78 rindas
2.0 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"database/sql"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"log"
|
||
|
"os"
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
|
"github.com/tj/go-naturaldate"
|
||
|
)
|
||
|
|
||
|
type history struct {
|
||
|
conn *sql.DB
|
||
|
}
|
||
|
|
||
|
func (h *history) GetHistoryByPrefix(prefix string) (ph []string) {
|
||
|
beginTimestamp, err := naturaldate.Parse("50 years ago", time.Now())
|
||
|
if err != nil {
|
||
|
fmt.Fprintf(os.Stderr, "Failed to convert time string: %s\n", err.Error())
|
||
|
}
|
||
|
endTimeStamp, err := naturaldate.Parse("now", time.Now())
|
||
|
if err != nil {
|
||
|
fmt.Fprintf(os.Stderr, "Failed to convert time string: %s\n", err.Error())
|
||
|
}
|
||
|
results := search(h.conn, prefix+"%", "%", beginTimestamp, endTimeStamp, -9001)
|
||
|
for e := results.Front(); e != nil; e = e.Next() {
|
||
|
entry, ok := e.Value.(*HistoryEntry)
|
||
|
if !ok {
|
||
|
log.Panic("Failed to retrieve entries")
|
||
|
}
|
||
|
ph = append(ph, entry.cmd)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
func (h *history) GetHistoryByPattern(pattern string) (ph []string, pos []int) {
|
||
|
beginTimestamp, err := naturaldate.Parse("50 years ago", time.Now())
|
||
|
if err != nil {
|
||
|
fmt.Fprintf(os.Stderr, "Failed to convert time string: %s\n", err.Error())
|
||
|
}
|
||
|
endTimeStamp, err := naturaldate.Parse("now", time.Now())
|
||
|
if err != nil {
|
||
|
fmt.Fprintf(os.Stderr, "Failed to convert time string: %s\n", err.Error())
|
||
|
}
|
||
|
results := search(h.conn, "%"+pattern+"%", "%", beginTimestamp, endTimeStamp, -9001)
|
||
|
for e := results.Front(); e != nil; e = e.Next() {
|
||
|
entry, ok := e.Value.(*HistoryEntry)
|
||
|
if !ok {
|
||
|
log.Panic("Failed to retrieve entries")
|
||
|
}
|
||
|
//mt.Printf("\nAppending: [%s] %s -- %d\n", pattern, entry.cmd, strings.Index(entry.cmd, pattern))
|
||
|
ph = append(ph, entry.cmd)
|
||
|
pos = append(pos, strings.Index(strings.ToLower(entry.cmd), strings.ToLower(pattern)))
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (h *history) ReadHistory(r io.Reader) (num int, err error) {
|
||
|
panic("not implemented")
|
||
|
}
|
||
|
func (h *history) WriteHistory(w io.Writer) (num int, err error) {
|
||
|
panic("not implemented")
|
||
|
}
|
||
|
func (h *history) AppendHistory(item string) {
|
||
|
panic("not implemented")
|
||
|
}
|
||
|
func (h *history) ClearHistory() {
|
||
|
panic("not implemented")
|
||
|
}
|
||
|
func (h *history) RLock() {
|
||
|
//noop
|
||
|
}
|
||
|
func (h *history) RUnlock() {
|
||
|
//noop
|
||
|
}
|