hack in support to use standard history in resh cli

only use standard history when resh history is too short
pull/137/head
Simon Let 6 years ago
parent 2e0737fab8
commit d16d59759b
  1. 57
      cmd/cli/item.go
  2. 7
      pkg/histcli/histcli.go
  3. 10
      pkg/histfile/histfile.go
  4. 10
      pkg/records/records.go

@ -14,6 +14,8 @@ import (
const itemLocationLenght = 30
type item struct {
isRaw bool
realtimeBefore float64
// [host:]pwd
@ -65,7 +67,23 @@ func (i item) less(i2 item) bool {
// reversed order
return i.score > i2.score
}
func (i item) drawItemColumns(compactRendering bool) itemColumns {
if i.isRaw {
notAvailable := "n/a"
return itemColumns{
date: notAvailable + " ",
dateWithColor: notAvailable + " ",
// dateWithColor: highlightDate(notAvailable) + " ",
host: "",
hostWithColor: "",
pwdTilde: notAvailable,
cmdLine: i.cmdLine,
cmdLineWithColor: i.cmdLineWithColor,
// score: i.score,
key: i.key,
}
}
// DISPLAY
// DISPLAY > date
@ -80,7 +98,6 @@ func (i item) drawItemColumns(compactRendering bool) itemColumns {
date = formatTimeRelativeLong(tm) + " "
}
dateWithColor := highlightDate(date)
// DISPLAY > location
// DISPLAY > location > host
host := ""
@ -232,6 +249,31 @@ func newItemFromRecordForQuery(record records.CliRecord, query query, debug bool
// NO continue
}
}
// DISPLAY > cmdline
// cmd := "<" + strings.ReplaceAll(record.CmdLine, "\n", ";") + ">"
cmdLine := strings.ReplaceAll(record.CmdLine, "\n", ";")
cmdLineWithColor := strings.ReplaceAll(cmd, "\n", ";")
// KEY for deduplication
key := record.CmdLine
// NOTE: since we import standard history we need a compatible key without metadata
/*
unlikelySeparator := "|||||"
key := record.CmdLine + unlikelySeparator + record.Pwd + unlikelySeparator +
record.GitOriginRemote + unlikelySeparator + record.Host
*/
if record.IsRaw {
return item{
isRaw: true,
cmdLine: cmdLine,
cmdLineWithColor: cmdLineWithColor,
score: score,
key: key,
}, nil
}
// actual pwd matches
// N terms can only produce:
// -> N matches against the command
@ -265,19 +307,6 @@ func newItemFromRecordForQuery(record records.CliRecord, query query, debug bool
return item{}, errors.New("no match for given record and query")
}
// KEY for deduplication
unlikelySeparator := "|||||"
key := record.CmdLine + unlikelySeparator + record.Pwd + unlikelySeparator +
record.GitOriginRemote + unlikelySeparator + record.Host
// + strconv.Itoa(record.ExitCode) + unlikelySeparator
// DISPLAY > cmdline
// cmd := "<" + strings.ReplaceAll(record.CmdLine, "\n", ";") + ">"
cmdLine := strings.ReplaceAll(record.CmdLine, "\n", ";")
cmdLineWithColor := strings.ReplaceAll(cmd, "\n", ";")
it := item{
realtimeBefore: record.RealtimeBefore,

@ -22,3 +22,10 @@ func (h *Histcli) AddRecord(record records.Record) {
h.List = append(h.List, cli)
}
// AddCmdLine to the histcli
func (h *Histcli) AddCmdLine(cmdline string) {
cli := records.NewCliRecordFromCmdLine(cmdline)
h.List = append(h.List, cli)
}

@ -50,7 +50,13 @@ func New(input chan records.Record, sessionsToDrop chan string,
}
// load records from resh history, reverse, enrich and save
func (h *Histfile) loadFullRecords(recs []records.Record) {
func (h *Histfile) loadCliRecords(recs []records.Record) {
for _, cmdline := range h.bashCmdLines.List {
h.cliRecords.AddCmdLine(cmdline)
}
for _, cmdline := range h.zshCmdLines.List {
h.cliRecords.AddCmdLine(cmdline)
}
for i := len(recs) - 1; i >= 0; i-- {
rec := recs[i]
h.cliRecords.AddRecord(rec)
@ -82,7 +88,7 @@ func (h *Histfile) loadHistory(bashHistoryPath, zshHistoryPath string, maxInitHi
}
log.Println("histfile: Loading resh history from file ...")
history := records.LoadFromFile(h.historyPath, math.MaxInt32)
go h.loadFullRecords(history)
go h.loadCliRecords(history)
// NOTE: keeping this weird interface for now because we might use it in the future
// when we only load bash or zsh history
reshCmdLines := loadCmdLines(history)

@ -150,6 +150,7 @@ type SlimRecord struct {
// CliRecord used for sending records to RESH-CLI
type CliRecord struct {
IsRaw bool `json:"isRaw"`
SessionID string `json:"sessionId"`
CmdLine string `json:"cmdLine"`
@ -164,9 +165,18 @@ type CliRecord struct {
// RealtimeDuration float64 `json:"realtimeDuration"`
}
// NewCliRecordFromCmdLine from EnrichedRecord
func NewCliRecordFromCmdLine(cmdLine string) CliRecord {
return CliRecord{
IsRaw: true,
CmdLine: cmdLine,
}
}
// NewCliRecord from EnrichedRecord
func NewCliRecord(r EnrichedRecord) CliRecord {
return CliRecord{
IsRaw: false,
SessionID: r.SessionID,
CmdLine: r.CmdLine,
Host: r.Host,

Loading…
Cancel
Save