Ledger

An app for tracking what comes in and goes out, with a monthly budget, charts, search and undo. 145 lines of Cobalt in one file.

Ledger
The Ledger app running

What it uses

Live values
this_month, money_in, money_out, balance and budget_left are made with <-, so they recalculate by themselves after every change.
Time travel
Adding and deleting replace the whole list with :=, so every change is saved in its history and Undo is just rewind entries.
A watcher
on money_out > settings.budget shows a warning the moment spending goes over the budget, once, not on every redraw.
Charts
A donut chart of spending by category and an area chart of the balance, each one line.
Saved data
load and save keep entries and settings between runs.
Controls
Tabs, inputs, selects, a slider inside a dialog, search and filters, all connected with bind:.

The code

Save it as ledger.cbt and run cobalt ledger.cbt.

View on GitHub
~ Ledger: track what comes in and goes out, with a budget, charts and undo.
lock CATEGORIES := ["Food", "Transport", "Bills", "Shopping", "Fun", "Health", "Salary", "Other"]
lock COLORS := ["#3b82f6", "#06b6d4", "#8b5cf6", "#ec4899", "#f59e0b", "#10b981", "#22c55e", "#94a3b8"]
 
today := date()
entries := load "entries", [
{what: "Salary", amount: 2400, kind: "in", category: "Salary", day: today},
{what: "Rent", amount: 900, kind: "out", category: "Bills", day: today},
{what: "Groceries", amount: 86.4, kind: "out", category: "Food", day: today},
{what: "Train pass", amount: 54, kind: "out", category: "Transport", day: today},
{what: "Cinema", amount: 24, kind: "out", category: "Fun", day: today},
]
settings := load "settings", {currency: "$", budget: 1500}
 
~ The form
what := ""
amount := ""
kind := "out"
category := "Food"
show := "All"
search := ""
edit_budget := false
 
~ Live values: these recalculate by themselves whenever entries change
month := slice(today, 0, 7)
this_month <- filter entries, fx e => slice(e.day, 0, 7) == month
money_in <- sum map(filter(this_month, fx e => e.kind == "in"), fx e => e.amount)
money_out <- sum map(filter(this_month, fx e => e.kind == "out"), fx e => e.amount)
balance <- money_in - money_out
budget_left <- settings.budget - money_out
 
on money_out > settings.budget => toast "You went over this month's budget", "warning"
 
fx money n =>
total := round(abs(n) * 100)
cents := total % 100
"{? n < 0 and total > 0 => "-" | _ => ""}{settings.currency}{int(total / 100)}.{? cents < 10 => "0{cents}" | _ => cents}"
 
fx payments n => ? n == 1 => "1 payment" | _ => "{n} payments"
 
fx save_all =>
save "entries", entries
save "settings", settings
 
fx add_entry =>
value := num amount
? trim(what) == "" or value == nil or value <= 0 =>
toast "Add a name and an amount above 0", "danger"
give
~ `:=` with a new list (not push) so time travel can undo it
entries := entries + [{what: trim(what), amount: value, kind: kind, category: category, day: today}]
what := ""
amount := ""
save_all
focus "what-box"
 
fx undo =>
? len(entries.history) < 2 => give toast "Nothing to undo"
rewind entries
save_all
toast "Undone"
 
fx spent_by_category =>
totals := {}
filter(this_month, fx e => e.kind == "out") each e =>
totals[e.category] := (totals[e.category] ?? 0) + e.amount
give totals
 
fx running_balance =>
points := {}
total := 0
this_month each i, e =>
total += ? e.kind == "in" => e.amount | _ => -e.amount
points["#{i + 1}"] := round(total, 2)
give points
 
fx color_of cat => COLORS[find(0..(len(CATEGORIES) - 1), fx i => CATEGORIES[i] == cat) ?? 7]
 
window "Ledger", 1100x780, theme: "dark", accent: "#3b82f6", max_width: 1060 =>
row =>
column gap: 2 =>
heading "Ledger", 1, size: 30, weight: 800
text "This month: {month}", color: "var(--muted)"
space grow: true
select ["$", "€", "£", "₹", "¥"], bind: settings.currency, width: 80 => save_all
button "Undo", kind: "secondary", icon: "refresh" => undo
 
grid min: 200, gap: 14, reveal: true =>
stat "Balance", money(balance), (? balance >= 0 => "you're in the green" | _ => "spending more than you earn")
stat "In", money(money_in), payments(len(filter(this_month, fx e => e.kind == "in")))
stat "Out", money(money_out), payments(len(filter(this_month, fx e => e.kind == "out")))
card gap: 8, click: (fx => edit_budget := true), cursor: "pointer", hover: {scale: 1.02}, transition: true =>
text "Budget left", color: "var(--muted)", size: 13
title money(budget_left), size: 26, color: (? budget_left < 0 => "#f87171" | _ => "inherit")
progress min(money_out, settings.budget), settings.budget
 
card gap: 12 =>
row wrap: true =>
tabs ["out", "in"], bind: kind, kind: "pills"
input "What was it?", bind: what, id: "what-box", grow: true, enter: (fx t => add_entry())
input "Amount", bind: amount, type: "number", width: 130, enter: (fx t => add_entry())
select CATEGORIES, bind: category, width: 150
button "Add", kind: "primary", icon: "plus" => add_entry
 
row gap: 16, align: "stretch", wrap: true =>
card grow: 1, min_width: 300, animate: "fade-up" =>
subtitle "Where it went"
spent := spent_by_category()
? len(keys(spent)) == 0 => notice "No spending yet this month."
| _ => chart "donut", spent, colors: map(keys(spent), fx c => color_of(c))
card grow: 2, min_width: 380, animate: "fade-up", delay: 0.1 =>
subtitle "Balance over time"
chart "area", running_balance()
 
card gap: 4 =>
row =>
subtitle "History"
space grow: true
tabs ["All", "In", "Out"], bind: show
input "Search...", bind: search, type: "search", width: 200
shown := []
entries each i, e =>
wanted := show ? "In" => e.kind == "in" | "Out" => e.kind == "out" | _ => true
? wanted and contains(lower(e.what), lower(search)) => shown.push i
? len(shown) == 0 => text "Nothing here.", color: "var(--muted)", padding: 16
reverse(shown) each i =>
e := entries[i]
row padding: "10px 12px", radius: 10, hover: {bg: "var(--surface-2)"}, transition: true =>
box width: 10, height: 10, radius: "50%", bg: color_of(e.category)
text e.what, grow: true
badge e.category, kind: "plain"
text e.day, color: "var(--muted)", size: 13
text (? e.kind == "in" => "+" | _ => "−") + money(e.amount), bold: true, width: 110, align: "right",
color: (? e.kind == "in" => "#4ade80" | _ => "inherit")
button "", kind: "ghost", icon: "trash", tooltip: "Delete" =>
entries := filter entries, fx x, j => j != i
save_all
 
dialog "Monthly budget", bind: edit_budget =>
slider min: 100, max: 5000, step: 50, bind: settings.budget
text "Budget: {money(settings.budget)}", bold: true
row justify: "end" =>
button "Done", kind: "primary" =>
edit_budget := false
save_all