Coverage / src/looks.ts 96.0%
72 of 75 lines run, 3 of 3 functions, 20 of 45 branches · run #26 on main · run missed
| 1 | 0× | // How an operation of the op log reads: a friendlier label, its kind, and |
| 2 | 0× | // the codicon and colour the Operations view gives it (see style.ts). |
| 3 | 1× | |
| 4 | export interface OperationLook { | |
| 5 | /** What happened, in words: a commit's subject, "Pushed to earth". */ | |
| 6 | label: string; | |
| 7 | /** The kind, shown beside it: "commit", "push". */ | |
| 8 | kind: string; | |
| 9 | icon: string; | |
| 10 | color: string; | |
| 11 | } | |
| 12 | 1× | |
| 13 | 0× | /** The first line of a quoted `-m` message in an operation's command. */ |
| 14 | 2× | function message(command: string): string | undefined { |
| 15 | 2× | const m = /-m\s+"((?:[^"\\]|\\.)*)"/.exec(command); |
| 16 | 2× | if (!m) return undefined; |
| 17 | 2× | const text = m[1].replace(/\\n/g, "\n").replace(/\\"/g, '"').replace(/\\\\/g, "\\"); |
| 18 | 2× | return text.split("\n")[0].trim() || undefined; |
| 19 | 2× | } |
| 20 | 1× | |
| 21 | 1× | /** How an operation of the op log looks, from its command. */ |
| 22 | 1× | export function operationLook(command: string, snapshot: boolean): OperationLook { |
| 23 | 11× | const cmd = command.replace(/^oxy\s+/, "").replace(/\s*\((?:snapshot|merged with a concurrent operation)\)\s*/g, " ").trim(); |
| 24 | 11× | const words = cmd.split(/\s+/); |
| 25 | 11× | const [verb, sub] = words; |
| 26 | 11× | const arg = (i: number) => (words[i] ?? "").replace(/^"|"$/g, ""); |
| 27 | 11× | if (snapshot) { |
| 28 | 1× | return { label: `Snapshot for ${verb}`, kind: "snapshot", icon: "circle-small", color: "descriptionForeground" }; |
| 29 | 1× | } |
| 30 | 10× | switch (verb) { |
| 31 | 11× | case "commit": |
| 32 | 11× | return { label: message(cmd) ?? "Committed", kind: "commit", icon: "git-commit", color: "oxygen.commit" }; |
| 33 | 11× | case "amend": |
| 34 | 11× | case "split": |
| 35 | 11× | case "squash": |
| 36 | 11× | case "describe": |
| 37 | 11× | return { label: message(cmd) ?? `${verb[0].toUpperCase()}${verb.slice(1)}ed`, kind: verb, icon: "edit", color: "oxygen.commit" }; |
| 38 | 11× | case "push": |
| 39 | 11× | return { label: sub ? `Pushed to ${sub}` : "Pushed", kind: "push", icon: "cloud-upload", color: "oxygen.push" }; |
| 40 | 11× | case "pull": |
| 41 | 11× | return { label: sub ? `Pulled from ${sub}` : "Pulled", kind: "pull", icon: "cloud-download", color: "oxygen.pull" }; |
| 42 | 11× | case "fetch": |
| 43 | 11× | return { label: sub ? `Fetched ${sub}` : "Fetched", kind: "fetch", icon: "sync", color: "oxygen.pull" }; |
| 44 | 11× | case "clone": |
| 45 | 11× | case "init": |
| 46 | 11× | return { label: verb === "init" ? "Repository created" : "Cloned", kind: verb, icon: "repo-clone", color: "oxygen.pull" }; |
| 47 | 11× | case "merge": |
| 48 | 11× | return { label: sub ? `Merged ${sub}` : "Merged", kind: "merge", icon: "git-merge", color: "oxygen.merge" }; |
| 49 | 11× | case "rebase": |
| 50 | 11× | case "resolve": |
| 51 | 11× | return { label: message(cmd) ?? `${verb[0].toUpperCase()}${verb.slice(1)}d`, kind: verb, icon: "git-pull-request", color: "oxygen.merge" }; |
| 52 | 11× | case "checkout": |
| 53 | 11× | return { label: "Checked out", kind: "checkout", icon: "arrow-swap", color: "oxygen.switch" }; |
| 54 | 11× | case "bookmark": |
| 55 | 11× | case "tag": { |
| 56 | 2× | const name = arg(2); |
| 57 | 2× | if (sub === "switch") return { label: `Switched to ${name}`, kind: "switch", icon: "arrow-swap", color: "oxygen.switch" }; |
| 58 | 1× | const done: Record<string, string> = { create: "created", set: "moved", delete: "deleted", move: "moved" }; |
| 59 | 2× | return { label: `${verb === "tag" ? "Tag" : "Bookmark"} ${name} ${done[sub] ?? sub}`.trim(), kind: verb, icon: verb === "tag" ? "tag" : "bookmark", color: "oxygen.bookmark" }; |
| 60 | 2× | } |
| 61 | 11× | case "undo": |
| 62 | 11× | case "discard": |
| 63 | 11× | case "abandon": |
| 64 | 11× | case "restore": |
| 65 | 11× | return { label: `${verb[0].toUpperCase()}${verb.slice(1)}`, kind: verb, icon: "discard", color: "oxygen.undo" }; |
| 66 | 11× | case "seal": |
| 67 | 11× | case "sign": |
| 68 | 11× | case "policy": |
| 69 | 11× | return { label: cmd, kind: verb, icon: "shield", color: "oxygen.key" }; |
| 70 | 11× | case "submodule": |
| 71 | 11× | return { label: `Subrepos ${sub ?? ""}`.trim(), kind: "subrepo", icon: "repo", color: "oxygen.subrepo" }; |
| 72 | 11× | default: |
| 73 | 11× | return { label: cmd, kind: verb ?? "", icon: "circle-outline", color: "descriptionForeground" }; |
| 74 | 11× | } |
| 75 | 11× | } |