How to display command history with jline3? - jline

I want the most recent command entered to be displayed when the user presses the up arrow key.
The Terminal is defined like this (Scala code):
val terminal: Terminal =
TerminalBuilder.builder
.system(true)
.build
The LineReader is defined like this:
def reader(parser: Parser, terminal: Terminal): LineReader = {
val lineReader: LineReader = LineReaderBuilder.builder
.terminal(terminal)
.completer(shellManager.topShell.completer)
.parser(parser)
.variable(LineReader.HISTORY_FILE, historyFile)
.history(new DefaultHistory())
.build
lineReader.unsetOpt(LineReader.Option.INSERT_TAB)
lineReader
}
Update: I found that the above actually works on some consoles, not others. I am still discovering what works and what does not. Any insight would be appreciated.

This is supposed to work out-of-the box. If you have an issue with a specific terminal, please report which exact terminal you use. For what it's worth, this can't work from inside build tools (gradle, maven) or IDE (Eclipse, Intellij IDEA).

Related

Electron Windows Production Bundle Command Line Arguments

I'm building an Electron App using electron-builder on macOS.
In my code I access the command line args like this:
const cmd = electron.remote.app.commandLine;
const val = cmd.hasSwitch('myArg')
? cmd.getSwitchValue('myArg')
: undefined;
This works fine for the production build on macOS when providing the arguments:
./my-electron-app.app/Contents/MacOS/my-electron-app --myArg=foo
// or:
open my-electron-app.app --args -myArg=foo
But on Windows I can't get it working.
Here's what I tried using cmd.exe:
my-electron-app.exe --myArg=foo
my-electron-app.exe -myArg=foo
my-electron-app.exe /myArg=foo
my-electron-app.exe myArg=foo
When logging electron.remote.process.argv[1] I can see the passed arguments on macOS and Windows, but hasSwitch and getSwitchValue won't give me the value.
What am I doing wrong? Or is there a better way to get cross platform command line arguments working?
I'm gonna take a guess that this is because of the capital letters in your switch. See this closed issue:
This is intentional. The hasSwitch API is a direct wrapper of the Chromium CommandLine class, which behaves this way on purpose.
From Chromium source:
Switch names must be lowercase.
Though it's not totally clear to me yet why Mac doesn't suffer from the same problem.

GoLand setting terminal cells doesn't work

I'm using the tcell library to display terminal cell graphics.
While writing this project in GoLand, I've noticed that using a normal run configuration and running the program in the integrated terminal, I'm not seeing the cells getting set as intended, despite tcell not giving any errors.
Program:
package main
import (
"time"
"github.com/gdamore/tcell"
)
func main() {
screen, err := tcell.NewScreen()
if err != nil {
panic(err)
}
err = screen.Init()
if err != nil {
panic(err)
}
screen.SetCell(0, 0, tcell.StyleDefault, 'X')
screen.SetCell(1, 0, tcell.StyleDefault, 'X')
screen.SetCell(1, 1, tcell.StyleDefault, 'X')
screen.SetCell(10, 10, tcell.StyleDefault, 'X')
screen.Show()
time.Sleep(time.Second*5)
}
GoLand output:
The program works as expected when running through cmd:
How can I set a run configuration in GoLand to run my program in cmd, or some other form of terminal that will allow me to set cells like this?
Open Help | Find Action...
Type Registry and hit Enter.
Find go.run.processes.with.pty there and turn it on.
Please, keep in mind that it can cause problems with run configurations like failing green tests or vice versa, never finishing debug sessions, and so on. If you notice weird IDE behavior related to console output, please disable the registry option back.
I'm not sure if points on Y-axis do display properly inside the Run window.
I guess GoLands terminal is a fake terminal without real cursor addressability. There may not be a good solution if that is the case.
I’m the author of tcell and I use goland but I confess I always run my test programs in a real terminal rather than in the toy terminal that the IDE provides. This is true whether I use goland, visual studio code, or even the venerable emacs.
By using a new Batch run configuration, you can run a batch file to build the program, then run the program in a new cmd window.
In the run configuration in GoLand, set "Working directory" to the main package directory. Then set the script to a new batch file.
Here is the code in my batch file for my package client
go build
start cmd /C client.exe
Running this configuration will build the package, then run the program in a new external cmd window where the cells display properly.
This solution isn't great, because most of the advantages of the GoLand run configuration system are lost, including debugging, process management (stop / restart), and other build options.
Does anyone have a better solution?

Ansible Expect module question - the installer makes me respond on a new line

I have an interesting issue. I'm using the expect module for ansible to install an application. All works well until the final question.
It does something like this.
Enter the default profile name [default] followed by a blank line:
<cursor starts here no at the semi colon>
and the response look looks like this if you manually do it. (yes i know its silly but its the way the binary installs - i can't control it)
Enter the default profile name [default] followed by a blank line:
default
The application is now installed message.
It errors out because the the cursor drops to the line below. Now, i tried doing something like this
expect:
'':'default'
but it errored out. Is there any other way to do this?

How to run/debug a streamlit application from an IDE

I really like streamlit as an environment for research. Mixing a notebook/dashboard-like output I can design quickly with pure code for its definition (no cells etc.) as well as the ability to influence my code through widgets while it runs is a game changer.
For this purpose, I was looking for a way to run or even debug a streamlit application, since the tutorials only show it being started via the commandline:
streamlit run code.py
Is there a way to do either running or debugging from an IDE?
I found a way to at least run the code from the IDE (PyCharm in my case). The streamlit run code.py command can directly be called from your IDE. (The streamlit run code.py command actually calls python -m streamlit.cli run code.py, which was the former solution to run from the IDE.)
The -m streamlit run goes into the interpreter options field of the Run/Debug Configuration (this is supported by Streamlit, so has guarantees to not be broken in the future1), the code.py goes into the Script path field as expected. In past versions, it was also working to use -m streamlit.cli run in the interpreter options field of the Run/Debug Configuration, but this option might break in the future.
Unfortunately, debugging that way does not seem to work since the parameters appended by PyCharm are passed to streamlit instead of the pydev debugger.
Edit: Just found a way to debug your own scripts. Instead of debugging your script, you debug the streamlit.cli module which runs your script. To do so, you need to change from Script path: to Module name: in the top-most field (there is a slightly hidden dropdown box there...). Then you can insert streamlit.cli into the field. As the parameters, you now add run code.py into the Parameters: field of the Run/Debug Configuration.
EDIT: adding #sismo 's comment
If your script needs to be run with some args you can easily add them as
run main.py -- --option1 val1 --option2 val2
Note the first -- with blank: it is needed to stop streamlit argument parsing and pass to main.py argument parsing.
1 https://discuss.streamlit.io/t/run-streamlit-from-pycharm/21624/3
If you're a VS Code user, you can debug your Streamlit app by adding the following configuration to your launch.json file:
{
"name": "Python:Streamlit",
"type": "python",
"request": "launch",
"module": "streamlit",
"args": [
"run",
"${file}",
"--server.port",
"SPECIFY_YOUR_OWN_PORT_NUMBER_HERE" ]
}
Specifying the port number allows you to launch the app on a fixed port number each time you run your debug script.
Once you've updated your launch.json file, you need to navigate to the Run tab on the left gutter of the VS code app and tell it which Python config it should use to debug the app:
Selecting Debug config for python interpreter
Thanks to git-steb for pointing me to the solution!
I've come up with an alternative solution which allows you to use PyCharm debugging in a natural way. Simply set up a run script (which I call run.py which looks like this:
from streamlit import bootstrap
real_script = 'main_script.py'
bootstrap.run(real_script, f'run.py {real_script}', [], {})
and set that up as a normal Python run configuration in PyCharm.
Cannot comment so I have to put this as an answer.
An addition to #Ben's answer (module debugging part):
if your script needs to be run with some args you can easily add them as
run main.py -- --option1 val1 --option2 val2
Note the first -- with blank: it is needed to stop streamlit argument parsing and pass to main.py argument parsing
With some modification to #aiwa answer - This worked for me in the VS code version - 1.58
{
"configurations": [
{
"name": "Python:Streamlit",
"type": "python",
"request": "launch",
"module": "streamlit.cli",
"args": [
"run",
"${file}"
],
}
]
}
Aug, 12, 2022:
Please update your pip and streamlit versions. Sometime, it is mandatory to update all both version.
pip install pip --upgrade
pip install --upgrade streamlit
Open Pycharm Editor and go to the Edit Configuration file as mentioned below in picture. Do not clear streamlit in my dropdown box. Click on dropdown box.
Run/Debug Configurations:
You have to change three directories remember that script path.
1) You can obtain script path by typing which streamlit in terminal and paste the path in script path.
2) click on working directory and give directory of your python file which contain streamlit.
3) in Paramaters: give python file name like app.py with run.
Alongside other solutions, another easy and quick solution is using pdb library.
For instance;
st.dataframe(df)
import pdb; pdb.set_trace()
st.bar_chart(df)
When you run code, your IDE (or even command line) will stop at the 'set trace' point and the command line show you something like that:
(Pdb)>
In that case, you can call your variables and process them on the command line. For instance:
For other options of PDB library please see: https://docs.python.org/3/library/pdb.html

How to run scala code from terminal

I have a simple Scala code that i want to run from osx Terminal.
Currently, my code runs in intellij.
object test
{
def main(args: Array[String]): Unit =
{
// my code
}
}
So i have this .scala path:
/Users/rdave/projects/test.scala
This is what i have tried from osx Terminal:
scala /Users/rdave/projects/test.scala
scalac /Users/rdave/projects/test.scala
And got command not found
command not found
Is generated by the terminal, signaling that it can't find your executable
As the docs suggest:
Path and Environment
For quick access, add scala and scalac to your path. For example:
Environment Variable Value (example)
Unix $SCALA_HOME /usr/local/share/scala
$PATH $PATH:$SCALA_HOME/bin
After this, you will be able to call scala and scalac without any errors.
Better yet, put your code in the "standard" directory structure (your scala class would therefore end up in /Users/rdave/projects/myproject/src/main/scala/) and run sbt console. You will have access to all your code and be able to use the REPL for experimentation.

Resources