Script is a one of my favourite packages for Atom. Run your code, directly from a text editor, in a HUGE number of languages. But it has one major flaw, there is no method of getting command line inputs from the user. This means that beginners trying out
name = input("Enter your name: ")
print("Hello " + name)
are going to be waiting endlessly for a python process that can't get user input.
Terminus is another great package for Atom. It adds an internal terminal shell to your Atom window, allowing you to run commands from the comfort of your text editor.
Maybe you already see where I'm going with this. If we can pass commands from Script to Terminus, the 'real' shell window should allow us to enter inputs.
- Ensure both Packages are installed.
apm install script
apm install terminus
Or simply search for them in Atom's internal package manager.
-
From
Atom -> Settings -> Packages -> Script -> view code
or wherever your atom packages install, find the filescript/lib/runtime.js
and open it. -
Find the function
execute()
around line 50. Add the following code (I have commented out unchanged lines):
NOTE: The first two lines if (atom.config.get...
and this.emitter.emit...
need to be moved down the page to the indicated spot.
// execute(argType = 'Selection Based', input = null, options = null) {
// const codeContext = this.codeContextBuilder.buildCodeContext(
// atom.workspace.getActiveTextEditor(), argType);
// In the future we could handle a runner without the language being part
// of the grammar map, using the options runner
// if (!codeContext || !codeContext.lang) return;
// const executionOptions = !options ? this.scriptOptions : options;
// const commandContext = CommandContext.build(this, executionOptions, codeContext);
try {
var terminus = require('../../terminus/lib/terminus.js').provideTerminus();
} catch (e) {
var terminus = null;
console.log("Could not find Terminus");
}
if (terminus != null) {
var command = commandContext.command;
for (let i = 0; i < commandContext.args.length; i++) {
command += ' "' + commandContext.args[i] + '"';
}
terminus.run(['printf "\\33c\\e[3J" && ' + command]);
return;
}
if (atom.config.get('script.stopOnRerun')) this.stop();
this.emitter.emit('start'); // These were moved from above!!!
// if (!commandContext) return;
// if (commandContext.workingDirectory) {
// executionOptions.workingDirectory = commandContext.workingDirectory;
// }
// this.emitter.emit('did-context-create', {
// lang: codeContext.lang,
// filename: codeContext.filename,
// lineNumber: codeContext.lineNumber,
// });
// this.runner.scriptOptions = executionOptions;
// this.runner.run(commandContext.command, commandContext.args, codeContext, input);
// this.emitter.emit('started', commandContext);
//}
Also, the line terminus.run(
adds the command 'printf "\\33c\\e[3J" && '
. This is not necessary but cleans up the terminal after running commands. If you would rather see the run commands in your terminal, then replace that line with simply terminus.run([command]);
Once that code is in, save and reopen Atom. From now on, when you run Script, it will open in a Terminus window, and accept inputs. I have tested with C++ and Python, and I think it should work for others, but if you run into a problem, let me know.
I hope that was helpful to someone, happy coding!
- Keldan
Hey Sami,
The problem you are having is due to how the
input
function worked in Python 2.7, rather than Python 3. If you did the steps I showed Jess, then either:a) You didn't save the file properly and/or restart atom after saving it
b)
Script
was updated since you made the change (Some updates might revert the changes back to the way they were originally)c) This tutorial might not work anymore. I haven't checked it since I wrote it, it could be that something else needs changing. If you search for "Atom script python 3" you should find other people with the same issues with python.