Make habit Entry toStringable

This commit is contained in:
2022-08-29 20:35:42 +00:00
parent ffd8cae013
commit e60115818a
4 changed files with 98 additions and 92 deletions

View File

@@ -0,0 +1,8 @@
package;
import kiss.Prelude;
import kiss.List;
import HabitModel;
@:build(kiss.Kiss.build())
class Entry {}

View File

@@ -0,0 +1,26 @@
(defNew [&prop &mut :EntryType type
&prop :Array<EntryLabel> labels])
(method toString []
"$(case type
((Daily days lastDayDone)
(+
(.join (for day days
(case day
(0 "Su")
(1 "M")
(2 "T")
(3 "W")
(4 "Th")
(5 "F")
(6 "S")
(otherwise (throw "bad day")))) "")
" "
lastDayDone
": "))
((Monthly days lastDayDone)
"$(days.join ",") ${lastDayDone}: ")
((Interval days lastDayDone)
"$days ${lastDayDone}: ")
(otherwise ""))$(.join (for label labels
"${label.label} $(* "|" label.points)") "/")")

View File

@@ -23,11 +23,6 @@ typedef EntryLabel = {
points:Int points:Int
}; };
typedef Entry = {
type: EntryType,
labels: Array<EntryLabel>,
};
typedef RewardFile = { typedef RewardFile = {
path: String, path: String,
startingPoints: Int, startingPoints: Int,

View File

@@ -49,69 +49,67 @@
("BONUS" bonusEntries) ("BONUS" bonusEntries)
("TODO" todoEntries) ("TODO" todoEntries)
(otherwise (throw "bad header"))) (otherwise (throw "bad header")))
(object (new Entry
type (case lastHeader
(case lastHeader ("BONUS" Bonus)
("BONUS" Bonus) ("TODO" Todo)
("TODO" Todo) ("DAILY"
("DAILY" (case (line.split ":")
(case (line.split ":") ([noColon]
([noColon] (Daily
(Daily // all days of week
// all days of week (collect (range 7))
(collect (range 7)) // never done before
// never done before ""))
"")) ([::&mut preColon ...afterColon]
([::&mut preColon ...afterColon] (set line (afterColon.join ":"))
(set line (afterColon.join ":")) (Daily
(Daily // Days of week specified by abbreviation:
// Days of week specified by abbreviation: (sort (filter
(sort (filter [
[ // disambiguate Th from T and Su from S:
// disambiguate Th from T and Su from S: (when (contains preColon "Th") {(set preColon (StringTools.replace preColon "Th" "")) 4})
(when (contains preColon "Th") {(set preColon (StringTools.replace preColon "Th" "")) 4}) (when (contains preColon "Su") {(set preColon (StringTools.replace preColon "Su" "")) 0})
(when (contains preColon "Su") {(set preColon (StringTools.replace preColon "Su" "")) 0}) (when (contains preColon "M") 1)
(when (contains preColon "M") 1) (when (contains preColon "T") 2)
(when (contains preColon "T") 2) (when (contains preColon "W") 3)
(when (contains preColon "W") 3) (when (contains preColon "F") 5)
(when (contains preColon "F") 5) (when (contains preColon "S") 6)
(when (contains preColon "S") 6)
]))
])) // Last date completed after that:
// Last date completed after that: (ifLet [[days date] (preColon.split " ")]
(ifLet [[days date] (preColon.split " ")] date
date "")))
""))) (otherwise (throw "bad line"))))
(otherwise (throw "bad line")))) ("MONTHLY"
("MONTHLY" (case (line.split ": ")
(case (line.split ": ") ([::&mut preColon ...afterColon]
([::&mut preColon ...afterColon] (set line (afterColon.join ": "))
(set line (afterColon.join ": ")) (Monthly
(Monthly // Days of month can be positive (1-31) or negative (-1 to -31)
// Days of month can be positive (1-31) or negative (-1 to -31) (map (.split (first (preColon.split " ")) ",") Std.parseInt)
(map (.split (first (preColon.split " ")) ",") Std.parseInt) // Last date completed after that:
// Last date completed after that: (ifLet [[::days ...date] (preColon.split " ")]
(ifLet [[::days ...date] (preColon.split " ")] (date.join " ")
(date.join " ") "")))
""))) (otherwise (throw "bad line"))))
(otherwise (throw "bad line")))) ("INTERVAL"
("INTERVAL" (case (line.split ": ")
(case (line.split ": ") ([::&mut preColon ...afterColon]
([::&mut preColon ...afterColon] (set line (afterColon.join ": "))
(set line (afterColon.join ": ")) (case (preColon.split " ")
(case (preColon.split " ") ([days]
([days] (Interval (Std.parseInt days) ""))
(Interval (Std.parseInt days) "")) ([::days ...lastDayDone]
([::days ...lastDayDone] (Interval (Std.parseInt days) (lastDayDone.join " ")))
(Interval (Std.parseInt days) (lastDayDone.join " "))) (otherwise (throw "bad interval habit: $line"))))
(otherwise (throw "bad interval habit: $line")))) (otherwise (throw "bad interval habit: $line"))))
(otherwise (throw "bad interval habit: $line")))) (otherwise (throw "bad header: $lastHeader")))
(otherwise (throw "bad header: $lastHeader"))) (for l (line.split "/")
labels (object
(for l (line.split "/") label (StringTools.trim (StringTools.replace l "|" ""))
(object points (count (l.split "") ->c (= c "|")))))))
label (StringTools.trim (StringTools.replace l "|" ""))
points (count (l.split "") ->c (= c "|")))))))
(otherwise (break)))))) (otherwise (break))))))
(method :Array<Entry> allEntries [] (method :Array<Entry> allEntries []
@@ -120,29 +118,8 @@
(method :Int totalPoints [] (method :Int totalPoints []
(apply + (for l (flatten (for e (allEntries) e.labels)) l.points))) (apply + (for l (flatten (for e (allEntries) e.labels)) l.points)))
(function :String stringify [:Entry e] (function stringify [:Entry e]
"$(case e.type (e.toString))
((Daily days lastDayDone)
(+
(.join (for day days
(case day
(0 "Su")
(1 "M")
(2 "T")
(3 "W")
(4 "Th")
(5 "F")
(6 "S")
(otherwise (throw "bad day")))) "")
" "
lastDayDone
": "))
((Monthly days lastDayDone)
"$(days.join ",") ${lastDayDone}: ")
((Interval days lastDayDone)
"$days ${lastDayDone}: ")
(otherwise ""))$(.join (for label e.labels
"${label.label} $(* "|" label.points)") "/")")
(function :String stringifyRewardFile [:RewardFile rewardFile] (function :String stringifyRewardFile [:RewardFile rewardFile]
"${rewardFile.path} ${rewardFile.startingPoints} ${rewardFile.puzzleWidth} ${rewardFile.puzzleHeight} ${rewardFile.piecesPerPoint} ${rewardFile.skipped}") "${rewardFile.path} ${rewardFile.startingPoints} ${rewardFile.puzzleWidth} ${rewardFile.puzzleHeight} ${rewardFile.piecesPerPoint} ${rewardFile.skipped}")
@@ -223,7 +200,7 @@
((Daily _ _) dailyEntries) ((Daily _ _) dailyEntries)
((Monthly _ _) monthlyEntries) ((Monthly _ _) monthlyEntries)
(otherwise (throw ""))) (otherwise (throw "")))
(objectWith [labels (for label labels (objectWith [points 0] label))] type)) (new Entry type (for label labels (objectWith [points 0] label))))
(save)) (save))
(method addRewardFile [path startingPoints puzzleWidth puzzleHeight piecesPerPoint] (method addRewardFile [path startingPoints puzzleWidth puzzleHeight piecesPerPoint]