mirror of
https://github.com/ddworken/hishtory.git
synced 2025-02-08 22:50:44 +01:00
Move tui.go out of lib and into a separate package
This commit is contained in:
parent
6d6a1a5e12
commit
e8ceb02138
@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/ddworken/hishtory/client/hctx"
|
"github.com/ddworken/hishtory/client/hctx"
|
||||||
"github.com/ddworken/hishtory/client/lib"
|
"github.com/ddworken/hishtory/client/lib"
|
||||||
|
"github.com/ddworken/hishtory/client/tui"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -43,7 +44,7 @@ var tqueryCmd = &cobra.Command{
|
|||||||
DisableFlagParsing: true,
|
DisableFlagParsing: true,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
ctx := hctx.MakeContext()
|
ctx := hctx.MakeContext()
|
||||||
lib.CheckFatalError(lib.TuiQuery(ctx, strings.Join(args, " ")))
|
lib.CheckFatalError(tui.TuiQuery(ctx, strings.Join(args, " ")))
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,7 +134,7 @@ func getCustomColumnValue(ctx context.Context, header string, entry data.History
|
|||||||
return "", fmt.Errorf("failed to find a column matching the column name %#v (is there a typo?)", header)
|
return "", fmt.Errorf("failed to find a column matching the column name %#v (is there a typo?)", header)
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildTableRow(ctx context.Context, columnNames []string, entry data.HistoryEntry) ([]string, error) {
|
func BuildTableRow(ctx context.Context, columnNames []string, entry data.HistoryEntry) ([]string, error) {
|
||||||
row := make([]string, 0)
|
row := make([]string, 0)
|
||||||
for _, header := range columnNames {
|
for _, header := range columnNames {
|
||||||
switch header {
|
switch header {
|
||||||
@ -193,7 +193,7 @@ func DisplayResults(ctx context.Context, results []*data.HistoryEntry, numResult
|
|||||||
if entry != nil && strings.TrimSpace(entry.Command) == strings.TrimSpace(lastCommand) && config.FilterDuplicateCommands {
|
if entry != nil && strings.TrimSpace(entry.Command) == strings.TrimSpace(lastCommand) && config.FilterDuplicateCommands {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
row, err := buildTableRow(ctx, config.DisplayedColumns, *entry)
|
row, err := BuildTableRow(ctx, config.DisplayedColumns, *entry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package lib
|
package tui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@ -17,6 +17,7 @@ import (
|
|||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
"github.com/ddworken/hishtory/client/data"
|
"github.com/ddworken/hishtory/client/data"
|
||||||
"github.com/ddworken/hishtory/client/hctx"
|
"github.com/ddworken/hishtory/client/hctx"
|
||||||
|
"github.com/ddworken/hishtory/client/lib"
|
||||||
"github.com/ddworken/hishtory/client/table"
|
"github.com/ddworken/hishtory/client/table"
|
||||||
"github.com/ddworken/hishtory/shared"
|
"github.com/ddworken/hishtory/shared"
|
||||||
"github.com/muesli/termenv"
|
"github.com/muesli/termenv"
|
||||||
@ -365,7 +366,7 @@ func (m model) View() string {
|
|||||||
func getRows(ctx context.Context, columnNames []string, query string, numEntries int) ([]table.Row, []*data.HistoryEntry, error) {
|
func getRows(ctx context.Context, columnNames []string, query string, numEntries int) ([]table.Row, []*data.HistoryEntry, error) {
|
||||||
db := hctx.GetDb(ctx)
|
db := hctx.GetDb(ctx)
|
||||||
config := hctx.GetConf(ctx)
|
config := hctx.GetConf(ctx)
|
||||||
searchResults, err := Search(ctx, db, query, numEntries)
|
searchResults, err := lib.Search(ctx, db, query, numEntries)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@ -379,7 +380,7 @@ func getRows(ctx context.Context, columnNames []string, query string, numEntries
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
entry.Command = strings.ReplaceAll(entry.Command, "\n", "\\n")
|
entry.Command = strings.ReplaceAll(entry.Command, "\n", "\\n")
|
||||||
row, err := buildTableRow(ctx, columnNames, *entry)
|
row, err := lib.BuildTableRow(ctx, columnNames, *entry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("failed to build row for entry=%#v: %w", entry, err)
|
return nil, nil, fmt.Errorf("failed to build row for entry=%#v: %w", entry, err)
|
||||||
}
|
}
|
||||||
@ -567,7 +568,7 @@ func deleteHistoryEntry(ctx context.Context, entry data.HistoryEntry) error {
|
|||||||
SendTime: time.Now(),
|
SendTime: time.Now(),
|
||||||
}
|
}
|
||||||
dr.Messages.Ids = append(dr.Messages.Ids, shared.MessageIdentifier{Date: entry.EndTime, DeviceId: entry.DeviceId})
|
dr.Messages.Ids = append(dr.Messages.Ids, shared.MessageIdentifier{Date: entry.EndTime, DeviceId: entry.DeviceId})
|
||||||
return SendDeletionRequest(dr)
|
return lib.SendDeletionRequest(dr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TuiQuery(ctx context.Context, initialQuery string) error {
|
func TuiQuery(ctx context.Context, initialQuery string) error {
|
||||||
@ -588,7 +589,7 @@ func TuiQuery(ctx context.Context, initialQuery string) error {
|
|||||||
p := tea.NewProgram(initialModel(ctx, t, entries, initialQuery), tea.WithOutput(os.Stderr))
|
p := tea.NewProgram(initialModel(ctx, t, entries, initialQuery), tea.WithOutput(os.Stderr))
|
||||||
// Async: Retrieve additional entries from the backend
|
// Async: Retrieve additional entries from the backend
|
||||||
go func() {
|
go func() {
|
||||||
err := RetrieveAdditionalEntriesFromRemote(ctx)
|
err := lib.RetrieveAdditionalEntriesFromRemote(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.Send(err)
|
p.Send(err)
|
||||||
}
|
}
|
||||||
@ -596,16 +597,16 @@ func TuiQuery(ctx context.Context, initialQuery string) error {
|
|||||||
}()
|
}()
|
||||||
// Async: Process deletion requests
|
// Async: Process deletion requests
|
||||||
go func() {
|
go func() {
|
||||||
err := ProcessDeletionRequests(ctx)
|
err := lib.ProcessDeletionRequests(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.Send(err)
|
p.Send(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
// Async: Check for any banner from the server
|
// Async: Check for any banner from the server
|
||||||
go func() {
|
go func() {
|
||||||
banner, err := GetBanner(ctx)
|
banner, err := lib.GetBanner(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if IsOfflineError(err) {
|
if lib.IsOfflineError(err) {
|
||||||
p.Send(offlineMsg{})
|
p.Send(offlineMsg{})
|
||||||
} else {
|
} else {
|
||||||
p.Send(err)
|
p.Send(err)
|
Loading…
Reference in New Issue
Block a user