Powershell profile, dynamic prompt and ansi color escape codes being reset after git has executed - powershell

I am trying to duplicate my cmder dynamic prompt inside Powershell.
This dynamic prompt uses a combination of some specific unicode characters and background and foreground colors to produce a prompt that can contain items, such as:
Current directory (white on blue)
Current git branch (black on green)
Ahead/behind (black on yellow or white)
Special status (merge, rebase, etc. - black on red)
The cmder prompt works wonders, and I've managed to reproduce most, if not all, of it using a Microsoft.Powershell_profile.ps1 script with code.
However, after running some git commands, like git pull, the powershell prompt becomes messed up. The symptoms are that handling of those escape codes are suddenly turned off. This does not happen if I only use cmder.
Example:
The top git lg (git log on one line, decorated) command is running from cmder, notice that the prompt comes back with colors.
Then I switch to powershell right above the middle, notice I got the same looking prompt there, only this is produced from the powershell profile script.
However, after running git lg once again, only now from Powershell, the handling of the escape codes are disabled and the prompt now becomes totally messed up.
My question is:
Is there anything I can do in the powershell profile to enable this before producing the prompt, or to prevent this from happening?
Here is a very simple powershell profile file:
function prompt {
"" + [char]0x001b + "[39;44mTEST" + [char]0x001b + "[0m" + [char]0x000a + "> "
}
Simply drop this into a file named as:
C:\Users\<USERNAME>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
(make sure to make a backup of the file if it already exists!)
Then start Powershell and you should be able to reproduce this:
Notice that last line before the prompt there contains the escape codes verbatim.
Things I've tried:
Outputting ESC c: Resets the device to its original state. (no apparent effect or change)
Running Powershell directly (not from within cmder), this works exactly the same as above, only the Windows has different initial colors, such as a blue background. The coloring of my prompt seems to work fine, the special characters doesn't though but that's to be expected (the branch symbol and that triangle), but after git lg, still messed up.

Related

VSCode Integrated Terminal With no Word Wrap Still Inserts Line Breaks (but with no Carriage Return)

I would like to turn off word wrap in my vscode terminal so I can more easily view compiler messages.
e.g.
Imagine this is a very long line generated from a compiler and it has a lot of information and when it word wraps it is very hard to read so I would like vs code to just have a scroll bar at the bottom like SO has here.
However, after attempting several different methods of disabling word wrap, what is actually generated looks like:
Imagine this is a very long line generated from a compiler and it
t has a lot of information and when it word wraps it is very hard to read so I would like vs code to just have a scroll bar at the bottom like SO has here.
(extra t on the second line is intentional)
So it appears that I've managed to turn off word wrap internal to vscode, but something is still causing a carriage return (with no line feed), I think?
These messages are produced by running a C++ build task in the integrated terminal that in turn calls cmd.exe to run a script (e.g. cmd.exe <build command script>
Here is what I've tried so far:
First thing I tried was turning off word wrap in vscode (alt + z, or View > Word Wrap). This led to the weird line break described above.
I was suspicious that cmd was the one causing this weird line break, so I disabled "wrap text output on resize" and set the screen buffer width manually to 1000 (1000 >> greater than any of my output lines) within
command prompt properties for both "properties" and "default". I then tested the same command running in my vscode terminal and it worked as expected in the cmd terminal but still not vscode.
I was suspicious powershell (which I think vs code uses in it's integrated terminal?) was the one causing the line break, so I set the properties the same as in the cmd image above. Again this worked as expected in the powershell terminal, but still not vscode.
Following this, I changed the "terminal fixed dimensions" using the command from the command pallet and set the width to a matching column width of 1000 (leaving row empty for "automatic").
I've tried restarting vscode and my pc between several of these steps with no change.
What could be causing this behavior inside of the vscode terminal?

Weird cases when drag and droping files in to vscode terminal

My problem
I am currently working with Python in VS Code and I encountered a weird problem with the drag and drop feature. When I drag a folder path or a file path into the Terminal, then it always begins with the &
sign. This only happens when I start my program.
Here is an example:
& 'filepath' or
& "filepath"
I don't have a problem with the quotes but with the & and the blankspace. I can't find anything online, I was wondering if anybody else has this problem.
Thanks in advance :)
What did you try and what were you expecting?
Well, I did try it with other files and folders, but it didn't help.
I've also tried running the code outside VS Code and drag and dropping was fine. So I guess it has something to do with VS Code.
This happens in certain conditions when the path contains a space or parenthesis and the current terminal is powershell based.
I'm not sure why this was added, but the mechanism happens here:
src/vs/workbench/contrib/terminal/browser/terminalInstance.ts:2664
if (isPowerShell && (hasSpace || originalPath.indexOf('\'') !== -1)) {
return `& '${originalPath.replace(/'/g, '\'\'')}'`;
}
if (hasParens && isPowerShell) {
return `& '${originalPath}'`;
}
The original PRs introducing this change are here and here
The rationale seems to be to run files with certain symbols in their name:
single quotes in paths for PowerShell (i.e /Users/tyler/tyler's stuff/ -> & '/Users/tyler/tyler''s stuff/' to escape a ' in a ' ' string, you add an extra ')
The smallest workaround is to not open a powershell terminal.
So either open a terminal with the drop-down next to the "+" (#command:workbench.action.terminal.newWithProfile) which should have other entries next to powershell like "Command Prompt"(cmd.exe).
If you want to make this the default select the terminal profile then use the "Terminal: Select Default Profile"
(#command:workbench.action.terminal.selectDefaultShell) command to change the default.
If you want this to be configurable or not happen when the foreground application is no longer powershell you should probably file an issue with vscode.

Scrolling "too long" command doesn't work properly

Assume I want to invoke a tool with a lot of options, like:
$ somescript --option1 --option2 --option3 --option4 --option5 whatever even more stuff
But the width of the terminal doesn't allow to have that thing on a "single line".
That alone isn't a problem, but with fish, there is a problem with scrolling.
When I enter that extra-wide command, at some point, there will be a line break:
$ somescript --option1 --option2 --option3 --option4 --option5
whatever even more stuff
All fine so far. While typing such a lengthy command, I can scroll forth and back at any point. Works as expected. So, assume I entered the command, made a mistake, and now want to remove --option4.
The normal thing: use "arrow up" to get to that command in the history, to then use "arrow left" to scroll within the command. And now something weird happens.
Everything is fine while the cursor is within the second line. But when the cursor moves to the first line ... after 3 to 5 more "arrow left" strokes, the cursor moves UP another line. Then it sits above the first line of the command. There is also a vertical jump of a few characters.
In other words: as soon as a command is longer than the width of the terminal, I am unable to scroll into the first line reliably. The cursor shows up somewhere, and it is really hard to guess where it really sits at any moment. Which makes it almost impossible to edit anything in that first line.
This is on MacOs, using iterm 3.2.9 and fish version 3.0.2 installed via brew.
Wrote up defect 6014 on github. Outcome is rather frustrating.
The problem is: the Mac terminal(s) all handle certain unicode characters the wrong way. "Normally" that isn't a problem, but when you use one of those fish prompts that give you the git status (like AcidHub which is my favorite) ... fish can't compute/determine the exact line width, and there you go.
So, basically, when using fish on MacOs, option space is limited to:
use the defaults, which (when using special prompts, like AcidHub will lead to scrolling issues)
adapt the fish prompt accordingly (in my case, I replaced all special unicode chars with something simpler). It doesn't look that great, but scrolling simply works again.
And a completely different and unexpected solution to the problem: I am using iterm2 on my Mac, and iterm2 just added a "status bar" section. That can be easily configured, and of course, it already has a status bar component that tells you about git status.
Thus my solution: I changed the fish prompt to just give the PWD, and all the other things that the AcidHub prompt has to offer, are now "iterm2 status bar" components!

How to type a tab (indent) in comments in PowerShell ISE?

This is weird.
I know tab is for command completion in the PowerShell ISE and fine so. But, it also messes up the editing pane.
Do this:
File > New (Untitled1.ps1 opens)
Press tab (all fine, you get an indent)
type enter, # (comment) and press tab after it
expected: one would get indentation after the hash
actual: one gets the hash replaced by $PSVersionTable or whatever the command prompt has in its history! (tab and Shift-tab circle through those)
Does this mean no-one uses tabs within comments in PowerShell scripts, or that no-one uses comments in PowerShell scripts?
Can I turn off this behavior anywhere?
Also, the behavior seems to be inconsistent. If I e.g. type ##, sometimes tab does not do the completion (it does not enter a tab either).
Can others reproduce this?
System:
Windows 8.1 Pro
PowerShell ISE
To answer the main question, you can enter Alt+09 (using numeric keypad) to enter <Tab>.
For the behavior described, I see this as expected behavior. You can get history completion by typing # and part of a previous command then pressing Tab repeatedly will cycle backwards through matching history. Typing # alone will match all history starting with the last command.
Does this mean no-one uses tabs within comments in PowerShell scripts?
Anecdotal, but I've never used tabs in a single line comment, but I do often use tabs within multiline comments which are bracketed by <# and #>. E.g.
<#
Functions
Get-Foo
Get-Bar
Variables
$Foo
$Bar
#>
Function Get-Foo { ...
With multiline comments, the auto-completion will not be an issue.
, or that no-one uses comments in PowerShell scripts?
I don't know why this would be implied by the behavior; I always use a single space to start a line comment.
I find this helpful when developing a script as I often try expressions in the command pane if I'm unsure of the behavior, then add the expression to the script if it works.
So, my workflow would be:
Ctrl-D to go to the Command Pane
Type a command
If the command did what I wanted, Ctrl-I to go to the Script Pane
Type #<Tab>, and the line is added to the script.

Would it be possible to jump between prev/next Terminal command prompts?

I'm using zsh in OS X Terminal.app and for quite a while, I've been longing for a way to jump back and forth between prev/next prompts in the terminal's output.
One convenience with this would be to be able to review (and track errors at) the end of each command's output; eg. when you building stuff from source with ./configure; make; make install. Note: I'm obviously not referring to jumping back and forth in the command-history, but for a way to take a peek at the endings of each command's output.
Has anyone heard of such functionality in the *nix (preferrably also Mac) world? Would it require some sort of OS-centric Terminal plugin, or can it be programmatically done via a shell script which can be tied to a keyboard shortcut? Maybe I'm the only one thinking about this? :)
Edit: Here's an example scenario: Let's say I want to compile and install some program (using standard ./configure && make && make install procedure) and after the make command, I run into some errors. Now, the way I understand it (I may be completely wrong), the crucial error causing the make command to fail usually shows up in the last line(s) in the output, no? Anyway, at this point, I might do something like cat INSTALL to read up on the INSTALL document to check whether there's something I've missed. NOW, if I want to go back to see what the error was, that caused my initial make command to fail, I then have to manually scroll up to that position again, since my cat INSTALL command printed a ton of text after it.
I don't know if this scenario is the most elucidative – but there are many other situations where I wish I could just "jump" back to previous prompt lines and check up on previous command output; whether it was a simple ls command, make, git status, or whatever it was – swapping positions in the window by means of using prompt lines as "bookmark" positions seems an interesting idea to me.
command + left or right goes between tabs in iterm. is this what you are asking?
Emacs has a shell-mode that runs a shell inside the Emacs editor, providing a rich environment of additional commands for navigating and working with shell commands. This includes commands for going to the previous/next prompt, and deleting the output from commands so you can "clean up" and issue another command.
If you aren't familiar with Emacs: to start a shell inside Emacs, run emacs from the shell, then type Esc-x (or Meta-x, if you have "Use option as meta key" enabled in Terminal > Preferences > Settings > [profile] > Keyboard). This will ask for a command to execute. Enter shell.
To see a list of commands you can use in Shell Mode, enter Control-h m. Here are the ones for moving the cursor to the previous/next prompt:
C-c C-n comint-next-prompt
C-c C-p comint-previous-prompt
These commands would also be useful:
C-c C-r comint-show-output
C-c C-o comint-delete-output