From 7fc161adebbc449d5e0754c28ed420dceff99428 Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Mon, 28 Jan 2019 08:48:54 +0530 Subject: [PATCH 1/2] Added control for speed --- README.md | 3 +++ main.go | 11 ++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1ebcccb..c2c0e2a 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,9 @@ Start watching Star Wars Episode IV from a shell like this: curl https://asciitv.fr ``` +Parameters +- `speed` - Any integer (default: 1) + ## Credits diff --git a/main.go b/main.go index d8787f8..26537b2 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,8 @@ type frame struct { duration time.Duration } +var speed = 1 // Default speed of the movie + func main() { moviePtr := flag.String("movie", "resources/sw1.txt", "path to ASCII movie file") addrPtr := flag.String("addr", ":8080", "TCP address to listen on") @@ -44,6 +46,13 @@ func main() { fmt.Fprintf(w, "Web browsers are so %d, use \"curl https://asciitv.fr\" from your terminal instead!\n", time.Now().Year()-1) return } + + if value, ok := r.URL.Query()["speed"]; ok { + if speedInt, err := strconv.Atoi(value[0]); err == nil { + speed = speedInt + } + } + for _, frame := range frames { // Clear terminal and move cursor to position (1,1) fmt.Fprint(w, "\033[2J\033[1;1H") @@ -53,7 +62,7 @@ func main() { if f, ok := w.(http.Flusher); ok { f.Flush() } - time.Sleep(frame.duration * time.Second / 15) + time.Sleep((frame.duration / time.Duration(speed)) * time.Second / 15) } }) From 577aea0e1f56c87383ff353563414ea5e6051e20 Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Mon, 28 Jan 2019 09:08:48 +0530 Subject: [PATCH 2/2] Displaying the speed if it's not the default speed --- main.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/main.go b/main.go index 26537b2..db931a1 100644 --- a/main.go +++ b/main.go @@ -59,6 +59,11 @@ func main() { for _, line := range frame.lines { fmt.Fprintln(w, line) } + + if speed != 1 { + fmt.Fprintln(w, "\n\nSpeed:", speed) + } + if f, ok := w.(http.Flusher); ok { f.Flush() }