|
6 | 6 | #include "scriptit_builtins.hpp" |
7 | 7 | #include "perser.hpp" |
8 | 8 | #include "json_and_kernel.hpp" |
| 9 | +#include <unistd.h> // readlink for --notebook and --customize |
9 | 10 | // ═══════════════════════════════════════════════════════════ |
10 | 11 | // ──── Execute Script Helper ──────────────────────────────── |
11 | 12 | // ═══════════════════════════════════════════════════════════ |
@@ -48,6 +49,132 @@ int main(int argc, char *argv[]) |
48 | 49 | return 0; |
49 | 50 | } |
50 | 51 |
|
| 52 | + if (argc > 1 && std::string(argv[1]) == "--notebook") |
| 53 | + { |
| 54 | + // Launch the web notebook server |
| 55 | + // Try the installed scriptit-notebook launcher first, then fallback to local notebook.sh |
| 56 | + std::string notebookCmd; |
| 57 | + // Check for system-installed launcher |
| 58 | + if (std::system("command -v scriptit-notebook >/dev/null 2>&1") == 0) |
| 59 | + { |
| 60 | + notebookCmd = "scriptit-notebook"; |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + // Fallback: find notebook.sh relative to this binary |
| 65 | + // argv[0] might be a path or just "scriptit" from PATH |
| 66 | + std::string selfPath; |
| 67 | + // Try /proc/self/exe on Linux |
| 68 | + char buf[4096] = {}; |
| 69 | + ssize_t len = readlink("/proc/self/exe", buf, sizeof(buf) - 1); |
| 70 | + if (len > 0) |
| 71 | + { |
| 72 | + buf[len] = '\0'; |
| 73 | + selfPath = std::string(buf); |
| 74 | + } |
| 75 | + if (!selfPath.empty()) |
| 76 | + { |
| 77 | + std::string dir = selfPath.substr(0, selfPath.rfind('/')); |
| 78 | + // If installed to /usr/local/bin, notebook files are in /usr/local/share/scriptit/notebook/ |
| 79 | + std::string shareNotebook = dir + "/../share/scriptit/notebook/notebook_server.py"; |
| 80 | + std::string localNotebook = dir + "/notebook.sh"; |
| 81 | + // Also check REPL layout |
| 82 | + std::string replNotebook = dir + "/notebook/notebook_server.py"; |
| 83 | + if (std::ifstream(shareNotebook).good()) |
| 84 | + { |
| 85 | + notebookCmd = "python3 \"" + shareNotebook + "\""; |
| 86 | + } |
| 87 | + else if (std::ifstream(replNotebook).good()) |
| 88 | + { |
| 89 | + notebookCmd = "python3 \"" + replNotebook + "\""; |
| 90 | + } |
| 91 | + else if (std::ifstream(localNotebook).good()) |
| 92 | + { |
| 93 | + notebookCmd = "bash \"" + localNotebook + "\""; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (notebookCmd.empty()) |
| 99 | + { |
| 100 | + std::cerr << "Error: Could not find notebook server.\n"; |
| 101 | + std::cerr << "Make sure ScriptIt is installed system-wide (sudo cmake --install build_scriptit)\n"; |
| 102 | + return 1; |
| 103 | + } |
| 104 | + |
| 105 | + // Forward remaining args (e.g. --port, notebook file) |
| 106 | + for (int i = 2; i < argc; i++) |
| 107 | + { |
| 108 | + notebookCmd += " "; |
| 109 | + notebookCmd += argv[i]; |
| 110 | + } |
| 111 | + return std::system(notebookCmd.c_str()); |
| 112 | + } |
| 113 | + |
| 114 | + if (argc > 1 && std::string(argv[1]) == "--customize") |
| 115 | + { |
| 116 | + // Launch the color customizer web server |
| 117 | + std::string customizerScript; |
| 118 | + std::string selfPath; |
| 119 | + char buf[4096] = {}; |
| 120 | + ssize_t len = readlink("/proc/self/exe", buf, sizeof(buf) - 1); |
| 121 | + if (len > 0) |
| 122 | + { |
| 123 | + buf[len] = '\0'; |
| 124 | + selfPath = std::string(buf); |
| 125 | + } |
| 126 | + if (!selfPath.empty()) |
| 127 | + { |
| 128 | + std::string dir = selfPath.substr(0, selfPath.rfind('/')); |
| 129 | + // Check various locations |
| 130 | + std::string paths[] = { |
| 131 | + dir + "/../share/scriptit/color_customizer/customizer_server.py", // system install |
| 132 | + dir + "/scriptit-vscode/color_customizer/customizer_server.py", // REPL layout |
| 133 | + dir + "/color_customizer/customizer_server.py", // local |
| 134 | + }; |
| 135 | + for (auto &p : paths) |
| 136 | + { |
| 137 | + if (std::ifstream(p).good()) |
| 138 | + { |
| 139 | + customizerScript = p; |
| 140 | + break; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + // Also try relative to CWD |
| 145 | + if (customizerScript.empty()) |
| 146 | + { |
| 147 | + std::string cwdPaths[] = { |
| 148 | + "scriptit-vscode/color_customizer/customizer_server.py", |
| 149 | + "color_customizer/customizer_server.py", |
| 150 | + }; |
| 151 | + for (auto &p : cwdPaths) |
| 152 | + { |
| 153 | + if (std::ifstream(p).good()) |
| 154 | + { |
| 155 | + customizerScript = p; |
| 156 | + break; |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + if (customizerScript.empty()) |
| 162 | + { |
| 163 | + std::cerr << "Error: Could not find the color customizer.\n"; |
| 164 | + std::cerr << "Make sure ScriptIt is installed with the VS Code extension files.\n"; |
| 165 | + return 1; |
| 166 | + } |
| 167 | + |
| 168 | + std::string cmd = "python3 \"" + customizerScript + "\""; |
| 169 | + // Forward --port if provided |
| 170 | + for (int i = 2; i < argc; i++) |
| 171 | + { |
| 172 | + cmd += " "; |
| 173 | + cmd += argv[i]; |
| 174 | + } |
| 175 | + return std::system(cmd.c_str()); |
| 176 | + } |
| 177 | + |
51 | 178 | if (argc > 1 && std::string(argv[1]) == "--test") |
52 | 179 | { |
53 | 180 | std::string source = |
|
0 commit comments