-
Notifications
You must be signed in to change notification settings - Fork 0
/
progress.sc
executable file
·49 lines (41 loc) · 1.71 KB
/
progress.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env -S scala-cli project.scala
// ------------------------------------------------------------------------------
// Universe creation progress bar demo
// Run with ./progress.sc
// ------------------------------------------------------------------------------
import org.terminal21.client.{*, given}
import org.terminal21.client.components.*
import org.terminal21.client.components.std.*
import org.terminal21.client.components.chakra.*
import org.terminal21.model.{ClientEvent, SessionOptions}
Sessions
.withNewSession("universe-generation", "Universe Generation Progress")
.connect: session =>
given ConnectedSession = session
def components(model: Int, events: Events): MV[Int] =
val status =
if model < 10 then "Generating universe ..."
else if model < 30 then "Creating atoms"
else if model < 50 then "Big bang!"
else if model < 80 then "Inflating"
else "Life evolution"
val msg = Paragraph(text = status)
val progress = Progress(value = model)
MV(
model + 1,
Seq(msg, progress)
)
// send a ticker to update the progress bar
object Ticker extends ClientEvent
fiberExecutor.submit:
for _ <- 1 to 100 do
Thread.sleep(200)
session.fireEvent(Ticker)
Controller(components)
.render(1) // render takes the initial model value, in this case our model is the progress as an Int between 0 and 100. We start with 1 and increment it in the components function
.iterator
.takeWhile(_.model < 100) // terminate when model == 100
.foreach(_ => ()) // and run it
// clear UI
session.render(Seq(Paragraph(text = "Universe ready!")))
session.leaveSessionOpenAfterExiting()