Skip to content

Commit 80d102b

Browse files
committed
Waiting animation for anilist mode
1 parent 9173718 commit 80d102b

File tree

5 files changed

+53
-41
lines changed

5 files changed

+53
-41
lines changed

anilist/animation_helpers.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package anilist
2+
3+
import (
4+
"github.com/aqatl/mal/oauth2"
5+
"github.com/aqatl/cliwait"
6+
)
7+
8+
func QueryUserListsWaitAnimation(userId int, token oauth2.OAuthToken) ([]MediaListGroup, error) {
9+
var mlg []MediaListGroup
10+
var err error
11+
cliwait.DoFuncWithWaitAnimation("Queyring user list", func() {
12+
mlg, err = QueryUserLists(userId, token)
13+
})
14+
return mlg, err
15+
}
16+
17+
func QueryAuthenticatedUserWaitAnimation(user *User, token oauth2.OAuthToken) error {
18+
var err error
19+
cliwait.DoFuncWithWaitAnimation("Saving entry", func() {
20+
err = QueryAuthenticatedUser(user, token)
21+
})
22+
return err
23+
}
24+
25+
func SaveMediaListEntryWaitAnimation(entry *MediaListEntry, token oauth2.OAuthToken) error {
26+
var err error
27+
cliwait.DoFuncWithWaitAnimation("Saving entry", func() {
28+
err = SaveMediaListEntry(entry, token)
29+
})
30+
return err
31+
}
32+
33+
func QueryAiringScheduleWaitAnimation(mediaId, episode int, token oauth2.OAuthToken) (
34+
AiringSchedule, error,
35+
) {
36+
var as AiringSchedule
37+
var err error
38+
cliwait.DoFuncWithWaitAnimation("Querying airing schedule", func() {
39+
as, err = QueryAiringSchedule(mediaId, episode, token)
40+
})
41+
return as, err
42+
}

anilist_app.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func alSetEntryEpisodes(ctx *cli.Context) error {
241241

242242
alStatusAutoUpdate(cfg, entry)
243243

244-
if err = anilist.SaveMediaListEntry(entry, al.Token); err != nil {
244+
if err = anilist.SaveMediaListEntryWaitAnimation(entry, al.Token); err != nil {
245245
return err
246246
}
247247
if err = saveAniListAnimeLists(al); err != nil {
@@ -285,7 +285,7 @@ func alSetEntryStatus(ctx *cli.Context) error {
285285

286286
entry.Status = status
287287

288-
if err = anilist.SaveMediaListEntry(entry, al.Token); err != nil {
288+
if err = anilist.SaveMediaListEntryWaitAnimation(entry, al.Token); err != nil {
289289
return err
290290
}
291291
if err = saveAniListAnimeLists(al); err != nil {
@@ -310,7 +310,7 @@ func alSetEntryScore(ctx *cli.Context) error {
310310

311311
entry.Score = score
312312

313-
if err = anilist.SaveMediaListEntry(entry, al.Token); err != nil {
313+
if err = anilist.SaveMediaListEntryWaitAnimation(entry, al.Token); err != nil {
314314
return err
315315
}
316316
if err = saveAniListAnimeLists(al); err != nil {
@@ -484,7 +484,7 @@ func alNextAiringEpisode(ctx *cli.Context) error {
484484
if cfg.StatusAutoUpdateMode != AfterThreshold && entry.Progress < entry.Episodes {
485485
episode++
486486
}
487-
schedule, err := anilist.QueryAiringSchedule(entry.Id, episode, al.Token)
487+
schedule, err := anilist.QueryAiringScheduleWaitAnimation(entry.Id, episode, al.Token)
488488
if err != nil {
489489
return err
490490
}

cache.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515
"io/ioutil"
1616
"golang.org/x/crypto/ssh/terminal"
17+
"github.com/aqatl/cliwait"
1718
)
1819

1920
func checkDataDir() {
@@ -64,7 +65,7 @@ func loadData(c *mal.Client, ctx *cli.Context) (mal.AnimeList, error) {
6465
if ctx.GlobalBool("refresh") || cacheNotExist() {
6566
{
6667
var err error
67-
mal.DoFuncWithWaitAnimation("Fetching your list", func() {
68+
cliwait.DoFuncWithWaitAnimation("Fetching your list", func() {
6869
list, err = c.AnimeList(mal.All)
6970
})
7071
return nil, fmt.Errorf("error fetching your list\n%v", err)

mal/utils.go

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import (
44
"strings"
55
"fmt"
66
"github.com/PuerkitoBio/goquery"
7-
"time"
8-
"github.com/fatih/color"
9-
"bufio"
10-
"os"
7+
"github.com/aqatl/cliwait"
118
)
129

1310
func ParseStatus(status string) MyStatus {
@@ -41,39 +38,10 @@ func isTextEqualFilterFunc(text string) func(i int, s *goquery.Selection) bool {
4138
}
4239
}
4340

44-
func DoFuncWithWaitAnimation(text string, f func()) {
45-
done := make(chan struct{})
46-
go func() {
47-
f()
48-
done <- struct{}{}
49-
}()
50-
51-
ticker := time.NewTicker(100 * time.Millisecond)
52-
defer ticker.Stop()
53-
54-
green := color.New(color.FgHiGreen).FprintFunc()
55-
clockStates := [...]string{"-", "\\", "|", "/"}
56-
currClockState := 0
57-
58-
stdout := bufio.NewWriter(os.Stdout)
59-
for {
60-
select {
61-
case <-ticker.C:
62-
green(color.Output, fmt.Sprintf("\r%s %s", text, clockStates[currClockState]))
63-
stdout.Flush()
64-
currClockState = (currClockState + 1) % len(clockStates)
65-
case <-done:
66-
fmt.Fprintf(color.Output, "\r%s\r", strings.Repeat(" ", len(text)+4))
67-
stdout.Flush()
68-
return
69-
}
70-
}
71-
}
72-
7341
func FetchDetailsWithAnimation(c *Client, entry *Anime) (*AnimeDetails, error) {
7442
var details *AnimeDetails
7543
var err error
76-
DoFuncWithWaitAnimation("Fetching details", func() {
44+
cliwait.DoFuncWithWaitAnimation("Fetching details", func() {
7745
details, err = c.FetchDetails(entry)
7846
})
7947
return details, err
@@ -82,7 +50,7 @@ func FetchDetailsWithAnimation(c *Client, entry *Anime) (*AnimeDetails, error) {
8250

8351
func UpdateEntryWithAnimation(c *Client, entry *Anime) (error) {
8452
var err error
85-
DoFuncWithWaitAnimation("Updating entry", func() {
53+
cliwait.DoFuncWithWaitAnimation("Updating entry", func() {
8654
err = c.Update(entry)
8755
})
8856
return err

mal_app.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/fatih/color"
1515
"github.com/skratchdot/open-golang/open"
1616
"github.com/urfave/cli"
17+
"github.com/aqatl/cliwait"
1718
)
1819

1920
func MalApp(app *cli.App) *cli.App {
@@ -544,7 +545,7 @@ func deleteEntry(ctx *cli.Context) error {
544545
return fmt.Errorf("no entry selected")
545546
}
546547

547-
mal.DoFuncWithWaitAnimation("Deleting entry", func() {
548+
cliwait.DoFuncWithWaitAnimation("Deleting entry", func() {
548549
err = c.Delete(entry)
549550
})
550551
if err != nil {

0 commit comments

Comments
 (0)