J.A.R.V.I.S-rust/resources/commands/expenses/today.lua
Bossiara13 87bb186e94
Some checks are pending
Rust CI / cargo test (jarvis-core) (push) Waiting to run
Rust CI / cargo clippy (push) Waiting to run
Rust CI / cargo check (workspace) (push) Waiting to run
feat: Wave 10 — expense tracker pack (voice-driven)
resources/commands/expenses/ (4 commands):
- expenses.log     "потратил 500 на еду"     → append entry
- expenses.today   "сколько потратил сегодня" → today's total
- expenses.week    "сколько на неделе"       → 7-day rolling total
- expenses.breakdown "куда ушли деньги"      → top-5 categories

Storage: `jarvis.state` (auto-namespaced per-pack JSON). Wire format is
compact "ts1,amount1,cat1|ts2,amount2,cat2" so we don't need a JSON parser
inside Lua.

Phrase parsing:
- Trigger stripped via jarvis.text.strip_trigger.
- First number captured; "12,50" comma decimals accepted.
- Category extracted from "на <слово>" (RU) or "for/on <word>" (EN);
  defaults to "разное" / "misc".

Time math:
- Day boundary: local-calendar midnight via os.time({year,month,day,...}).
- Week boundary: rolling 7×86400 seconds.

Pack count: 98 → 99 functional.
2026-05-16 14:57:58 +03:00

36 lines
1.1 KiB
Lua

-- Sum of today's expenses (local calendar day).
local lang = jarvis.context.language
local raw_state = jarvis.state.get("entries") or ""
if raw_state == "" then
return jarvis.cmd.not_found(lang == "ru"
and "Сегодня ничего не тратили, сэр."
or "No expenses logged today, sir.")
end
local now = jarvis.context.time
local day_start = os.time({
year = now.year, month = now.month, day = now.day,
hour = 0, min = 0, sec = 0,
})
local day_end = day_start + 86400
local total = 0
for chunk in raw_state:gmatch("([^|]+)") do
local ts, am = chunk:match("^(%d+),([%-%d%.]+),")
ts = tonumber(ts); am = tonumber(am)
if ts and am and ts >= day_start and ts < day_end then
total = total + am
end
end
if total == 0 then
return jarvis.cmd.ok(lang == "ru"
and "Сегодня ничего не тратили, сэр."
or "Nothing spent today, sir.")
end
return jarvis.cmd.ok(string.format(lang == "ru"
and "Сегодня потратили %.0f."
or "Today's spending: %.0f.", total))