# @dapp-format 1
# @id today
# @name Today
# @version 1.0.0
# @boards fnk0104
# @runtime >=1.4.2 <2.0.0
# @echo off
# @summary A pocket dashboard for focus, tasks, service events and device health
# /apps/today.dapp
# Reads Todo's plain-text task file and Control's event log. The focus line is
# kept separately in /apps/today.focus.

SETSTR focus "No focus set"
GOSUB loadfocus

:menu
CLEAR
COLOR pink
PRINT "TODAY"
COLOR cyan
PRINT "Focus: $focus"
COLOR white
PRINT ""
PRINT "Device"
PRINT "  battery $battery%   heap $heap"
PRINT "  wifi $wifi   ip $ip   uptime $seconds seconds"
PRINT ""
COLOR yellow
PRINT "Tasks"
COLOR white
GOSUB tasks
PRINT ""
COLOR yellow
PRINT "Recent service events"
COLOR white
GOSUB events
PRINT ""
PRINT "f focus   r refresh   q quit"
INPUT choice "today> "
IFEQ $choice "f" GOTO setfocus
IFEQ $choice "r" GOTO menu
IFEQ $choice "q" GOTO leave
GOTO menu

:setfocus
INPUT newfocus "focus> "
LEN n $newfocus
IF $n = 0 GOTO menu
SETSTR focus $newfocus
FOPEN "/apps/today.focus" write
IF $fok = 0 GOTO menu
FWRITE "$focus"
FCLOSE
GOTO menu

:tasks
FEXISTS have "/apps/todo.txt"
IF $have = 0 GOTO notasks
FOPEN "/apps/todo.txt" read
IF $fok = 0 GOTO notasks
SET count 0
:taskloop
FREAD line
IF $feof = 1 GOTO taskdone
PRINT "  $line"
ADD count 1
IF $count < 6 GOTO taskloop
PRINT "  ..."
:taskdone
FCLOSE
IF $count > 0 GOTO tasksdone
:notasks
FCLOSE
PRINT "  (none; run todo to add tasks)"
:tasksdone
RETURN

:events
FEXISTS have "/apps/control.log"
IF $have = 0 GOTO noevents
FOPEN "/apps/control.log" read
IF $fok = 0 GOTO noevents
SET count 0
:eventloop
FREAD line
IF $feof = 1 GOTO eventdone
PRINT "  $line"
ADD count 1
IF $count < 4 GOTO eventloop
PRINT "  ..."
:eventdone
FCLOSE
IF $count > 0 GOTO eventsdone
:noevents
FCLOSE
PRINT "  (none; run control to check services)"
:eventsdone
RETURN

:loadfocus
FEXISTS have "/apps/today.focus"
IF $have = 0 GOTO lf_done
FOPEN "/apps/today.focus" read
IF $fok = 0 GOTO lf_done
FREAD focus
FCLOSE
:lf_done
RETURN

:leave
PRINT "today: done"
EXIT
