73e26b1ee07b9f242929894c7a20b3584b350c4b

Author
TheEdgeOfRage <git@theedgeofrage.com>
Committer
TheEdgeOfRage <git@theedgeofrage.com>
Date

Message

Add modal for herdr workespace creation

Diff

This diff is truncated to protect this page.

  1diff --git a/dot_config/herdr/config.toml b/dot_config/herdr/config.toml
  2index 329af92bdcf140db4c0feb1930fe2f5d27787f67..1012d9a3fc8067502149b51b9754a9e1ddd4baf5 100644
  3--- a/dot_config/herdr/config.toml
  4+++ b/dot_config/herdr/config.toml
  5@@ -9,7 +9,7 @@ rename_tab = "prefix+ctrl+t"
  6 next_tab = "alt+i"
  7 previous_tab = "alt+h"
  8 
  9-new_workspace = "prefix+c"
 10+new_workspace = ""
 11 rename_workspace = "prefix+ctrl+c"
 12 previous_workspace = "alt+e"
 13 next_workspace = "alt+n"
 14@@ -42,3 +42,11 @@ enabled = false
 15 
 16 [worktrees]
 17 directory = "~/dev/dune/_worktrees/"
 18+
 19+[[keys.command]]
 20+key = "prefix+c"
 21+type = "popup"
 22+command = "exec \"$HOME/.local/bin/herdr-new-pi-workspace\""
 23+description = "new Pi workspace"
 24+width = "80%"
 25+height = 12
 26diff --git a/dot_local/bin/executable_herdr-new-pi-workspace b/dot_local/bin/executable_herdr-new-pi-workspace
 27new file mode 100644
 28index 0000000000000000000000000000000000000000..7159e5eadfd7b847163fc6c1983a1d8a7fc8c764
 29--- /dev/null
 30+++ b/dot_local/bin/executable_herdr-new-pi-workspace
 31@@ -0,0 +1,261 @@
 32+#!/usr/bin/env node
 33+'use strict';
 34+
 35+const fs = require('node:fs/promises');
 36+const os = require('node:os');
 37+const path = require('node:path');
 38+const { randomBytes } = require('node:crypto');
 39+const { spawn, spawnSync } = require('node:child_process');
 40+const readline = require('node:readline/promises');
 41+const process = require('node:process');
 42+
 43+let prompt;
 44+
 45+class CommandFailure extends Error {
 46+  constructor(command, args, result) {
 47+    const stderr = result.stderr?.trim() || 'No stderr output.';
 48+    super(`Command failed: ${formatCommand(command, args)}\n${stderr}`);
 49+  }
 50+}
 51+
 52+function formatCommand(command, args) {
 53+  return [command, ...args].map((value) => JSON.stringify(value)).join(' ');
 54+}
 55+
 56+function run(command, args) {
 57+  const result = spawnSync(command, args, {
 58+    encoding: 'utf8',
 59+    env: process.env,
 60+  });
 61+
 62+  if (result.error) {
 63+    throw new Error(`Cannot run ${JSON.stringify(command)}: ${result.error.message}`);
 64+  }
 65+
 66+  if (result.status !== 0) {
 67+    throw new CommandFailure(command, args, result);
 68+  }
 69+
 70+  return result;
 71+}
 72+
 73+function findHerdr() {
 74+  const candidates = [];
 75+  const configuredPath = process.env.HERDR_BIN_PATH?.trim();
 76+
 77+  if (configuredPath) {
 78+    candidates.push(configuredPath);
 79+  }
 80+  candidates.push('herdr');
 81+
 82+  const failures = [];
 83+  for (const candidate of [...new Set(candidates)]) {
 84+    try {
 85+      run(candidate, ['--version']);
 86+      return candidate;
 87+    } catch (error) {
 88+      failures.push(error.message);
 89+    }
 90+  }
 91+
 92+  throw new Error(`Cannot run Herdr. Tried ${[...new Set(candidates)].map((candidate) => JSON.stringify(candidate)).join(' and ')}.\n${failures.join('\n')}`);
 93+}
 94+
 95+function requirePi() {
 96+  try {
 97+    run('pi', ['--version']);
 98+  } catch (error) {
 99+    throw new Error(`Cannot run pi from PATH.\n${error.message}`);
100+  }
101+}
102+
103+function requireBash() {
104+  try {
105+    run('bash', ['--version']);
106+  } catch (error) {
107+    throw new Error(`Cannot run bash from PATH.\n${error.message}`);
108+  }
109+}
110+
111+function expandHome(directory) {
112+  return directory.startsWith('~/')
113+    ? path.join(os.homedir(), directory.slice(2))
114+    : directory;
115+}
116+
117+function promptForDirectory() {
118+  const result = spawnSync('bash', [
119+    '-c',
120+    'IFS= read -e -r -p "Directory: " directory; status=$?; printf "%s" "$directory"; exit "$status"',
121+  ], {
122+    encoding: 'utf8',
123+    env: process.env,
124+    stdio: ['inherit', 'pipe', 'inherit'],
125+  });
126+
127+  if (result.error) {
128+    throw new Error(`Cannot read a directory: ${result.error.message}`);
129+  }
130+