Skip to content

Commit

Permalink
hack in support to use standard history in resh cli
Browse files Browse the repository at this point in the history
only use standard history when resh history is too short
  • Loading branch information
curusarn committed May 6, 2020
1 parent 2e0737f commit d16d597
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 16 deletions.
57 changes: 43 additions & 14 deletions cmd/cli/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
const itemLocationLenght = 30

type item struct {
isRaw bool

realtimeBefore float64

// [host:]pwd
Expand Down Expand Up @@ -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
Expand All @@ -80,7 +98,6 @@ func (i item) drawItemColumns(compactRendering bool) itemColumns {
date = formatTimeRelativeLong(tm) + " "
}
dateWithColor := highlightDate(date)

// DISPLAY > location
// DISPLAY > location > host
host := ""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,

Expand Down
7 changes: 7 additions & 0 deletions pkg/histcli/histcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
10 changes: 8 additions & 2 deletions pkg/histfile/histfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions pkg/records/records.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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,
Expand Down

0 comments on commit d16d597

Please sign in to comment.