diff --git a/cmd/cli/item.go b/cmd/cli/item.go index 54e9fa3..1da6188 100644 --- a/cmd/cli/item.go +++ b/cmd/cli/item.go @@ -93,7 +93,7 @@ func properMatch(str, term, padChar string) bool { // newItemFromRecordForQuery creates new item from record based on given query // returns error if the query doesn't match the record -func newItemFromRecordForQuery(record records.EnrichedRecord, query query, debug bool) (item, error) { +func newItemFromRecordForQuery(record records.CliRecord, query query, debug bool) (item, error) { const hitScore = 1.0 const hitScoreConsecutive = 0.1 const properMatchScore = 0.3 @@ -237,7 +237,7 @@ type rawItem struct { // newRawItemFromRecordForQuery creates new item from record based on given query // returns error if the query doesn't match the record -func newRawItemFromRecordForQuery(record records.EnrichedRecord, terms []string, debug bool) (rawItem, error) { +func newRawItemFromRecordForQuery(record records.CliRecord, terms []string, debug bool) (rawItem, error) { const hitScore = 1.0 const hitScoreConsecutive = 0.1 const properMatchScore = 0.3 diff --git a/cmd/cli/main.go b/cmd/cli/main.go index f61b5ad..7fde661 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -96,15 +96,15 @@ func runReshCli() (string, int) { // g.SelBgColor = gocui.ColorGreen g.Highlight = true - mess := msg.DumpMsg{ + mess := msg.CliMsg{ SessionID: *sessionID, PWD: *pwd, } - resp := SendDumpMsg(mess, strconv.Itoa(config.Port)) + resp := SendCliMsg(mess, strconv.Itoa(config.Port)) st := state{ // lock sync.Mutex - fullRecords: resp.FullRecords, + cliRecords: resp.CliRecords, initialQuery: *query, } @@ -157,7 +157,7 @@ func runReshCli() (string, int) { type state struct { lock sync.Mutex - fullRecords []records.EnrichedRecord + cliRecords []records.CliRecord data []item rawData []rawItem highlightedItem int @@ -210,7 +210,7 @@ type dedupRecord struct { func (m manager) UpdateData(input string) { if debug { log.Println("EDIT start") - log.Println("len(fullRecords) =", len(m.s.fullRecords)) + log.Println("len(fullRecords) =", len(m.s.cliRecords)) log.Println("len(data) =", len(m.s.data)) } query := newQueryFromString(input, m.host, m.pwd, m.gitOriginRemote) @@ -218,7 +218,7 @@ func (m manager) UpdateData(input string) { itemSet := make(map[string]int) m.s.lock.Lock() defer m.s.lock.Unlock() - for _, rec := range m.s.fullRecords { + for _, rec := range m.s.cliRecords { itm, err := newItemFromRecordForQuery(rec, query, m.config.Debug) if err != nil { // records didn't match the query @@ -254,7 +254,7 @@ func (m manager) UpdateData(input string) { } m.s.highlightedItem = 0 if debug { - log.Println("len(fullRecords) =", len(m.s.fullRecords)) + log.Println("len(fullRecords) =", len(m.s.cliRecords)) log.Println("len(data) =", len(m.s.data)) log.Println("EDIT end") } @@ -263,7 +263,7 @@ func (m manager) UpdateData(input string) { func (m manager) UpdateRawData(input string) { if debug { log.Println("EDIT start") - log.Println("len(fullRecords) =", len(m.s.fullRecords)) + log.Println("len(fullRecords) =", len(m.s.cliRecords)) log.Println("len(data) =", len(m.s.data)) } query := getRawTermsFromString(input) @@ -271,7 +271,7 @@ func (m manager) UpdateRawData(input string) { itemSet := make(map[string]bool) m.s.lock.Lock() defer m.s.lock.Unlock() - for _, rec := range m.s.fullRecords { + for _, rec := range m.s.cliRecords { itm, err := newRawItemFromRecordForQuery(rec, query, m.config.Debug) if err != nil { // records didn't match the query @@ -301,7 +301,7 @@ func (m manager) UpdateRawData(input string) { } m.s.highlightedItem = 0 if debug { - log.Println("len(fullRecords) =", len(m.s.fullRecords)) + log.Println("len(fullRecords) =", len(m.s.cliRecords)) log.Println("len(data) =", len(m.s.data)) log.Println("EDIT end") } @@ -393,8 +393,8 @@ func quit(g *gocui.Gui, v *gocui.View) error { return gocui.ErrQuit } -// SendDumpMsg to daemon -func SendDumpMsg(m msg.DumpMsg, port string) msg.DumpResponse { +// SendCliMsg to daemon +func SendCliMsg(m msg.CliMsg, port string) msg.CliResponse { recJSON, err := json.Marshal(m) if err != nil { log.Fatal("send err 1", err) @@ -419,7 +419,7 @@ func SendDumpMsg(m msg.DumpMsg, port string) msg.DumpResponse { log.Fatal("read response error") } // log.Println(string(body)) - response := msg.DumpResponse{} + response := msg.CliResponse{} err = json.Unmarshal(body, &response) if err != nil { log.Fatal("unmarshal resp error: ", err) diff --git a/cmd/daemon/dump.go b/cmd/daemon/dump.go index d8cf5a6..375da76 100644 --- a/cmd/daemon/dump.go +++ b/cmd/daemon/dump.go @@ -25,7 +25,7 @@ func (h *dumpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - mess := msg.DumpMsg{} + mess := msg.CliMsg{} if Debug { log.Println("/dump unmarshaling record ...") } @@ -38,12 +38,12 @@ func (h *dumpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if Debug { log.Println("/dump dumping ...") } - fullRecords := h.histfileBox.DumpRecords() + fullRecords := h.histfileBox.DumpCliRecords() if err != nil { log.Println("Dump error:", err) } - resp := msg.DumpResponse{FullRecords: fullRecords.List} + resp := msg.CliResponse{CliRecords: fullRecords.List} jsn, err = json.Marshal(&resp) if err != nil { log.Println("Encoding error:", err) diff --git a/pkg/histcli/histcli.go b/pkg/histcli/histcli.go index 105cbeb..2abb799 100644 --- a/pkg/histcli/histcli.go +++ b/pkg/histcli/histcli.go @@ -7,7 +7,7 @@ import ( // Histcli is a dump of history preprocessed for resh cli purposes type Histcli struct { // list of records - List []records.EnrichedRecord + List []records.CliRecord } // New Histcli @@ -18,6 +18,7 @@ func New() Histcli { // AddRecord to the histcli func (h *Histcli) AddRecord(record records.Record) { enriched := records.Enriched(record) + cli := records.NewCliRecord(enriched) - h.List = append(h.List, enriched) + h.List = append(h.List, cli) } diff --git a/pkg/histfile/histfile.go b/pkg/histfile/histfile.go index c814166..a72da7d 100644 --- a/pkg/histfile/histfile.go +++ b/pkg/histfile/histfile.go @@ -27,7 +27,7 @@ type Histfile struct { bashCmdLines histlist.Histlist zshCmdLines histlist.Histlist - fullRecords histcli.Histcli + cliRecords histcli.Histcli } // New creates new histfile and runs its gorutines @@ -41,7 +41,7 @@ func New(input chan records.Record, sessionsToDrop chan string, historyPath: reshHistoryPath, bashCmdLines: histlist.New(), zshCmdLines: histlist.New(), - fullRecords: histcli.New(), + cliRecords: histcli.New(), } go hf.loadHistory(bashHistoryPath, zshHistoryPath, maxInitHistSize, minInitHistSizeKB) go hf.writer(input, signals, shutdownDone) @@ -53,7 +53,7 @@ func New(input chan records.Record, sessionsToDrop chan string, func (h *Histfile) loadFullRecords(recs []records.Record) { for i := len(recs) - 1; i >= 0; i-- { rec := recs[i] - h.fullRecords.AddRecord(rec) + h.cliRecords.AddRecord(rec) } } @@ -178,7 +178,7 @@ func (h *Histfile) mergeAndWriteRecord(part1, part2 records.Record) { cmdLine := part1.CmdLine h.bashCmdLines.AddCmdLine(cmdLine) h.zshCmdLines.AddCmdLine(cmdLine) - h.fullRecords.AddRecord(part1) + h.cliRecords.AddRecord(part1) }() writeRecord(part1, h.historyPath) @@ -224,10 +224,10 @@ func (h *Histfile) GetRecentCmdLines(shell string, limit int) histlist.Histlist return hl } -// DumpRecords returns enriched records -func (h *Histfile) DumpRecords() histcli.Histcli { +// DumpCliRecords returns enriched records +func (h *Histfile) DumpCliRecords() histcli.Histcli { // don't forget locks in the future - return h.fullRecords + return h.cliRecords } func loadCmdLines(recs []records.Record) histlist.Histlist { diff --git a/pkg/msg/msg.go b/pkg/msg/msg.go index b0a8d29..3c85987 100644 --- a/pkg/msg/msg.go +++ b/pkg/msg/msg.go @@ -2,15 +2,15 @@ package msg import "github.com/curusarn/resh/pkg/records" -// DumpMsg struct -type DumpMsg struct { +// CliMsg struct +type CliMsg struct { SessionID string `json:"sessionID"` PWD string `json:"pwd"` } -// DumpResponse struct -type DumpResponse struct { - FullRecords []records.EnrichedRecord `json:"fullRecords"` +// CliResponse struct +type CliResponse struct { + CliRecords []records.CliRecord `json:"cliRecords"` } // InspectMsg struct diff --git a/pkg/records/records.go b/pkg/records/records.go index 0d0be91..b8d9d67 100644 --- a/pkg/records/records.go +++ b/pkg/records/records.go @@ -147,6 +147,35 @@ type SlimRecord struct { } +// CliRecord used for sending records to RESH-CLI +type CliRecord struct { + SessionID string `json:"sessionId"` + + CmdLine string `json:"cmdLine"` + Host string `json:"host"` + Pwd string `json:"pwd"` + Home string `json:"home"` // helps us to collapse /home/user to tilde + GitOriginRemote string `json:"gitOriginRemote"` + ExitCode int `json:"exitCode"` + + // RealtimeBefore float64 `json:"realtimeBefore"` + // RealtimeAfter float64 `json:"realtimeAfter"` + // RealtimeDuration float64 `json:"realtimeDuration"` +} + +// NewCliRecord from EnrichedRecord +func NewCliRecord(r EnrichedRecord) CliRecord { + return CliRecord{ + SessionID: r.SessionID, + CmdLine: r.CmdLine, + Host: r.Host, + Pwd: r.Pwd, + Home: r.Home, + GitOriginRemote: r.GitOriginRemote, + ExitCode: r.ExitCode, + } +} + // Convert from FallbackRecord to Record func Convert(r *FallbackRecord) Record { return Record{