Skip to content

Commit c6899ed

Browse files
authored
Merge pull request #287 from dmur1/ctftime-spinner-and-order-#279
fetch ctftime info with a spinner and display it only when its all ready
2 parents de2cb7c + 2ce3b87 commit c6899ed

File tree

1 file changed

+51
-15
lines changed

1 file changed

+51
-15
lines changed

commands/ctftime.go

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ import (
1212
"time"
1313
)
1414

15+
type CTFTimeInfo struct {
16+
Title string
17+
Start time.Time
18+
Finish time.Time
19+
}
20+
1521
func init() {
1622
Commands = append(Commands, Command{
1723
Name: "ctftime",
@@ -37,7 +43,20 @@ func CtfTimeHelp() string {
3743
"the ctftime urls will be used to aid in the generation of writeups with the " + theme.ColorGreen + "`writeup`" + theme.ColorReset + " command\n\n"
3844
}
3945

40-
func showStats(ctfTimeUrl string) {
46+
func ctftimeSpinner() {
47+
emojis := []string{
48+
"🕛", "⏳", "🕧", "🕐", "⏰", "🕞", "⏲️", "🚩",
49+
}
50+
51+
for {
52+
for _, e := range emojis {
53+
fmt.Printf("\r%s", e)
54+
time.Sleep(200 * time.Millisecond)
55+
}
56+
}
57+
}
58+
59+
func fetchInfo(ctfTimeUrl string, info *CTFTimeInfo) {
4160
splits := strings.Split(ctfTimeUrl, "/")
4261
eventId := splits[len(splits)-1]
4362

@@ -68,36 +87,41 @@ func showStats(ctfTimeUrl string) {
6887
log.Fatalf("💥 "+theme.ColorRed+" error"+theme.ColorReset+": %v\n", err)
6988
}
7089

71-
title := result["title"]
72-
fmt.Printf(theme.ColorGray+"title: "+theme.ColorBlue+"%s"+theme.ColorReset+"\n", title)
73-
74-
now := time.Now()
90+
info.Title = fmt.Sprintf("%s", result["title"])
7591

7692
start := fmt.Sprintf("%s", result["start"])
77-
startTime, err := time.Parse(time.RFC3339, start)
93+
info.Start, err = time.Parse(time.RFC3339, start)
94+
7895
if err != nil {
7996
log.Fatalf("💥 "+theme.ColorRed+" error"+theme.ColorReset+": %v\n", err)
8097
}
8198

8299
finish := fmt.Sprintf("%s", result["finish"])
83-
finishTime, err := time.Parse(time.RFC3339, finish)
100+
info.Finish, err = time.Parse(time.RFC3339, finish)
101+
84102
if err != nil {
85103
log.Fatalf("💥 "+theme.ColorRed+" error"+theme.ColorReset+": %v\n", err)
86104
}
105+
}
106+
107+
func showInfo(info *CTFTimeInfo) {
108+
fmt.Printf(theme.ColorGray+"title: "+theme.ColorBlue+"%s"+theme.ColorReset+"\n", info.Title)
109+
110+
now := time.Now()
87111

88-
fmt.Printf(theme.ColorGray+"start: "+theme.ColorGreen+"%v "+theme.ColorGray+"finish: "+theme.ColorRed+"%v"+theme.ColorReset+"\n", startTime, finishTime)
112+
fmt.Printf(theme.ColorGray+"start: "+theme.ColorGreen+"%v "+theme.ColorGray+"finish: "+theme.ColorRed+"%v"+theme.ColorReset+"\n", info.Start, info.Finish)
89113

90-
started := startTime.Before(now)
91-
finished := finishTime.Before(now)
114+
started := info.Start.Before(now)
115+
finished := info.Finish.Before(now)
92116
ongoing := started && !finished
93117

94118
if ongoing {
95-
fmt.Printf(theme.ColorGray+"time remaining: "+theme.ColorReset+"%v\n", finishTime.Sub(now))
119+
fmt.Printf(theme.ColorGray+"time remaining: "+theme.ColorReset+"%v\n", info.Finish.Sub(now))
96120
} else if finished {
97-
fmt.Printf(theme.ColorGray+"time since finish: "+theme.ColorReset+"%v\n", finishTime.Sub(now))
121+
fmt.Printf(theme.ColorGray+"time since finish: "+theme.ColorReset+"%v\n", info.Finish.Sub(now))
98122
} else {
99-
fmt.Printf(theme.ColorGray+"time to start: "+theme.ColorReset+"%v\n", startTime.Sub(now))
100-
fmt.Printf(theme.ColorGray+"time to finish: "+theme.ColorReset+"%v\n", finishTime.Sub(now))
123+
fmt.Printf(theme.ColorGray+"time to start: "+theme.ColorReset+"%v\n", info.Start.Sub(now))
124+
fmt.Printf(theme.ColorGray+"time to finish: "+theme.ColorReset+"%v\n", info.Finish.Sub(now))
101125
}
102126
}
103127

@@ -120,11 +144,23 @@ func CtfTime(args []string) {
120144
return
121145
}
122146

147+
go ctftimeSpinner()
148+
149+
infos := make([]CTFTimeInfo, len(config.CtfTimeUrls))
150+
151+
for idx, ctfTimeUrl := range config.CtfTimeUrls {
152+
if strings.Contains(ctfTimeUrl, "ctftime.org") {
153+
fetchInfo(ctfTimeUrl, &infos[idx])
154+
}
155+
}
156+
157+
fmt.Printf("\r")
158+
123159
for idx, ctfTimeUrl := range config.CtfTimeUrls {
124160
fmt.Printf(theme.ColorGray+"url: "+theme.ColorReset+"%v"+theme.ColorReset+"\n", ctfTimeUrl)
125161

126162
if strings.Contains(ctfTimeUrl, "ctftime.org") {
127-
showStats(ctfTimeUrl)
163+
showInfo(&infos[idx])
128164
if idx+1 < len(config.CtfTimeUrls) {
129165
fmt.Printf("\n")
130166
}

0 commit comments

Comments
 (0)