2022-10-16 18:22:34 +02:00
|
|
|
package lib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-10-16 21:43:16 +02:00
|
|
|
"os"
|
|
|
|
"strings"
|
2022-10-16 18:22:34 +02:00
|
|
|
|
|
|
|
_ "embed" // for embedding config.sh
|
|
|
|
|
|
|
|
"github.com/charmbracelet/bubbles/spinner"
|
|
|
|
"github.com/charmbracelet/bubbles/table"
|
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
"github.com/ddworken/hishtory/client/data"
|
|
|
|
"github.com/ddworken/hishtory/client/hctx"
|
2022-10-16 21:43:16 +02:00
|
|
|
"github.com/muesli/termenv"
|
2022-10-16 18:22:34 +02:00
|
|
|
)
|
|
|
|
|
2022-10-16 21:43:16 +02:00
|
|
|
const TABLE_HEIGHT = 20
|
|
|
|
const PADDED_NUM_ENTRIES = TABLE_HEIGHT * 3
|
|
|
|
|
|
|
|
var selectedRow string = ""
|
|
|
|
|
2022-10-16 18:22:34 +02:00
|
|
|
var baseStyle = lipgloss.NewStyle().
|
|
|
|
BorderStyle(lipgloss.NormalBorder()).
|
|
|
|
BorderForeground(lipgloss.Color("240"))
|
|
|
|
|
|
|
|
type errMsg error
|
|
|
|
|
|
|
|
type model struct {
|
|
|
|
ctx *context.Context
|
|
|
|
spinner spinner.Model
|
|
|
|
quitting bool
|
|
|
|
isLoading bool
|
|
|
|
selected bool
|
|
|
|
table table.Model
|
|
|
|
runQuery string
|
2022-10-16 21:43:16 +02:00
|
|
|
lastQuery string
|
2022-10-16 18:22:34 +02:00
|
|
|
err error
|
|
|
|
queryInput textinput.Model
|
|
|
|
banner string
|
|
|
|
isOffline bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type doneDownloadingMsg struct{}
|
|
|
|
type offlineMsg struct{}
|
|
|
|
type bannerMsg struct {
|
|
|
|
banner string
|
|
|
|
}
|
|
|
|
|
2022-10-16 21:43:16 +02:00
|
|
|
func initialModel(ctx *context.Context, t table.Model, initialQuery string) model {
|
2022-10-16 18:22:34 +02:00
|
|
|
s := spinner.New()
|
|
|
|
s.Spinner = spinner.Dot
|
|
|
|
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
|
|
|
|
queryInput := textinput.New()
|
|
|
|
queryInput.Placeholder = "ls"
|
|
|
|
queryInput.Focus()
|
|
|
|
queryInput.CharLimit = 156
|
|
|
|
queryInput.Width = 50
|
2022-10-16 21:43:16 +02:00
|
|
|
if initialQuery != "" {
|
|
|
|
queryInput.SetValue(initialQuery)
|
|
|
|
}
|
|
|
|
return model{ctx: ctx, spinner: s, isLoading: true, table: t, runQuery: initialQuery, queryInput: queryInput}
|
2022-10-16 18:22:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) Init() tea.Cmd {
|
|
|
|
return m.spinner.Tick
|
|
|
|
}
|
|
|
|
|
2022-10-16 21:43:16 +02:00
|
|
|
func runQueryAndUpdateTable(m model) model {
|
|
|
|
if m.runQuery != "" && m.runQuery != m.lastQuery {
|
2022-10-16 18:22:34 +02:00
|
|
|
rows, err := getRows(m.ctx, m.runQuery)
|
|
|
|
if err != nil {
|
|
|
|
m.err = err
|
2022-10-16 21:43:16 +02:00
|
|
|
return m
|
2022-10-16 18:22:34 +02:00
|
|
|
}
|
|
|
|
m.table.SetRows(rows)
|
|
|
|
m.table.SetCursor(0)
|
2022-10-16 21:43:16 +02:00
|
|
|
m.lastQuery = m.runQuery
|
2022-10-16 18:22:34 +02:00
|
|
|
m.runQuery = ""
|
|
|
|
}
|
2022-10-16 21:43:16 +02:00
|
|
|
return m
|
|
|
|
}
|
2022-10-16 18:22:34 +02:00
|
|
|
|
2022-10-16 21:43:16 +02:00
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
2022-10-16 18:22:34 +02:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
case tea.KeyMsg:
|
|
|
|
switch msg.String() {
|
|
|
|
case "q", "ctrl+c":
|
|
|
|
m.quitting = true
|
|
|
|
return m, tea.Quit
|
|
|
|
case "enter":
|
|
|
|
m.selected = true
|
|
|
|
return m, tea.Quit
|
|
|
|
default:
|
|
|
|
t, cmd1 := m.table.Update(msg)
|
|
|
|
m.table = t
|
|
|
|
i, cmd2 := m.queryInput.Update(msg)
|
|
|
|
m.queryInput = i
|
|
|
|
m.runQuery = m.queryInput.Value()
|
2022-10-16 21:43:16 +02:00
|
|
|
m = runQueryAndUpdateTable(m)
|
2022-10-16 18:22:34 +02:00
|
|
|
return m, tea.Batch(cmd1, cmd2)
|
|
|
|
}
|
|
|
|
case errMsg:
|
|
|
|
m.err = msg
|
|
|
|
return m, nil
|
|
|
|
case offlineMsg:
|
|
|
|
m.isOffline = true
|
|
|
|
return m, nil
|
|
|
|
case bannerMsg:
|
|
|
|
m.banner = msg.banner
|
|
|
|
return m, nil
|
|
|
|
case doneDownloadingMsg:
|
|
|
|
m.isLoading = false
|
|
|
|
return m, nil
|
|
|
|
default:
|
|
|
|
var cmd tea.Cmd
|
|
|
|
if m.isLoading {
|
|
|
|
m.spinner, cmd = m.spinner.Update(msg)
|
|
|
|
return m, cmd
|
|
|
|
} else {
|
|
|
|
m.table, cmd = m.table.Update(msg)
|
|
|
|
return m, cmd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) View() string {
|
|
|
|
if m.err != nil {
|
|
|
|
return fmt.Sprintf("An unrecoverable error occured: %v\n", m.err)
|
|
|
|
}
|
|
|
|
if m.selected {
|
2022-10-16 21:43:16 +02:00
|
|
|
selectedRow = m.table.SelectedRow()[4]
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
loadingMessage := ""
|
|
|
|
if m.isLoading {
|
|
|
|
loadingMessage = fmt.Sprintf("%s Loading hishtory entries from other devices...", m.spinner.View())
|
2022-10-16 18:22:34 +02:00
|
|
|
}
|
|
|
|
offlineWarning := ""
|
|
|
|
if m.isOffline {
|
|
|
|
offlineWarning = "Warning: failed to contact the hishtory backend (are you offline?), so some results may be stale\n\n"
|
|
|
|
}
|
2022-10-16 21:43:16 +02:00
|
|
|
return fmt.Sprintf("\n%s\n%s%s\nSearch Query: %s\n\n%s\n", loadingMessage, offlineWarning, m.banner, m.queryInput.View(), baseStyle.Render(m.table.View()))
|
2022-10-16 18:22:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getRows(ctx *context.Context, query string) ([]table.Row, error) {
|
|
|
|
db := hctx.GetDb(ctx)
|
2022-10-16 21:43:16 +02:00
|
|
|
data, err := data.Search(db, query, PADDED_NUM_ENTRIES)
|
2022-10-16 18:22:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var rows []table.Row
|
2022-10-16 21:43:16 +02:00
|
|
|
for i := 0; i < PADDED_NUM_ENTRIES; i++ {
|
|
|
|
if i < len(data) {
|
|
|
|
entry := data[i]
|
|
|
|
entry.Command = strings.ReplaceAll(entry.Command, "\n", " ") // TODO: handle multi-line commands better here
|
|
|
|
row := table.Row{entry.Hostname, entry.CurrentWorkingDirectory, entry.StartTime.Format("Jan 2 2006 15:04:05 MST"), fmt.Sprintf("%d", entry.ExitCode), entry.Command}
|
|
|
|
rows = append(rows, row)
|
|
|
|
} else {
|
|
|
|
rows = append(rows, table.Row{})
|
|
|
|
}
|
2022-10-16 18:22:34 +02:00
|
|
|
}
|
|
|
|
return rows, nil
|
|
|
|
}
|
|
|
|
|
2022-10-16 21:43:16 +02:00
|
|
|
func TuiQuery(ctx *context.Context, initialQuery string) error {
|
|
|
|
lipgloss.SetColorProfile(termenv.ANSI)
|
2022-10-16 18:22:34 +02:00
|
|
|
columns := []table.Column{
|
|
|
|
{Title: "Hostname", Width: 25},
|
|
|
|
{Title: "CWD", Width: 40},
|
|
|
|
{Title: "Timestamp", Width: 25},
|
|
|
|
{Title: "Exit Code", Width: 9},
|
|
|
|
{Title: "Command", Width: 70},
|
|
|
|
}
|
2022-10-16 21:43:16 +02:00
|
|
|
rows, err := getRows(ctx, initialQuery)
|
2022-10-16 18:22:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t := table.New(
|
|
|
|
table.WithColumns(columns),
|
2022-10-16 21:43:16 +02:00
|
|
|
table.WithRows(rows), // TODO: need to pad this to always have at least length items
|
2022-10-16 18:22:34 +02:00
|
|
|
table.WithFocused(true),
|
2022-10-16 21:43:16 +02:00
|
|
|
table.WithHeight(TABLE_HEIGHT),
|
2022-10-16 18:22:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
s := table.DefaultStyles()
|
|
|
|
s.Header = s.Header.
|
|
|
|
BorderStyle(lipgloss.NormalBorder()).
|
|
|
|
BorderForeground(lipgloss.Color("240")).
|
|
|
|
BorderBottom(true).
|
|
|
|
Bold(false)
|
|
|
|
s.Selected = s.Selected.
|
|
|
|
Foreground(lipgloss.Color("229")).
|
|
|
|
Background(lipgloss.Color("57")).
|
|
|
|
Bold(false)
|
|
|
|
t.SetStyles(s)
|
|
|
|
t.Focus()
|
|
|
|
|
2022-10-16 21:43:16 +02:00
|
|
|
p := tea.NewProgram(initialModel(ctx, t, initialQuery), tea.WithOutput(os.Stderr))
|
2022-10-16 18:22:34 +02:00
|
|
|
go func() {
|
|
|
|
err := RetrieveAdditionalEntriesFromRemote(ctx)
|
|
|
|
if err != nil {
|
|
|
|
p.Send(err)
|
|
|
|
}
|
|
|
|
p.Send(doneDownloadingMsg{})
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
banner, err := GetBanner(ctx, "TODO_WIRE_GIT_COMMIT_HERE")
|
|
|
|
if err != nil {
|
|
|
|
if IsOfflineError(err) {
|
|
|
|
p.Send(offlineMsg{})
|
|
|
|
} else {
|
|
|
|
p.Send(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.Send(bannerMsg{banner: string(banner)})
|
|
|
|
}()
|
|
|
|
err = p.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-16 21:43:16 +02:00
|
|
|
fmt.Printf("%s\n", selectedRow)
|
2022-10-16 18:22:34 +02:00
|
|
|
return nil
|
|
|
|
}
|
2022-10-16 21:43:16 +02:00
|
|
|
|
|
|
|
// TODO: handling if someone hits enter when there are no results
|