/
main.js
158 lines (137 loc) · 4.57 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'use babel';
import term from 'term-launcher';
import path from 'path';
import { CompositeDisposable, Disposable } from 'atom';
const HydrogenLauncher = {
config: {
app: {
title: 'Terminal application',
description: 'This will depend on your operation system.',
type: 'string',
default: term.getDefaultTerminal(),
},
console: {
title: 'Jupyter console',
description: 'Change this if you want to start a `qtconsole` or any other jupyter interface that can be started with `jupyter <your-console> --existing <connection-file>`.',
type: 'string',
default: 'console',
},
command: {
title: 'Custom command',
description: 'This command will be excuted in the launched terminal. You can access the connection file from Hydrogen by using `{connection-file}` within your command',
type: 'string',
default: '',
},
},
subscriptions: null,
hydrogen: null,
platformIoTerminal: null,
activate() {
this.subscriptions = new CompositeDisposable();
this.subscriptions.add(atom.commands.add('atom-text-editor', {
'hydrogen-launcher:launch-terminal': () => this.launchTerminal(),
'hydrogen-launcher:launch-jupyter-console': () => this.launchJupyter(),
'hydrogen-launcher:launch-jupyter-console-in-platformio-terminal': () =>
this.launchJupyterInPlatformIoTerminal(),
'hydrogen-launcher:launch-terminal-command': () =>
this.launchTerminal(true),
'hydrogen-launcher:copy-path-to-connection-file': () =>
this.copyPathToConnectionFile(),
}));
},
deactivate() {
this.subscriptions.dispose();
},
consumeHydrogen(hydrogen) {
this.hydrogen = hydrogen;
return new Disposable(() => {
this.hydrogen = null;
});
},
consumePlatformIoTerminal(provider) {
this.platformIoTerminal = provider;
return new Disposable(() => {
this.platformIoTerminal = null;
});
},
launchTerminal(command = false) {
let cmd;
if (command) {
cmd = this.getCommand();
if (!cmd) return;
}
term.launchTerminal(cmd, this.getCWD(), this.getTerminal(), (err) => {
if (err) {
atom.notifications.addError(err.message);
}
});
},
launchJupyter() {
const connectionFile = this.getConnectionFile();
if (!connectionFile) return;
const jpConsole = atom.config.get('hydrogen-launcher.console');
term.launchJupyter(connectionFile, this.getCWD(), jpConsole, this.getTerminal(), (err) => {
if (err) atom.notifications.addError(err.message);
});
},
launchJupyterInPlatformIoTerminal() {
const connectionFile = this.getConnectionFile();
if (!connectionFile) return;
const jpConsole = atom.config.get('hydrogen-launcher.console');
term.getConnectionCommand(connectionFile, jpConsole, (err, command) => {
if (!this.platformIoTerminal) {
atom.notifications.addError('PlatformIO IDE Terminal has to be installed.');
} else if (err) {
atom.notifications.addError(err.message);
} else {
this.platformIoTerminal.run([command]);
}
});
},
copyPathToConnectionFile() {
const connectionFile = this.getConnectionFile();
if (!connectionFile) return;
atom.clipboard.write(connectionFile);
const message = 'Path to connection file copied to clipboard.';
const description = `Use \`jupyter console --existing ${connectionFile}\` to
connect to the running kernel.`;
atom.notifications.addSuccess(message, { description });
},
getConnectionFile() {
if (!this.hydrogen) {
atom.notifications.addError('Hydrogen `v1.0.0+` has to be running.');
return null;
}
try {
return this.hydrogen.getActiveKernel() ?
this.hydrogen.getActiveKernel().getConnectionFile() : null;
} catch (error) {
atom.notifications.addError(error.message);
}
return null;
},
getCommand() {
let cmd = atom.config.get('hydrogen-launcher.command');
if (cmd === '') {
atom.notifications.addError('No custom command set.');
return null;
}
if (cmd.indexOf('{connection-file}') > -1) {
const connectionFile = this.getConnectionFile();
if (!connectionFile) {
return null;
}
cmd = cmd.replace('{connection-file}', connectionFile);
}
return cmd;
},
getTerminal() {
return atom.config.get('hydrogen-launcher.app');
},
getCWD() {
return (atom.project.rootDirectories[0]) ?
atom.project.rootDirectories[0].path :
path.dirname(atom.workspace.getActiveTextEditor().getPath());
},
};
export default HydrogenLauncher;