Skip to content

Commit 10eaab0

Browse files
authored
Merge pull request #62 from ASCIT31/fix/tui-80col-legend
fix(explorer): readable tabs at 80 columns, scrollable legend
2 parents 129501e + 8ebff39 commit 10eaab0

3 files changed

Lines changed: 181 additions & 32 deletions

File tree

‎conf/plugins/darkmoon-explorer/core.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,3 +497,61 @@ describe("maintenance windows (L9)", () => {
497497
expect(maintenanceLines(undefined, "Lab")[0]).toContain("not available")
498498
})
499499
})
500+
501+
describe("top tabs and scrollable dialogs", () => {
502+
const core = require("./core")
503+
const TABS = [
504+
{ label: "Workspace", short: "Work" },
505+
{ label: "Projects", short: "Proj" },
506+
{ label: "Campaigns", short: "Camp" },
507+
{ label: "Findings", short: "Find" },
508+
{ label: "Plan", short: "Plan" },
509+
{ label: "Attack paths", short: "Paths" },
510+
{ label: "Reports", short: "Docs" },
511+
]
512+
test("80 columns: every tab shows a label and the bar leaves room for the status", () => {
513+
for (const w of [80, 90, 99]) {
514+
const bar = core.topTabs(TABS, w, 2)
515+
expect(bar.mode).toBe("narrow")
516+
expect(bar.tabs.map((t) => t.text)).toEqual(["1 Work", "2 Proj", "3 Camp", "4 Find", "5 Plan", "6 Path", "7 Docs"])
517+
expect(bar.tabs.filter((t) => t.active).map((t) => t.text)).toEqual(["3 Camp"])
518+
// DARKMOON + gaps + paddings = 17 cells, the status keeps at least 10
519+
expect(17 + bar.total + 10).toBeLessThanOrEqual(80)
520+
}
521+
})
522+
test("120 and 160 columns keep the short and full labels", () => {
523+
const b120 = core.topTabs(TABS, 120, 0)
524+
expect(b120.mode).toBe("short")
525+
expect(b120.tabs.map((t) => t.text)).toEqual(["1 Work", "2 Proj", "3 Camp", "4 Find", "5 Plan", "6 Paths", "7 Docs"])
526+
expect(b120.tabs.every((t) => t.padL === 1 && t.padR === 1)).toBe(true)
527+
const b160 = core.topTabs(TABS, 160, 0)
528+
expect(b160.mode).toBe("full")
529+
expect(b160.tabs[5].text).toBe("6 Attack paths")
530+
expect(core.topTabs(TABS, 79, 0)).toEqual({ tabs: [], total: 0, mode: "none" })
531+
})
532+
test("wrapDialogLines keeps blank lines and indents continuations", () => {
533+
const out = core.wrapDialogLines(["A", "", " aaa bbb ccc ddd"], 10)
534+
expect(out).toEqual(["A", "", " aaa bbb", " ccc ddd"])
535+
expect(out.every((l) => l.length <= 10)).toBe(true)
536+
expect(core.wrapDialogLines(["x".repeat(30)], 10)).toEqual(["xxxxxxxxx…"])
537+
})
538+
test("scroll window and keys: clamped, page = height - 1, home/end", () => {
539+
expect(core.scrollWindow(30, 10, 0)).toEqual({ offset: 0, max: 20, above: 0, below: 20 })
540+
expect(core.scrollWindow(30, 10, 99)).toEqual({ offset: 20, max: 20, above: 20, below: 0 })
541+
expect(core.scrollWindow(5, 10, 3)).toEqual({ offset: 0, max: 0, above: 0, below: 0 })
542+
expect(core.scrollKey("down", 30, 10, 0)).toBe(1)
543+
expect(core.scrollKey("up", 30, 10, 0)).toBe(0)
544+
expect(core.scrollKey("pagedown", 30, 10, 0)).toBe(9)
545+
expect(core.scrollKey("pagedown", 30, 10, 15)).toBe(20)
546+
expect(core.scrollKey("pageup", 30, 10, 5)).toBe(0)
547+
expect(core.scrollKey("end", 30, 10, 0)).toBe(20)
548+
expect(core.scrollKey("home", 30, 10, 20)).toBe(0)
549+
expect(core.scrollKey("x", 30, 10, 0)).toBeUndefined()
550+
})
551+
test("the full legend scrolls in a short terminal instead of being cut", () => {
552+
const rows = core.wrapDialogLines(core.legendLines(undefined), 56)
553+
const win = core.scrollWindow(rows.length, 8, 0)
554+
expect(win.below).toBeGreaterThan(0)
555+
expect(rows.slice(core.scrollKey("end", rows.length, 8, 0)!).join("\n")).toContain("CYBER SCORE")
556+
})
557+
})

‎conf/plugins/darkmoon-explorer/core.ts‎

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ export function wrapLines(text: string, width: number, max: number): string[] {
10281028
}
10291029

10301030
/** The legend dialog text: shared words (exec_core.legend) + how the terminal draws them. ``compact``: labels only,
1031-
* for short terminals (the dialog cannot scroll). */
1031+
* for a short summary (the dialog itself scrolls the full text). */
10321032
export function legendLines(l: Legend | undefined, unicode = true, compact = false): string[] {
10331033
const g = unicode ? { solid: "│", dashed: "┆", node: "●", open: "○" } : { solid: "|", dashed: ":", node: "*", open: "o" }
10341034
const pk = l?.path_kind ?? {}
@@ -1078,3 +1078,74 @@ export function legendLines(l: Legend | undefined, unicode = true, compact = fal
10781078
...(["rated", "limited", "not_rated"] as const).map((k) => line(" ", ss[k], k)),
10791079
]
10801080
}
1081+
1082+
/** Top section bar: which label each tab shows at a terminal width. ≥ 130 full labels, 100–129 short labels,
1083+
* 80–99 four-letter labels with a one-cell gap (« 1 Work 2 Proj … »), so every tab stays readable at 80 columns;
1084+
* below 80 no tabs. ``width`` per tab = cells drawn (padding included); ``total`` = their sum. */
1085+
export function topTabs<T extends { label: string; short: string }>(
1086+
tabs: T[],
1087+
width: number,
1088+
active: number,
1089+
): { tabs: { tab: T; text: string; padL: number; padR: number; width: number; active: boolean }[]; total: number; mode: "full" | "short" | "narrow" | "none" } {
1090+
const mode = width < 80 ? "none" : width < 100 ? "narrow" : width < 130 ? "short" : "full"
1091+
if (mode === "none") return { tabs: [], total: 0, mode }
1092+
const out = tabs.map((tab, i) => {
1093+
const on = i === active
1094+
const name = mode === "full" ? tab.label : mode === "short" ? tab.short : tab.short.slice(0, 4)
1095+
const text = `${i + 1} ${name}`
1096+
const padL = 1
1097+
const padR = mode === "narrow" && !on ? 0 : 1
1098+
return { tab, text, padL, padR, width: padL + text.length + padR, active: on }
1099+
})
1100+
return { tabs: out, total: out.reduce((n, t) => n + t.width, 0), mode }
1101+
}
1102+
1103+
/** Wrap dialog lines to ``width`` cells, keeping blank lines and the leading indent of each line; a continuation
1104+
* line is indented two cells more. A word longer than the width is cut with « … ». */
1105+
export function wrapDialogLines(lines: string[], width: number): string[] {
1106+
const w = Math.max(8, width)
1107+
const out: string[] = []
1108+
for (const raw of lines) {
1109+
const s = clean(raw, 4000)
1110+
if (s.length <= w) {
1111+
out.push(s)
1112+
continue
1113+
}
1114+
const lead = s.match(/^ */)?.[0] ?? ""
1115+
const cont = " ".repeat(Math.min(4, lead.length + 2))
1116+
let line = ""
1117+
let start = lead
1118+
for (const word of s.trim().split(/\s+/)) {
1119+
const next = line ? `${line} ${word}` : start + word
1120+
if (next.length <= w) {
1121+
line = next
1122+
continue
1123+
}
1124+
if (line) out.push(line)
1125+
line = (line ? cont : start) + word
1126+
start = cont
1127+
if (line.length > w) line = line.slice(0, w - 1) + "…"
1128+
}
1129+
if (line) out.push(line)
1130+
}
1131+
return out
1132+
}
1133+
1134+
/** A scrollable window of ``total`` rows, ``height`` visible: clamps the offset and says what is hidden. */
1135+
export function scrollWindow(total: number, height: number, offset: number): { offset: number; max: number; above: number; below: number } {
1136+
const h = Math.max(1, height)
1137+
const max = Math.max(0, total - h)
1138+
const o = Math.min(max, Math.max(0, Math.floor(offset)))
1139+
return { offset: o, max, above: o, below: Math.max(0, total - h - o) }
1140+
}
1141+
1142+
/** Offset after a scroll key (up/down/pageup/pagedown/home/end, also j/k); ``undefined`` for any other key. */
1143+
export function scrollKey(key: string, total: number, height: number, offset: number): number | undefined {
1144+
const h = Math.max(1, height)
1145+
const page = Math.max(1, h - 1)
1146+
const d: Record<string, number> = { up: -1, k: -1, down: 1, j: 1, pageup: -page, pagedown: page, home: -Infinity, end: Infinity }
1147+
if (!(key in d)) return undefined
1148+
const max = Math.max(0, total - h)
1149+
const step = d[key]
1150+
return step === -Infinity ? 0 : step === Infinity ? max : scrollWindow(total, h, offset + step).offset
1151+
}

‎conf/plugins/darkmoon-explorer/index.tsx‎

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ import {
2424
retestNote,
2525
riskAcceptanceText,
2626
legendLines,
27+
scrollKey,
28+
scrollWindow,
29+
topTabs,
30+
wrapDialogLines,
2731
maintenanceLines,
2832
pathKindCounts,
2933
profileValue,
@@ -966,23 +970,45 @@ function Explorer(props: { api: TuiPluginApi; src: CachedSource; close: () => vo
966970
setLegendDoc(Object.values(execDocs()).find((x) => x.doc?.legend)?.doc?.legend as Legend | undefined)
967971
}
968972
}
969-
api.ui.dialog.replace(() => (
970-
<api.ui.DialogAlert
971-
title="Legend · Darkmoon vocabulary"
972-
message={legendText()}
973-
onConfirm={() => api.ui.dialog.clear()}
974-
/>
975-
))
973+
api.ui.dialog.replace(() => <LegendDialog />)
976974
api.ui.dialog.setSize(legendSize())
977975
}
978-
// the dialog cannot scroll: the full legend (with meanings) only when it fits, else the labels only
979976
const legendSize = () => (W() >= 120 ? "xlarge" : W() >= 92 ? "large" : "medium")
980-
function legendText() {
981-
const inner = ({ xlarge: 116, large: 88, medium: 60 } as const)[legendSize()] - 6
982-
const tail = ["", "The same words on the dashboard, in the report and here."]
983-
const full = [...legendLines(legendDoc(), G.mark === "■"), ...tail]
984-
const rows = full.reduce((n, l) => n + Math.max(1, Math.ceil(l.length / inner)), 0)
985-
return (rows <= dims().height - 9 ? full : [...legendLines(legendDoc(), G.mark === "■", true), ...tail]).join("\n")
977+
// the full legend (with meanings), scrolled with ↑↓ PgUp PgDn Home End when taller than the terminal
978+
const LegendDialog = () => {
979+
const [off, setOff] = createSignal(0)
980+
const inner = () => Math.min(({ xlarge: 116, large: 88, medium: 60 } as const)[legendSize()], W() - 2) - 4
981+
const rows = createMemo(() =>
982+
wrapDialogLines([...legendLines(legendDoc(), G.mark === "■"), "", "The same words on the dashboard, in the report and here."], inner()),
983+
)
984+
// the dialog opens a quarter down the screen; title, gap, hint and paddings take 6 rows
985+
const height = () => Math.max(3, dims().height - Math.floor(dims().height / 4) - 6)
986+
const win = createMemo(() => scrollWindow(rows().length, height(), off()))
987+
useKeyboard((evt) => {
988+
if (evt.ctrl || evt.meta) return
989+
if (evt.name === "return") return evt.preventDefault(), evt.stopPropagation(), api.ui.dialog.clear()
990+
const next = scrollKey(evt.name, rows().length, height(), win().offset)
991+
if (next === undefined) return
992+
evt.preventDefault()
993+
evt.stopPropagation()
994+
setOff(next)
995+
})
996+
const uni = G.mark === "■"
997+
return (
998+
<box paddingLeft={2} paddingRight={2} paddingBottom={1}>
999+
<box flexDirection="row" justifyContent="space-between" height={1}>
1000+
<T bold>Legend · Darkmoon vocabulary</T>
1001+
<T fg={P().off}>esc</T>
1002+
</box>
1003+
<box height={1} />
1004+
<text fg={C(P().ink2)} wrapMode="none">{rows().slice(win().offset, win().offset + height()).join("\n")}</text>
1005+
<box flexDirection="row" height={1} marginTop={1}>
1006+
<T fg={P().jordy}>{win().below ? `more ${uni ? "↓" : "v"} (${win().below})` : win().above ? "end" : ""}</T>
1007+
<box flexGrow={1} />
1008+
<T fg={P().off}>{win().max ? `${uni ? "↑↓" : "up/down"} PgUp PgDn Home End · enter close` : "enter close"}</T>
1009+
</box>
1010+
</box>
1011+
)
9861012
}
9871013
function quick(target: "findings" | "report" | "paths") {
9881014
const c = contextCampaign()
@@ -1168,28 +1194,22 @@ function Explorer(props: { api: TuiPluginApi; src: CachedSource; close: () => vo
11681194
// ---- chrome: section bar, breadcrumb, key hints
11691195

11701196
const TopBar = () => {
1171-
const short = () => W() < 130
1172-
const digitsOnly = () => W() < 100
1197+
// full labels ≥ 130, short 100–129, four-letter labels 80–99 (every tab readable at 80 columns), none below
1198+
const bar = createMemo(() => topTabs(TABS, W(), TABS.findIndex((t) => t.id === tab())))
11731199
// the tabs never shrink: a long status (an error) is cut instead
1174-
const tabsW = () =>
1175-
view().k === "login" || W() < 80 ? 0 : TABS.reduce((n, t) => n + (digitsOnly() && tab() !== t.id ? 3 : 4 + (short() ? t.short : t.label).length), 0)
1200+
const tabsW = () => (view().k === "login" ? 0 : bar().total)
11761201
return (
11771202
<box flexDirection="row" height={1} backgroundColor={C(P().surface)} paddingLeft={2} paddingRight={2}>
11781203
<T fg={P().alice} bold>DARKMOON</T>
11791204
<box width={3} flexShrink={0} />
1180-
<Show when={view().k !== "login" && W() >= 80}>
1181-
<For each={TABS}>
1182-
{(t, i) => {
1183-
const active = () => tab() === t.id
1184-
return (
1185-
<box flexDirection="row" flexShrink={0} backgroundColor={active() ? C(P().hover) : undefined} paddingLeft={1} paddingRight={1}>
1186-
<T fg={active() ? P().jordy : P().off}>{`${i() + 1}`}</T>
1187-
<Show when={active() || !digitsOnly()}>
1188-
<T fg={active() ? P().ink : P().ink3} bold={active()}>{` ${short() ? t.short : t.label}`}</T>
1189-
</Show>
1190-
</box>
1191-
)
1192-
}}
1205+
<Show when={view().k !== "login" && bar().mode !== "none"}>
1206+
<For each={bar().tabs}>
1207+
{(t) => (
1208+
<box flexDirection="row" flexShrink={0} backgroundColor={t.active ? C(P().hover) : undefined} paddingLeft={t.padL} paddingRight={t.padR}>
1209+
<T fg={t.active ? P().jordy : P().off}>{t.text.slice(0, t.text.indexOf(" "))}</T>
1210+
<T fg={t.active ? P().ink : P().ink3} bold={t.active}>{t.text.slice(t.text.indexOf(" "))}</T>
1211+
</box>
1212+
)}
11931213
</For>
11941214
</Show>
11951215
<box flexGrow={1} />

0 commit comments

Comments
 (0)