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