Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
kostaskougios committed Feb 5, 2024
1 parent 84449cc commit a0ea6c8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
48 changes: 48 additions & 0 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ like to learn how to create user interfaces.

This tutorial will use `scala-cli` but the same applies for `sbt` or `mill` projects that use the terminal21 libraries.

All example code is under `example-scripts` of this repo, feel free to check the repo and run them.

## Creating a folder for our scripts

Create a folder and a file `project.scala` into it. This file will help us include the library dependencies and also
Expand Down Expand Up @@ -155,3 +157,49 @@ Finally, when the universe is ready, we just clear the UI and render a paragraph
session.clear()
Paragraph(text = "Universe ready!").render()
```
## Handling clicks

Some UI elements allow us to attach an `onClick` handler. When the user clicks the element, our scala code runs.

Let's see for example [on-click.sc](../example-scripts/on-click.sc). We will create a paragraph and a button. When the
user clicks the button, the paragraph text will change and the script will exit.

```scala
#!/usr/bin/env -S scala-cli project.scala

import org.terminal21.client.*
import org.terminal21.client.components.*
import org.terminal21.client.components.std.*
import org.terminal21.client.components.chakra.*

Sessions.withNewSession("on-click-example", "On Click Handler"): session =>
given ConnectedSession = session

@volatile var exit = false
val msg = Paragraph(text = "Waiting for user to click the button")
val button = Button(text = "Please click me").onClick: () =>
msg.withText("Button clicked.").renderChanges()
exit = true

Seq(msg, button).render()

session.waitTillUserClosesSessionOr(exit)
```

First we create the paragraph and button. We attach an `onClick` handler on the button:

```scala
val button = Button(text = "Please click me").onClick: () =>
msg.withText("Button clicked.").renderChanges()
exit = true
```
Here we change the paragraph text and also update `exit` to `true`.

Our script waits until var `exit` becomes true and then terminates.

```scala
session.waitTillUserClosesSessionOr(exit)
```

Now if we run it with `./on-click.sc` and click the button, the script will terminate.

19 changes: 19 additions & 0 deletions example-scripts/on-click.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env -S scala-cli project.scala

import org.terminal21.client.*
import org.terminal21.client.components.*
import org.terminal21.client.components.std.*
import org.terminal21.client.components.chakra.*

Sessions.withNewSession("on-click-example", "On Click Handler"): session =>
given ConnectedSession = session

@volatile var exit = false
val msg = Paragraph(text = "Waiting for user to click the button")
val button = Button(text = "Please click me").onClick: () =>
msg.withText("Button clicked.").renderChanges()
exit = true

Seq(msg, button).render()

session.waitTillUserClosesSessionOr(exit)

0 comments on commit a0ea6c8

Please sign in to comment.