Skip to content

Instantly share code, notes, and snippets.

@keirlawson
Created November 8, 2018 15:11
Show Gist options
  • Save keirlawson/f68660a9a8284474ab52d5cfed6ad168 to your computer and use it in GitHub Desktop.
Save keirlawson/f68660a9a8284474ab52d5cfed6ad168 to your computer and use it in GitHub Desktop.
package com.example.websocketdownload
import cats.effect.IO
import me.tongfei.progressbar.{ProgressBar => pBar}
import fs2.{Sink, Stream}
import cats.implicits._
class ProgressBar(value: pBar) {
def start(): IO[Unit] = IO {
value.start()
}
def stepTo(n: Long): IO[Unit] = IO {
value.stepTo(n)
}
def stop(): IO[Unit] = IO {
value.stop()
}
}
object ProgressBar {
def apply(name: String, initialMax: Long): IO[ProgressBar] = IO {
val bar = new pBar(name, initialMax)
new ProgressBar(bar)
}
def stream(name: String, initialMax: Long): Stream[IO, ProgressBar] = {
Stream.bracket(ProgressBar(name, initialMax).flatTap(p => p.start()))(pb => Stream.emit(pb), _.stop())
}
def sink(name: String, initialMax: Long): Sink[IO, Long] = input =>
stream(name, initialMax).flatMap { pb =>
input.evalMap(l => pb.stepTo(l))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment