Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/util/jupyter-api/chdir-commands.ts
Views: 687
/*1Code to change the working directory to a given directory in2several different languages.3*/45export default function createChdirCommand(lang: string, path: string): string {6// throws exception if don't know how.7lang = lang.toLowerCase(); // xeus-cling is 'C++-17'8if (lang == "sparql") {9// there is no notion of current directory for sparql.10return "";11}12if (lang.startsWith("python") || lang.startsWith("sage")) {13return createPythonChangeDirectoryCode(path);14} else if (lang == "r") {15return createRChangeDirectoryCode(path);16} else if (lang == "julia") {17return createJuliaChangeDirectoryCode(path);18} else if (lang == "octave" || lang == "matlab") {19return createOctaveChangeDirectoryCode(path);20} else if (lang == "javascript") {21return createNodeChangeDirectoryCode(path);22} else if (lang == "bash" || lang == "sh") {23return createBashChangeDirectoryCommand(path);24} else if (lang == "prolog") {25return createPrologChangeDirectoryCode(path);26} else if (lang == "c" || lang.startsWith("c++")) {27return createCppChangeDirectoryCode(path);28} else {29// e.g., "gap" -- I got stumped on that.30throw Error(31`unable to change directory: chdir for language ${lang} is not implemented.`32);33}34}3536// mostly written by ChatGPT437function createPythonChangeDirectoryCode(path: string): string {38const escapedPath = path.replace(/\\/g, "\\\\").replace(/'/g, "\\'");39return `import os; os.chdir('${escapedPath}')`;40}4142function createRChangeDirectoryCode(path) {43const escapedPath = path.replace(/\\/g, "\\\\").replace(/"/g, '\\"');44return `setwd("${escapedPath}")`;45}4647function createJuliaChangeDirectoryCode(path) {48const escapedPath = path.replace(/\\/g, "\\\\").replace(/"/g, '\\"');49return `cd("${escapedPath}")`;50}5152function createOctaveChangeDirectoryCode(path) {53const escapedPath = path.replace(/\\/g, "\\\\").replace(/'/g, "\\'");54return `cd('${escapedPath}')`;55}5657function createNodeChangeDirectoryCode(path) {58const escapedPath = path.replace(/\\/g, "\\\\").replace(/`/g, "\\`");59return `process.chdir(\`${escapedPath}\`)`;60}6162export function escapeBashChangeDirPath(path: string): string {63return path.replace(/(["'$`\\])/g, "\\$1");64}6566function createBashChangeDirectoryCommand(path) {67const escapedPath = escapeBashChangeDirPath(path);68return `cd '${escapedPath}'`;69}7071/*72function createGAPChangeDirectoryCode(path) {73const escapedPath = path.replace(/\\/g, "\\\\").replace(/"/g, '\\"');74// SetCurrentDirectory doesn't exist. ChatGPT made it up from C#.75// I studied gap docs and googled for quite a while and was totally76// stumped! Make this just isn't possible in gap...77return `SetCurrentDirectory(Directory("${escapedPath}"));;`;78}79*/8081function createPrologChangeDirectoryCode(path) {82const escapedPath = path.replace(/\\/g, "\\\\").replace(/'/g, "\\'");83return `working_directory(_, '${escapedPath}').`;84}8586function createCppChangeDirectoryCode(path) {87const escapedPath = path.replace(/\\/g, "\\\\").replace(/"/g, '\\"');88return `#include <unistd.h>\nchdir("${escapedPath}")\n`;89}909192