When using zeus in zsh cursor position shifts after the the termination of zeus command - ruby-on-rails-4

Sometimes there is a strange issue with terminal cursor in zsh when zeus command was terminated.
The position of cursor shifts and each line of the output in the console has extra indentation, e.g.
services GET /services(.:format) services#index
payments GET /payments(.:format) payments#index
orders_verify POST /orders_verify(.:format) orders_verify#index
orders GET /orders(.:format) orders#index
diets GET /diets(.:format) diets#index
The only way to fix that is to open a new terminal window/tab

Staircasing is unrelated to locale. Full-screen programs manipulate the terminal-mode settings to let them read single characters from the screen as well as send special characters (such as carriage return and line-feed) to the screen without having the terminal driver "translate" them.
The quick fix — run this command:
reset
(you may have to press controlJ after typing "reset" to enter this properly)
Further reading:
tset, reset - terminal initialization

Make sure you have set LOCALE environment vars to UTF-8 in your .zshrc file
Open .zshrc
vim ~/.zshrc
Add these lines
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
Close the file (Shift-ZZ) and reload it in the current session (or open a new terminal window/tab)
. ~/.zshrc

Related

subprocess.popen stream handling

Is it possible to prevent subprocess.popen from showing prompts in the terminal?
Attempting to map a drive but would like to read the prompt for credentials in the script rather than display them to the terminal. The idea being I can carry out actions based on the response.
I am aware the use of shell is frowned upon when using string based commands (for obvious reasons), however I'm controlling the input so am happy with the risk for testing purposes.
I was under the impression that all stdout (interaction) would be parsed into the output_null variable. Instead I am still getting the prompt in the terminal (as illustrated below). I'm either miss understanding how the streams work or I'm missing something. Can anyone enlighten me please
command = "mount -t smbfs //{s}/SYSVOL {m}".format(s=server, m=temp_dir)
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output_null = p.communicate()[0]
if "Password for" in output_null:
print 'awdaa'
Terminal Shows
Password for 192.168.1.111:

sublimerepl getenv failing

I'd like to use the SiblimeREPL package with Sublime Text. When I try to start a REPL, I get
SublimeREPL: obtaining sane environment failed in getenv()
Check console and 'getenv_command' setting
WARN: Falling back to SublimeText environment
This happens regardless of which REPL I try to start. (I tried Ruby, Python, and Clojure.) I tried Sublime Text 2 and Sublime Text 3 with the same results. This is on Mac OS X, if that matters.
I looked in the package settings, where I see
"getenv_command": ["/bin/bash", "--login", "-c", "env"],
If I run "/bin/bash --login -c env" at a Terminal prompt, I get my environment listed.
What do I need to change in order to get a successful getenv_command?
I had the same problem as ssgam. The problem line for me is in the getenv method. It calls subprocess.check_output(getenv_command), which doesn't exist in python 2.6, which ST2 seems to use.
The trick is, it only calls subprocess.check_output() if getenv_command is truthy, and defaults to os.environ.copy() otherwise. So to get ssgam's fix without modifying the SublimeREPL package, in Preferences > Package Settings > SublimeREPL > Settings - User, do something like this:
{
"getenv_command": false
}
I investigated this issue a little bit deeper and it seems SublimeText 3 is also affected. In my case the problem is related to bash-completion feature, in particular COMP_WORDBREAKS environment variable.
Use the following command to show the contents of COMP_WORDBREAKS:
$ echo "$COMP_WORDBREAKS"
will output
"'><=;|&(:
You can also use:
$ echo $COMP_WORDBREAKS
but note that with the second command (without quotes), you'll not see that
the variable also contains a line feed character.
The problem here is the line feed character which breaks output parsing in getenv_command feature. If you extract part of the source code for SublimeREPL you can get real error message from python interpreter.
Traceback (most recent call last):
File "main.py", line 71, in getenv
env = dict(line.split('=', 1) for line in lines)
ValueError: dictionary update sequence element #6 has length 1; 2 is required
You can match element #6 with the position of COMP_WORDBREAKS in env listing.
Solution (first that came to my mind)
I can't tell at the moment what is real impact on bash-completion feature after following solution is applied and of course SublimeREPL hopefully should be fixed accordingly. Please comment my answer to fill in missing knowledge.
We may want to remove disturbing characters to get rid of the error.
First let's identify those characters
$ echo -n "${COMP_WORDBREAKS}" | od -t x1c
will output
0000000 20 09 0a 22 27 3e 3c 3b 7c 26 28 3a
\t \n " ' > < ; | & ( :
0000014
so we have three to remove. The simplest way is to add to your .bashrc following line:
COMP_WORDBREAKS="${COMP_WORDBREAKS#???}"
Voila! No more error message.
My final thought is about removed characters. I'm not fully in how bash-completion works and I'm aware of that modifying COMP_WORDBREAKS can affect other scripts using it. For now you can always change it ad-hoc.
I hope this helped.
Cheers
Found it. Fixed it. SublimeREPL assumes that running getenv_command will produce SOLELY the output from running env, and every line will contain an equals sign. But my .bash_profile echos some stuff to stdout.
The solution was to wrap my .bash_profile output in a
if [[ $- == *i* ]]
to not produce extra output besides the executed command.
TLDR;
Replace:
env = dict(line.split('=', 1) for line in lines)
in ~/.config/sublime-text-3/Packages/SublimeREPL/repls/subprocess_repl.py with
env = dict(line.split('=', 1) for line in lines if '=' in line)
(Thanks #MichaelOhlrogge for the shorter syntax)
Why this works
#develucas's solution helped me solve my issue. I didn't have the problem he was describing, but his investigation helped.
In my case, the login shell had a greeting. So, bash --login -c env (the command specified in SublimeREPL.sublime-settings file under the getenv_command option) was printing something like this:
Hello, parth!
USER=parth
SHELL=/bin/bash
.
.
.
It turns out that SublimeREPL uses the output of this command to load the environment variables - as mentioned in the comment above the getenv_command setting:
// On POSIX system SublimeText launched from GUI does not inherit
// a proper environment. Often leading to problems with finding interpreters
// or not using the ones affected by changes in ~/.profile / *rc files
// This command is used as a workaround, it's launched before any subprocess
// repl starts and it's output is parsed as an environment
"getenv_command": ["/bin/bash", "--login", "-c", "env"],
The code that parses this output is like this (in the ~/.config/sublime-text-3/Packages/SublimeREPL/repls/subprocess_repl.py file for ST3):
def getenv(self, settings):
"""Tries to get most appropriate environent, on windows
it's os.environ.copy, but on other system's we'll
try get values from login shell"""
getenv_command = settings.get("getenv_command")
if getenv_command and POSIX:
try:
output = subprocess.check_output(getenv_command)
lines = output.decode("utf-8", errors="replace").splitlines()
env = dict(line.split('=', 1) for line in lines)
return env
except:
import traceback
traceback.print_exc()
error_message(
"SublimeREPL: obtaining sane environment failed in getenv()\n"
"Check console and 'getenv_command' setting \n"
"WARN: Falling back to SublimeText environment")
# Fallback to environ.copy() if not on POSIX or sane getenv failed
return os.environ.copy()
The env = dict(line.split('=', 1) for line in lines) line causes an issue, because the first line in the bash --login -c env output has no =. So I modified this line to ignore the lines that don't have an = sign:
env = dict(line.split('=', 1) for line in lines if '=' in line)
And this solved the issue for me. Don't forget the restart Sublime Text after modifying this file.
changing COMP_WORDBREAKS does not work for me ...
i'm using ST2, and the exception was thrown at check_output().
also, name completions at the command line fails, after changing COMP_WORDBREAKS.
in my case, i changed subprocess_repl.py's env() method:
[wind]$ diff subprocess_repl.py.20151117.173317 subprocess_repl.py
160c160,161
< updated_env = env if env else self.getenv(settings)
---
> # updated_env = env if env else self.getenv(settings)
> updated_env = env if env else os.environ.copy()
[wind]$
would be interesting to find out why the problem started appearing suddenly ...
hth,cheers,
sam
The answer from #develucas mostly works for me with ST3 with OSX El Capitan except I had to put
export COMP_WORDBREAKS="${COMP_WORDBREAKS#???}"
Note the export. However, if I do this, tab completion no longer works.
I had the same problem, it was my .bash_profile that had some utility outputs, such as a welcome message etc.
These outputs are parsed by SublimeREPL to try to extract environment variables from the output of the env command, and the extraneous text lines mixed together provoked the error.
(I'd like to make a PR to SublimeREPL to try to make that phase more robust, it should not depend on particular .bash_profile implementations!)

How to read text from an application window using pywinauto

I have a python code which opens a SSH session using Putty and passes a command to reboot a remote machine using pywinauto.
I want to read the text from the putty terminal after typing the password and compare it
Is there a way I can do it?
Below is the piece of code for the same
app_Putty = application.Application()
app_Putty.start_("C:\Users\debajyoti.bose\Downloads\putty.exe")
app_Putty.top_window_().TypeKeys(IP)
app_Putty.top_window_().TypeKeys("{TAB}"+"22")
app_Putty.top_window_().RadioButton4.Click()
app_Putty.top_window_().OpenButton.Click()
time.sleep(10)
app_Putty.top_window_().NoButton.Click()
time.sleep(2)
app_Putty.top_window_().TypeKeys(user+"{ENTER}")
time.sleep(3)
app_Putty.top_window_().TypeKeys(password+"{ENTER}")
time.sleep(3)
app_Putty.top_window_().TypeKeys("/bin/reboot"+"{ENTER}")
time.sleep(5)
app_Putty.kill_()
time.sleep(120)
I am using pywinauto v0.4.0
Thanks in advance.
OK, let's try app_Putty.top_window_().WindowText(). If it fails your mission looks impossible.
You can't capture directly like this from what I can tell, but I had to find a workaround, and the one I found was this
#clear the buffer with alt space menu
app.window(title='PuTTY - Title').type_keys('% l',with_spaces=True)
#copy the buffer to clipboard
app.window(title='PuTTY - Title').type_keys('% o',with_spaces=True)
I was having to do this because the putty.log file was missing the selection indicator icon (asterisk) on the screen when it was logging output and I needed a way to know which item was selected to move up or down.

How can I find why system can not run my application?

I have a c++ program that run a command and pass some arguments to it. The code is as follow:
int RunApplication(fs::path applicationPathName,std::string arguments)
{
std::string applicationShortPath=GetShortFileName(applicationPathName);
std::string cmd="\""+applicationShortPath +"\" "+ arguments+" >>log.txt 2>&1 \"";
std::cout<<cmd<<std::endl;
int result=std::system(cmd.c_str());
return result;
}
When I run system command, the cmd window appears shortly and then closes, but the result is 1 and the cmd was not run (the command should generate output which is not generated).
To check that the cmd is correct, I stopped the application just before system line and copy/ paste cmd content to a cmd window and it worked.
I am wondering how can I find why application is not run in system()?
the cmd has this value just before running it:
"D:/DEVELO~3/x64/Debug/enfuse.exe" -w --hard-mask --exposure-weight=1 --saturation-weight=0.328 --contrast-weight=0.164 -o "C:/Users/m/AppData/Local/Temp/1.tif" "C:/Users/m/AppData/Local/Temp/1.jpg" "C:/Users/m/AppData/Local/Temp/2.jpg" >>log.txt 2>&1 "
How can I find why it is not working?
Is there any way that I set the system so it doesn't close cmd window so I can inspect it?
is there any better way to run a command on OS?
Does Boost has any solution for this?
Edit
After running it with cmd /k, I get this error message:
The input line is too long.
How can I fix it other than reducing cmd line?
There are two different things here: if you have to start a suprocess, "system" is not the best way of doing it (better to use the proper API, like CreateProcess, or a multiplatform wrapper, but avoid to go through the command interpreter, to avoid to open to potential malware injection).
But in this case system() is probably the right way to go since you in fact need the command interpreter (you cannot manage things like >>log.txt 2>&1 with only a process creation.)
The problem looks like a failure in the called program: may be the path is not correct or some of the files it has to work with are not existent or accessible with appropriate-permission and so on.
One of the firt thing to do: open a command prompt and paste the string you posted, in there. Does it run? Does it say something about any error?
Another thing to check is how escape sequence are used in C++ literals: to get a '\', you need '\\' since the first is the escape for the second (like \n, or \t etc.). Although it seems not the case, here, it is one of the most common mistakes.
Use cmd /k to keep the terminal: http://ss64.com/nt/cmd.html
Or just spawn cmd.exe instead and inspect the environment, permissions, etc. You can manually paste that command to see whether it would work from that shell. If it does, you know that paths, permssions and environment are ok, so you have some other issue on your hands (argument escaping, character encoding issues)
Check here How to execute a command and get output of command within C++ using POSIX?
Boost.Process is not official yet http://www.highscore.de/boost/process/

popen and system behaves unexpectedly with multiple quoted file paths

I am trying to execute a dos command from within my C++ program, however soon as I add quotes to the output filepath (of a redirection) the command no longer gets executed and returns instantly. I've shown an example below of a path without spaces, but since paths may have spaces and thus be quoted for the shell to understand it properly I need to solve this dilemma - and I'm trying to get the simplest case working first.
i.e.
The following WORKS:
sprintf(exec_cmd,"\"C:/MySQL Server 5.5/bin/mysqldump.exe\" -u%s -p%s %s > C:/backup.bak",user,password,db_name);
system(exec_cmd);
The following does NOT work (notice the quotes around the output):
sprintf(exec_cmd,"\"C:/MySQL Server 5.5/bin/mysqldump.exe\" -u%s -p%s %s > \"C:/backup.bak\"",user,password,db_name);
system(exec_cmd);
I'm guessing it is choking somewhere. I've tried the same "exec_cmd" in popen to no avail.
Any help/advice is greatly appreciated.
I don't think your shell (cmd.exe) allows redirection to a file name with spaces. I couldn't make my command.com from DOS 6.22 accept it (I don't have a cmd.exe nearby to test).
Anyway, you can use the --result-file option to pass the redirection to the command itself.
mysqldump ... --result-file="file name" ...