Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
kostaskougios committed Apr 5, 2024
1 parent 18a4b2b commit e16ca0e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
36 changes: 36 additions & 0 deletions example-scripts/budget.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env -S scala-cli

//> using file project.scala
import org.terminal21.client.components.chakra.QuickTable
import java.time.LocalDate

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

Sessions
.withNewSession("budget", "Personal Budget Calculator")
.connect: session =>
given ConnectedSession = session

println(new BudgetPage(BudgetForm()).run)

case class BudgetForm(startDate: LocalDate = LocalDate.of(2024, 4, 1), initialBudget: Int = 1000, percentIncreasePerYear: Float = 4f / 100f)

class BudgetPage(initialForm: BudgetForm)(using ConnectedSession):
def run: Option[BudgetForm] =
controller.render(initialForm).run()

def components(form: BudgetForm, events: Events): MV[BudgetForm] =

case class Row(date: LocalDate, budget: Float, total: Float)
val rows = (1 to 30 * 12).foldLeft(Seq.empty[Row]): (rows, month) =>
val date = form.startDate.plusMonths(month)
val budget = form.initialBudget + form.initialBudget * ((month / 12) * form.percentIncreasePerYear)
val total = rows.map(_.budget).sum + budget
rows :+ Row(date, budget, total)
val table = QuickTable().withHeaders("Date", "Budget", "Total").withRows(rows.map(r => Seq(r.date, r.budget, r.total)))

MV(form, table)

def controller: Controller[BudgetForm] = Controller(components)
2 changes: 1 addition & 1 deletion example-scripts/project.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
//> using dep io.github.kostaskougios::terminal21-nivo:0.30
//> using dep io.github.kostaskougios::terminal21-mathjax:0.30

//> using dep commons-io:commons-io:2.15.1
//> using dep commons-io:commons-io:2.16.0

//> using javaOpt -Xmx128m

0 comments on commit e16ca0e

Please sign in to comment.