From e16ca0ed125d1857a0713c3bbf176397767b123d Mon Sep 17 00:00:00 2001 From: Kostas Kougios Date: Fri, 5 Apr 2024 16:33:16 +0100 Subject: [PATCH] - --- example-scripts/budget.sc | 36 +++++++++++++++++++++++++++++++++++ example-scripts/project.scala | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100755 example-scripts/budget.sc diff --git a/example-scripts/budget.sc b/example-scripts/budget.sc new file mode 100755 index 0000000..82dc636 --- /dev/null +++ b/example-scripts/budget.sc @@ -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) diff --git a/example-scripts/project.scala b/example-scripts/project.scala index 4cd6873..9ea7cdc 100644 --- a/example-scripts/project.scala +++ b/example-scripts/project.scala @@ -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