code wiki / (root) / nx_climbout_progress.nx

nx_climbout_progress.nx source

↩ module page · 88 lines · 3253 B

1// nx_climbout_progress.nx -- R7 of THE NISHI SITUATION-MANAGEMENT arc: TRACK THE CLIMB. As a person 2// completes climb-out steps (pay the overdue bill, file the dispute, refinance, ...), record it so the 3// system shows PROGRESS toward financial peace and surfaces the next step. The "climb out to happiness" 4// made visible -- and especially motivating for ADHD (the done step disappears, progress is concrete, only 5// the next one shows). The done-state is a sovereign content-addressed record per respondent: a string of 6// '0'/'1', one char per step, in the seg-store keyed "progress:<respondent>" (last-wins). No bit-shifts, 7// no floats, no hardware writes. license_tier: ORIGINAL 8import "nx_syscalls.nx" 9import "nx_seg_store.nx" 10 11func cp_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 12 13// build "progress:<respondent>" into out (NUL-terminated); returns length. 14func cp_key(respondent: *u8, out: *u8) -> i64 { 15 var o: i64 = 0 16 let p: *u8 = "progress:\x00" as *u8 17 var i: i64 = 0 18 while p[i] != (0 as u8) { out[o] = p[i]; o = o + 1; i = i + 1 } 19 i = 0 20 while respondent[i] != (0 as u8) { out[o] = respondent[i]; o = o + 1; i = i + 1 } 21 out[o] = 0 as u8 22 return o 23} 24 25// read the done-string for a respondent into out (n chars, '0' default + NUL at n). returns 0. 26func cp_get(prefix: *u8, respondent: *u8, out: *u8, n: i64) -> i64 { 27 var i: i64 = 0 28 while i < n { out[i] = 0x30 as u8; i = i + 1 } 29 out[n] = 0 as u8 30 let key: *u8 = sys_mmap(256) 31 cp_key(respondent, key) 32 let h: *i64 = ss_open(prefix) 33 if (h as i64) == 0 { return 0 } 34 let pp: *i64 = sys_mmap(16) as *i64 35 let ll: *i64 = sys_mmap(16) as *i64 36 if ss_hget(h, key, pp, ll) == 1 { 37 let src: *u8 = pp[0] as *u8 38 let sn: i64 = ll[0] 39 i = 0 40 while i < sn { if i < n { out[i] = src[i] } i = i + 1 } 41 } 42 return 0 43} 44 45// mark step `step` (0-based) done for a respondent over an n-step plan. read-modify-write. returns 0 ok. 46func cp_mark(prefix: *u8, respondent: *u8, step: i64, n: i64) -> i64 { 47 let buf: *u8 = sys_mmap(128) 48 cp_get(prefix, respondent, buf, n) 49 if step >= 0 { if step < n { buf[step] = 0x31 as u8 } } 50 buf[n] = 0 as u8 51 let key: *u8 = sys_mmap(256) 52 cp_key(respondent, key) 53 let w: *i64 = ss_begin() 54 if ss_add(w, 1, key, buf, n + 1) != 0 { return 0 - 1 } 55 if ss_commit(prefix, w, sys_now_us()) != 0 { return 0 - 2 } 56 return 0 57} 58 59// count completed steps in a done-string. 60func cp_count(donestr: *u8, n: i64) -> i64 { 61 var c: i64 = 0 62 var i: i64 = 0 63 while i < n { if donestr[i] == (0x31 as u8) { c = c + 1 } i = i + 1 } 64 return c 65} 66 67// percent complete (integer). 68func cp_pct(done_count: i64, n: i64) -> i64 { 69 if n <= 0 { return 0 } 70 return done_count * 100 / n 71} 72 73// is step `step` done? 74func cp_is_done(donestr: *u8, step: i64) -> i64 { 75 if donestr[step] == (0x31 as u8) { return 1 } 76 return 0 77} 78 79// the next step to do = first not-done step in plan order. -1 if all done. 80func cp_next(donestr: *u8, order: *i64, n: i64) -> i64 { 81 var i: i64 = 0 82 while i < n { 83 let a: i64 = order[i] 84 if donestr[a] == (0x30 as u8) { return a } 85 i = i + 1 86 } 87 return 0 - 1 88}