Skip to content

Commit

Permalink
Merge pull request #4 from rusq/deps
Browse files Browse the repository at this point in the history
Dependencies bump
  • Loading branch information
rusq authored Nov 13, 2023
2 parents e83c884 + 91c0a3f commit 4039167
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 224 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.21

- name: Build
run: go build -v ./...
Expand Down
5 changes: 3 additions & 2 deletions cmd/testui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ func init() {

func main() {
chats := generateChats(fakechats)
app := tui.New(FakeTelegram{chats: chats})
ctx := context.Background()
app := tui.New(ctx, FakeTelegram{chats: chats})

if err := app.Run(context.Background(), chats); err != nil {
if err := app.Run(ctx, chats); err != nil {
dlog.Fatal(err)
}

Expand Down
54 changes: 27 additions & 27 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
module github.com/rusq/wipemychat

go 1.18
go 1.21

require (
github.com/fatih/color v1.14.1
github.com/fatih/color v1.16.0
github.com/gdamore/tcell/v2 v2.6.0
github.com/gotd/td v0.76.0
github.com/gotd/td v0.89.0
github.com/joho/godotenv v1.5.1
github.com/looplab/fsm v0.3.0
github.com/rivo/tview v0.0.0-20230208211350-7dfff1ce7854
github.com/rusq/dlog v1.3.3
github.com/looplab/fsm v1.0.1
github.com/rivo/tview v0.0.0-20231113063814-05d01944a18b
github.com/rusq/dlog v1.4.0
github.com/rusq/mtpwrap v0.0.4
github.com/rusq/osenv/v2 v2.0.1
github.com/rusq/tracer v1.0.1
github.com/schollz/progressbar/v3 v3.13.0
github.com/schollz/progressbar/v3 v3.14.1
)

require (
github.com/bluele/gcache v0.0.2 // indirect
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/denisbrodbeck/machineid v1.0.1 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/go-faster/errors v0.6.1 // indirect
github.com/go-faster/jx v0.41.0 // indirect
github.com/go-faster/xor v0.3.0 // indirect
github.com/gotd/contrib v0.13.0 // indirect
github.com/go-faster/errors v0.7.0 // indirect
github.com/go-faster/jx v1.1.0 // indirect
github.com/go-faster/xor v1.0.0 // indirect
github.com/gotd/contrib v0.19.0 // indirect
github.com/gotd/ige v0.2.2 // indirect
github.com/gotd/neo v0.1.5 // indirect
github.com/klauspost/compress v1.15.15 // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rusq/encio v0.1.0 // indirect
github.com/rusq/secure v0.0.4 // indirect
github.com/segmentio/asm v1.2.0 // indirect
go.opentelemetry.io/otel v1.13.0 // indirect
go.opentelemetry.io/otel/trace v1.13.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
nhooyr.io/websocket v1.8.7 // indirect
go.opentelemetry.io/otel v1.20.0 // indirect
go.opentelemetry.io/otel/trace v1.20.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.15.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/term v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
nhooyr.io/websocket v1.8.10 // indirect
rsc.io/qr v0.2.0 // indirect
)
208 changes: 57 additions & 151 deletions go.sum

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions internal/tui/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type views struct {
tvLog *tview.TextView
}

func New(tg waipu.Telegramer) *App {
func New(ctx context.Context, tg waipu.Telegramer) *App {
app := &App{
tva: tview.NewApplication(),
tg: tg,
Expand All @@ -57,10 +57,10 @@ func New(tg waipu.Telegramer) *App {
},
}

app.initMain()
app.initFind()
app.initConfirm()
app.initNothing()
app.initMain(ctx)
app.initFind(ctx)
app.initConfirm(ctx)
app.initNothing(ctx)

app.tva.SetInputCapture(app.handleKeystrokes)

Expand Down Expand Up @@ -105,13 +105,13 @@ func (app *App) handleKeystrokes(event *tcell.EventKey) *tcell.EventKey {
}

// cancel sends a evCancelled event.
func (app *App) cancel() {
app.event(evCancelled)
func (app *App) cancel(ctx context.Context) {
app.event(ctx, evCancelled)
}

// event sends an event to FSM, will return true, if there were no errors.
func (app *App) event(event string) bool {
if err := app.fsm.Event(event); err != nil {
func (app *App) event(ctx context.Context, event string) bool {
if err := app.fsm.Event(ctx, event); err != nil {
app.error(err)
return false
}
Expand Down
25 changes: 13 additions & 12 deletions internal/tui/chatlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
mtp "github.com/rusq/mtpwrap"
)

const infoText = "Press [Ctrl+Q] or [F10] to quit, [Ctrl+F] or [/] to search chats"
const infoText = "Press <Ctrl+Q> or <F10> to quit, <Ctrl+F> or </> to search chats"

func (app *App) initMain() {
func (app *App) initMain(context.Context) {
app.view.lvChats.
SetHighlightFullLine(true).
SetSelectedBackgroundColor(tcell.Color190).
Expand Down Expand Up @@ -64,16 +64,16 @@ func (app *App) populateChatList(ctx context.Context, chats []mtp.Entity) {
}

func (app *App) handleChats(ctx context.Context, chats []mtp.Entity) {
if !app.event(evSelected) {
if !app.event(ctx, evSelected) {
return
}

selected := chats[app.view.lvChats.GetCurrentItem()]
// async fetch is needed so that the tvLog will keep updating.
go app.runDelete(selected)
go app.runDelete(ctx, selected)
}

func (app *App) runDelete(selected mtp.Entity) {
func (app *App) runDelete(ctx context.Context, selected mtp.Entity) {
// disable input on lvChats
app.view.lvChats.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { return nil })
defer func() {
Expand All @@ -95,15 +95,15 @@ func (app *App) runDelete(selected mtp.Entity) {
}
if err != nil {
app.error(err)
app.cancel()
app.cancel(ctx)
return
}
app.logf("Scan complete, found %d messages", len(msgs))

if len(msgs) == 0 {
// show nothing to do message.
if !app.event(evNothingToDo) {
app.cancel()
if !app.event(ctx, evNothingToDo) {
app.cancel(ctx)
}
return
}
Expand All @@ -112,22 +112,23 @@ func (app *App) runDelete(selected mtp.Entity) {
app.fsm.SetMetadata(metaMessages, msgs)
app.view.mbConfirm.SetText(fmt.Sprintf("Found %d messages in %q. Delete?", len(msgs), selected.GetTitle()))

if !app.event(evFetched) {
app.cancel()
if !app.event(ctx, evFetched) {
app.cancel(ctx)
return
}
}

func (app *App) chatInputCapture(event *tcell.EventKey) *tcell.EventKey {
ctx := context.TODO()
switch event.Key() {
case tcell.KeyCtrlF:
if !app.event(evSearch) {
if !app.event(ctx, evSearch) {
return event
}
case tcell.KeyRune:
switch event.Rune() {
case '/':
if !app.event(evSearch) {
if !app.event(ctx, evSearch) {
return event
}
}
Expand Down
15 changes: 8 additions & 7 deletions internal/tui/confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ import (
mtp "github.com/rusq/mtpwrap"
)

func (app *App) initConfirm() {
func (app *App) initConfirm(ctx context.Context) {
app.pages.AddPage(stConfirming, app.view.mbConfirm, false, false)
app.view.mbConfirm.
AddButtons([]string{btnYes, btnNo}).
SetDoneFunc(app.handleConfirm).
SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyESC {
app.cancel()
app.cancel(ctx)
return nil
}
return event
})
}

func (app *App) handleConfirm(_ int, buttonLabel string) {
ctx := context.TODO()
var err error
switch buttonLabel {
case btnYes:
if !app.event(evConfirmed) {
if !app.event(ctx, evConfirmed) {
return
}
err = app.handleDelete()
err = app.handleDelete(ctx)
case btnNo:
app.cancel()
app.cancel(ctx)
default:
err = nil
}
Expand All @@ -44,8 +45,8 @@ func (app *App) handleConfirm(_ int, buttonLabel string) {

// handleDelete handles the deletion of the messages. It gets the chat
// and messages to delete from the FSM Metadata.
func (app *App) handleDelete() error {
defer app.event(evDeleted)
func (app *App) handleDelete(ctx context.Context) error {
defer app.event(ctx, evDeleted)
chat, err := metadata[mtp.Entity](app.fsm, metaChat)
if err != nil {
return fmt.Errorf("chat missing: %s", err)
Expand Down
16 changes: 9 additions & 7 deletions internal/tui/find.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package tui

import (
"context"

"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)

func (app *App) initFind() {
func (app *App) initFind(ctx context.Context) {
app.pages.AddPage(stSearching, modal(app.view.fmSearch, 60, 5), true, false)
input := tview.NewInputField().SetLabel("Search")
app.view.fmSearch.
Expand All @@ -16,36 +18,36 @@ func (app *App) initFind() {
SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyCR:
app.findChat()
app.findChat(ctx)
input.SetText("")
return nil
case tcell.KeyESC:
app.cancel()
app.cancel(ctx)
return nil
}
return event
})
}

func (app *App) findChat() {
func (app *App) findChat(ctx context.Context) {
val := app.view.fmSearch.GetFormItem(0).(*tview.InputField)

text := val.GetText()
if text == "" {
app.logf("search input is empty")
app.cancel()
app.cancel(ctx)
return
}

loc := app.view.lvChats.FindItems(text, text, false, true)
if len(loc) == 0 {
app.logf("search term not found: %q", text)
app.cancel()
app.cancel(ctx)
return
}

app.view.lvChats.SetCurrentItem(loc[0])
if !app.event(evLocate) {
if !app.event(ctx, evLocate) {
return
}
}
11 changes: 6 additions & 5 deletions internal/tui/fsm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tui

import (
"context"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -54,7 +55,7 @@ func initFSM(app *App) *fsm.FSM {
{Name: evCancelled, Src: []string{stFetching, stConfirming, stNothing, stSearching}, Dst: stSelecting},
},
fsm.Callbacks{
m.enter("state"): func(e *fsm.Event) {
m.enter("state"): func(_ context.Context, e *fsm.Event) {
m.app.log.Debugf("*** transition: %q -> %q\n", e.Src, e.Dst)
m.app.pages.ShowPage(e.Dst)
},
Expand Down Expand Up @@ -88,20 +89,20 @@ func (*machine) after(event string) string {
// States
//

func (m *machine) hidePage(e *fsm.Event) {
func (m *machine) hidePage(_ context.Context, e *fsm.Event) {
m.app.pages.HidePage(e.Src)
}

func (m *machine) leaveDeleting(e *fsm.Event) {
func (m *machine) leaveDeleting(ctx context.Context, e *fsm.Event) {
m.cleanUp()
m.hidePage(e)
m.hidePage(ctx, e)
}

//
// Events
//

func (m *machine) afterCancelled(*fsm.Event) {
func (m *machine) afterCancelled(context.Context, *fsm.Event) {
// clear metadata
m.cleanUp()
m.app.logf("Operation cancelled")
Expand Down
6 changes: 4 additions & 2 deletions internal/tui/nothing.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package tui

func (app *App) initNothing() {
import "context"

func (app *App) initNothing(ctx context.Context) {
app.pages.AddPage(stNothing, app.view.mbNothing, false, false)
app.view.mbNothing.
SetDoneFunc(func(_ int, _ string) {
app.cancel()
app.cancel(ctx)
}).
SetText("There are no messages to delete").
AddButtons([]string{btnOK})
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func run(ctx context.Context, p Params) error {
return waipu.Batch(ctx, cl, []int64(p.Batch))
} else {
// run UI
tva := tui.New(cl)
tva := tui.New(ctx, cl)
if err := tva.Run(ctx, chats); err != nil {
return err
}
Expand Down

0 comments on commit 4039167

Please sign in to comment.