Powershell how to make a hint change when a key is pressed?

I am new to Powershell. I am trying to create my own prompt that shows a truncated version of the current directory, unless you expand it by pressing CTRL + R Moreover, I would like to do this:

 $short\dir\path...> *user presses CTRL+R* $really\really\long\full\dir\path> *Full dir path is shown as long as CTLR + R is held down* 
+6
source share
3 answers

I don’t think there is a way to do this, but I can share with you a special invitation that I like to use:

 function prompt { $Host.UI.RawUI.WindowTitle = Get-Location "[$env:COMPUTERNAME] PS> " } 

This puts the current computer name in the prompt (maybe you don’t need to), but puts the current path in the window title, where long paths are not particularly consistent and do not clutter the prompt.

0
source

The following is a way to do this, but this involves a lot of trade-offs. At first it is only while working in the PS console, and not in ISE, or not in any application that hosts the PS. Secondly, it will require a lot of work, depending on how, ummm, the “smooth” you need. Thirdly, this is likely to cause other unexpected things.

Basically, you are writing a script for a prompt function that checks for a keyboard that looks for either Ctrl-R or any other action character. By the symbol "action" I mean something like input, tabs, backspace, up arrows, down arrows, etc., which causes something. If the script sees Ctrl-R, you can print the full path, either in the title bar or in the console. If you print it to the console, then your script will have to figure out which console line contains the current prompt. The Rawui member contains this information.

A bare outline might look like this:

  do { sleep -milli 250 if ($host.ui.rawui.keyavailable) { $key = $host.ui.rawui.readkey() if ($key.character -eq [char]'x') { write-host burp } else { break} } } while ($true) 

Every quarter second, the script sees if the key is available. If so, he reads the key. If the key is "x" (ctrl-r is for you), it displays a burp. But you change the burp to the full path, as well as the logic, to put the message in the right place (header line or right line on the console).

If the key is not an "x", this simple example is simply returned. This means that "x" gets "eaten", and the functionality of "x" no longer works until the next prompt is displayed. However, instead, you could handle all the action keys yourself, rather than returning. If you press the up arrow, you will get the previous command and display it on the console. If you click on the background, you must delete the character in front of the cursor. If the tab is clicked, you will have to interact with the built-in PS command (not sure if this is possible). Etc.

In the end, when the user presses return, you need to either pass all the keys that make up the command line to the console (you can use the SendKeys method for the WScript host), or you can execute the command directly from you call the function using Invoke-Expression (iex ) If you use iex, then the command history that PS supports (i.e. get-history) will not work unless you figure out a way to fix it (maybe Add-History?). If you use SendKeys, there is a chance that someone would quickly type in his key strokes interspersed with SendKeys.

As a compromise, instead of processing all the "actions" yourself, you can simply exit the prompt when a key is pressed except ctrl-r and use SendKeys to send this one key to the console to prevent the char from getting eaten. That way, you could possibly go through 20 lines of script instead of a few hundred, by using ctrl-r just before any characters are typed.

Another complication. The readkey () function allows you to do both keystrokes and keystrokes. That way, you can display the full path as long as you press "r" in ctrl-r. However, your script should understand the up / down keys elsewhere. It can be as simple as simply ignoring the keystrokes down.

There is one bright side to this. You could implement key processing functionality beyond what PS usually does. Therefore, if you want ctrl-t to display the current time, you could do it. If you want ctrl-w to delete the current word under the cursor, you could.

0
source

This is not a very elegant solution, but it basically turns Ctrl + Alt + R into a hotkey to display the current directory. You can change the chord to "Ctrl + R", but you will overwrite the default PSReadlineKeyHandler for powershell. It looks like he's stuck in a team, but if you start typing, the next line will appear. You will need to add both of them to Microsoft.Powershell_Profile.ps1 located in $env:USERPROFILE\Documents\WindowsPowershell\

 function prompt { if(($pwd | split-path -Parent | Split-Path -Leaf) -match ':') { "$pwd\>" } elseif (($pwd | Split-Path -Leaf) -match ':') { "$pwd>" } else { "...\$($pwd | split-path -Parent | Split-Path -Leaf)\$($pwd | Split-path -Leaf)\>" } } Set-PSReadlineKeyHandler -Chord "Ctrl+Alt+R" -ScriptBlock { write-host $pwd -nonewLine } 

Hope this helps!

0
source

All Articles