Skip to content

Instantly share code, notes, and snippets.

@Kacarott
Created September 3, 2020 05:34
Show Gist options
  • Save Kacarott/e750aa216a7dc2bbe0ff4728bdbd7d37 to your computer and use it in GitHub Desktop.
Save Kacarott/e750aa216a7dc2bbe0ff4728bdbd7d37 to your computer and use it in GitHub Desktop.
Inputs for Script

Inputs for Script

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

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.

Connecting Script and Terminus

  1. Ensure both Packages are installed.

apm install script

apm install terminus

Or simply search for them in Atom's internal package manager.

  1. From Atom -> Settings -> Packages -> Script -> view code or wherever your atom packages install, find the file script/lib/runtime.js and open it.

  2. 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]);

Done!

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

@wmunmun
Copy link

wmunmun commented Sep 12, 2020

Hi there! I have installed terminus, added your codes into script/lib/runtime.js but I encountered this error:

Enter your name: Jess
Traceback (most recent call last):
File "/var/folders/_1/qmv07vn53vvb_h37tp93s9s40000gn/T/atom_script_tempfiles/de436750-f4c7-11ea-8381-533e72ca57df", line 73, in
name = input("Enter your name: ")
File "", line 1, in
NameError: name 'Jess' is not defined

I tried to run this set of codes:
name = input("Enter your name: ")
print("Hello " + name)

Would you be able to assist pls?? Thanks very much!

@Kacarott
Copy link
Author

Kacarott commented Sep 12, 2020

Hi wmunmun (Jess),

So I am pretty sure this has to do with a different issue with Atom Script. Atom Script by default uses Python 2.x while your code shows you are using Python 3.x.

It is pretty easy to fix. If you go to script/lib/grammars/python.coffee, you will see on lines 5 and 12 something like:
command: 'python'
Just change them both to be:
command: 'python3'

If the problem persists let me know and I'll dig a bit more. Hope that works!
- Keldan

@wmunmun
Copy link

wmunmun commented Sep 12, 2020

Hi Keldan,

Thanks so much!! This solved my issue. Truly appreciated your help!

Regards,
Jess

@ManjimSarkar
Copy link

ManjimSarkar commented Sep 13, 2020

Hi Keldan
im noob at programming and i did what you said but when i tried to run this program to test if everythings all right :

print(fuggmylife)

I get this in the terminal :
"
Windows PowerShell
Copyright (C) 2014 Microsoft Corporation. All rights reserved.

PS C:\Users\041501.atom\packages\script> printf "\33c\e[3J" && python3 "-u" "C:\Users\041501\AppData\Local\Temp\atom_script_tempfiles\58725b
f0-f5ca-11ea-a014-7163d7cea1a0"
At line:1 char:20

  • printf "\33c\e[3J" && python3 "-u" "C:\Users\041501\AppData\Local\Temp\atom_scri ...
  •                ~~
    

The token '&&' is not a valid statement separator in this version.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : InvalidEndOfLine

PS C:\Users\041501.atom\packages\script>
"
Any help appreciated

@Kacarott
Copy link
Author

Hi Koinzelle,

Was that the entire code? If it was, you need quotes around strings. Try run print("fuggmylife") and see if that works. But if there is more code maybe paste the whole thing here so I can see what might be wrong?

-Keldan

@ManjimSarkar
Copy link

ManjimSarkar commented Sep 14, 2020

Hi Keldan,

Thanks for getting back to me...the program in the comment was a typo....i did use print("text")...i am sending you the screenshots
....might help understand the issue better.
I think i followed the steps properly...copied your script....indented it.....removed the two lines if (atom.config.get...) and (this.emitter.emit...)
I'm using Python 3.8 x64. Have installed Terminus by bus-stop and Script by rgbkrk.
I'm on an old Windows 8.1 (3rd world problems) though i don't think it can point to any problem.

Thx!

atom
runtime

@Kacarott
Copy link
Author

Kacarott commented Sep 14, 2020

Hi Koinzelle,

So I had a look around, and I think the issue is that your version of powershell is a bit old, and so doesn't support the && operator. I think that newer versions have introduced it (maybe only prereleases), but I am not a windows user so I can't be sure.

Try this out for me, and let me know if it works. Change the line:

terminus.run(['printf "\\33c\\e[3J" && ' + command]);

To be:

terminus.run(['printf "\\33c\\e[3J"; ' + command]);

If it works I'll update my gist before other people get the same problem.
- Keldan

@ManjimSarkar
Copy link

Hey Keldan,

Ok, my program does work now as intended but drops a error too (but dosen't affect my program either!)...

I tried replacing "&&" with ";" which solved one problem but pointed me to a more severe problem that is my powershell dosen't recognise "printf" command...I found this thread and I don't think anyone has found a solution yet :

daltonmenezes/hyper-init#1

But either way, in the end, the problem was solved, my program works and even takes input even though I'm getting this weird error which interestingly doesn't affect the program's operation.

So, thank you very much Keldan, you're a f-ing wizard!!

I'll attach the screenshot if you want to check it...
pyt

@Kacarott
Copy link
Author

Hey Koinzelle,

Happy to help! That is strange... Well until you figure it out, to get rid of that error, you can shorten the line:

terminus.run(['printf "\\33c\\e[3J"; ' + command]);

To just

terminus.run([command]);

All the first is supposed to do is clear the console so that every time you run it is nice and clean, but that error isn't any better haha. So maybe that makes it a bit more pleasant.

Happy coding!
- Keldan

@ManjimSarkar
Copy link

Hey Keldan,

That fixed the additional error I was having....I'm getting a clean output now.

Thank you very much!

@nvstph
Copy link

nvstph commented Feb 21, 2021

This helped me greatly, thanks so much! All I wanted to do was run a python script inside of Atom, took me a whole day to figure out.

I used this to clear the console, run the command, wait for user to press 'enter' , and then exit the console.

terminus.run(['clear; ' + command + '; pause; exit']);

@RyanJawad
Copy link

RyanJawad commented Apr 19, 2021

Thanks, Keldan. This was very helpful. I'm surprised it takes so many steps to run python in Atom. I wonder if there is a way to set this up so it can be installed from Atom's internal package manager. I think others would benefit from that.

@Sami990
Copy link

Sami990 commented Apr 23, 2021

Hi wmunmun (Jess),

So I am pretty sure this has to do with a different issue with Atom Script. Atom Script by default uses Python 2.x while your code shows you are using Python 3.x.

It is pretty easy to fix. If you go to script/lib/grammars/python.coffee, you will see on lines 5 and 12 something like:
command: 'python'
Just change them both to be:
command: 'python3'

If the problem persists let me know and I'll dig a bit more. Hope that works!

  • Keldan

Dear Keldan
I have the same problem. I did what you suggested to Jess but my problem still persists. would you mind guiding me a little more?
Thanks in advance!
Sami
p.s.
Traceback (most recent call last):
File "/Users/mohsen/Desktop/python/inputt.py", line 1, in
name = input('please insert your name: ')
File "", line 1, in
NameError: name 'Sami' is not defined

@Kacarott
Copy link
Author

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.

@aeyates
Copy link

aeyates commented May 21, 2021

Thanks for this snippet. It worked great!

@manngo
Copy link

manngo commented Aug 16, 2021

Works beautifully for me. I was looking for a tool to teach Python, and Atom does so many other useful things. This fills in a gap.

Thanks for your work.

@manngo
Copy link

manngo commented Aug 16, 2021

Hi Keldan

There is one minor issue. Every time I run the script the Terminus window closes and reopens, which is a little distracting. Do you know of a solution to this?

@the-j0k3r
Copy link

@Kacarott

If I may suggest an improvement the code you pasted would perhaps make more sense if you used a diff block inside the md file
described here https://support.codebasehq.com/articles/tips-tricks/syntax-highlighting-in-markdown

--- a
+++ b
@@ -12,9 +12,6 @@ def 'cep.App', class App
 
     if @currentUser.isLoggedIn()
       @logoutView.render()
-      @navigateToDashboard()
-    else
+      @navigateToLoginPage()

@manngo
Copy link

manngo commented Jan 30, 2022

I have found some strange behaviour using Script & Terminus on my Macintosh.

If my document is set to CRLF line breaks, I get the dreaded unexpected indent error. This is regardless of whether I run the whole document or a selection.

This is not a mixed indents issues (I prefer tabs to spaces), and it’s not due to using tabs (I have converted tabs to spaces to see what would happen). I can get it working reliably by switching back to using LF line breaks.

The error occurs in line 1, as:

File "<stdin>", line 1
    "-u" "/Users/mark/Library/Mobile Documents/com~apple~CloudDocs/Training/python/while.py"
    ^

so it’s happening when the script is actually run.

On Windows, this doesn’t appear to be a problem.

I’m still trying to get to the bottom of this.

@amancad
Copy link

amancad commented Aug 6, 2022

Hi wmunmun (Jess),

So I am pretty sure this has to do with a different issue with Atom Script. Atom Script by default uses Python 2.x while your code shows you are using Python 3.x.

It is pretty easy to fix. If you go to script/lib/grammars/python.coffee, you will see on lines 5 and 12 something like:

command: 'python'

Just change them both to be:

command: 'python3'

If the problem persists let me know and I'll dig a bit more. Hope that works!

  • Keldan

In my case the following worked for Windows 10 and paython 3.10 and Atom 1.60:

It is pretty easy to fix. If you go to script/lib/grammars/python.js, you will see on lines 16 and 26 something like:

command: 'python'

Just change them both to be:

command: 'python.exe'


I also confirm that it helped me to change the line:

terminus.run(['printf "\\33c\\e[3J" && ' + command]);

along the line:

terminus.run(['clear; ' + command + '; pause; exit']);
@
in the runtime.js file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment