Skip to content

Commit

Permalink
version: 1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
twibiral authored Nov 5, 2022
2 parents f3ab0de + 9e7bfbe commit aae1f04
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 34 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),


## [1.4.0]
### Added
- Notebook mode for R (Thanks to @chlohal)
- Improved hiding of running indicator (Thanks to @chlohal and @ZackYJz)

### Changed
- Fix problem with the output boxes showing up (Thanks to @qiaogaojian)

## [1.3.0]
### Added
- WSL support (thanks to @clohal)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ undefined
3
```

To manage the open runtimes for Notebook Mode, you can use the `Open Code Runtime Management` command in the command palette. From this sidebar window, you can stop kernels.
To manage the open runtimes for Notebook Mode, you can use the `Open Code Runtime Management` command in the command palette. From this sidebar window, you can stop kernels. **Note: force-stopping requires `taskkill` on Windows and `pkill` on Unix. 99% of systems should have these preinstalled: if yours doesn't, please [file an issue](https://github.com/twibiral/obsidian-execute-code/issues/new/choose)**


## Style Settings
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "execute-code",
"name": "Execute Code",
"version": "1.3.0",
"version": "1.4.0",
"minAppVersion": "0.12.0",
"description": "Allows to execute code snippets within a note.",
"author": "twibiral",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "execute-code",
"version": "1.3.0",
"version": "1.4.0",
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
"main": "src/main.js",
"scripts": {
Expand Down
4 changes: 3 additions & 1 deletion src/ExecutorContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import PrologExecutor from "./executors/PrologExecutor";
import PythonExecutor from "./executors/python/PythonExecutor";
import CppExecutor from './executors/CppExecutor';
import ExecuteCodePlugin, {LanguageId} from "./main";
import RExecutor from "./executors/RExecutor.js";

const interactiveExecutors: Partial<Record<LanguageId, any>> = {
"js": NodeJSExecutor,
"python": PythonExecutor
"python": PythonExecutor,
"r": RExecutor
};

const nonInteractiveExecutors: Partial<Record<LanguageId, any>> = {
Expand Down
2 changes: 2 additions & 0 deletions src/Outputter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ export class Outputter extends EventEmitter {
* @see {@link clear()}
*/
private makeOutputVisible() {
this.closeInput();

if (!this.clearButton) this.addClearButton();
if (!this.outputElement) this.addOutputElement();

Expand Down
14 changes: 0 additions & 14 deletions src/executors/NodeJSExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,6 @@ export default class NodeJSExecutor extends ReplExecutor {
super(settings, settings.nodePath, args, file, "js");
}

/**
* Close the runtime.
* @returns A promise that resolves once the runtime is fully closed
*/
stop(): Promise<void> {
return new Promise((resolve, reject) => {
this.process.on("close", () => {
resolve();
});
this.process.kill();
this.process = null;
});
}

/**
* Writes a single newline to ensure that the stdin is set up correctly.
*/
Expand Down
59 changes: 59 additions & 0 deletions src/executors/RExecutor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {ChildProcessWithoutNullStreams, spawn} from "child_process";
import {Outputter} from "src/Outputter";
import {ExecutorSettings} from "src/settings/Settings";
import AsyncExecutor from "./AsyncExecutor";
import ReplExecutor from "./ReplExecutor.js";


export default class RExecutor extends ReplExecutor {

process: ChildProcessWithoutNullStreams

constructor(settings: ExecutorSettings, file: string) {
//use empty array for empty string, instead of [""]
const args = settings.RArgs ? settings.RArgs.split(" ") : [];

let conArgName = `notebook_connection_${Math.random().toString(16).substring(2)}`;

// This is the R repl.
// It's coded by itself because Rscript has no REPL, and adding an additional dep on R would be lazy.
//It doesn't handle printing by itself because of the need to print the sigil, so
// it's really more of a REL.
args.unshift(`-e`,
/*R*/
`${conArgName}=file("stdin", "r"); while(1) { eval(parse(text=tail(readLines(con = ${conArgName}, n=1)))) }`
)


super(settings, settings.RPath, args, file, "r");
}

/**
* Writes a single newline to ensure that the stdin is set up correctly.
*/
async setup() {
console.log("setup");
//this.process.stdin.write("\n");
}

wrapCode(code: string, finishSigil: string): string {
return `tryCatch({
cat(sprintf("%s",
eval(parse(text = ${JSON.stringify(code)} ))
))
},
error = function(e){
cat(sprintf("%s", e), file=stderr())
},
finally = {
cat(${JSON.stringify(finishSigil)});
flush.console()
})`.replace(/\r?\n/g, "") +
"\n";
}

removePrompts(output: string, source: "stdout" | "stderr"): string {
return output;
}

}
6 changes: 4 additions & 2 deletions src/executors/ReplExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { LanguageId } from "../main.js";
import { Outputter } from "../Outputter.js";
import { ExecutorSettings } from "../settings/Settings.js";
import AsyncExecutor from "./AsyncExecutor.js";
import killWithChildren from "./killWithChildren.js";

export default abstract class ReplExecutor extends AsyncExecutor {
process: ChildProcessWithoutNullStreams;
Expand Down Expand Up @@ -103,8 +104,9 @@ export default abstract class ReplExecutor extends AsyncExecutor {
return new Promise((resolve, reject) => {
this.process.on("close", () => {
resolve();
});
this.process.kill();
});

killWithChildren(this.process.pid);
this.process = null;
});
}
Expand Down
10 changes: 10 additions & 0 deletions src/executors/killWithChildren.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { execSync } from "child_process"

export default (pid: number) => {
if(process.platform === "win32") {
execSync(`taskkill /pid ${pid} /T /F`)
} else {
execSync(`pkill -P ${pid}`)
process.kill(pid);
}
}
13 changes: 0 additions & 13 deletions src/executors/python/PythonExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,6 @@ export default class PythonExecutor extends ReplExecutor {
this.globalsDictionaryName = `__globals_${Math.random().toString().substring(2)}_${Date.now()}`;
}

/**
* Close the runtime.
* @returns A promise that resolves once the runtime is fully closed
*/
stop(): Promise<void> {
return new Promise((resolve, reject) => {
this.process.on("close", () => {
resolve();
});
this.process.kill();
this.process = null;
});
}

/**
* Swallows and does not output the "Welcome to Python v..." message that shows at startup.
Expand Down
8 changes: 8 additions & 0 deletions src/settings/per-lang/makeRSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,13 @@ export default (tab: SettingsTab, containerEl: HTMLElement) => {
console.log('R args set to: ' + value);
await tab.plugin.saveSettings();
}));
new Setting(containerEl)
.setName("Run R blocks in Notebook Mode")
.addToggle((toggle) => toggle
.setValue(tab.plugin.settings.rInteractive)
.onChange(async (value) => {
tab.plugin.settings.rInteractive = value;
await tab.plugin.saveSettings();
}));
tab.makeInjectSetting(containerEl, "r");
}
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@
"1.1.0": "0.12.0",
"1.1.1": "0.12.0",
"1.2.0": "0.12.0",
"1.3.0": "0.12.0"
"1.3.0": "0.12.0",
"1.4.0": "0.12.0"
}

0 comments on commit aae1f04

Please sign in to comment.