mirror of
https://github.com/starship/starship.git
synced 2025-03-12 22:41:29 +01:00
add preprompt and precmd support
This commit is contained in:
parent
f83d15f99c
commit
7585505de1
@ -10,6 +10,38 @@ The configurations in this section are subject to change in future releases of S
|
||||
|
||||
:::
|
||||
|
||||
## Custom pre-prompt and pre-execution Commands in Cmd
|
||||
|
||||
Clink provides extremely flexible APIs to run pre-prompt and pre-exec commands
|
||||
in Cmd shell. It is fairly simple to use with Starship. Make the following changes
|
||||
to your `starship.lua` file as per your requirements:
|
||||
|
||||
- To run a custom function right before the prompt is drawn, define a new
|
||||
function called `starship_preprompt_user_func`. This function receives
|
||||
the current prompt as a string that you can utilize. For example, to
|
||||
draw a rocket before the prompt, you would do
|
||||
|
||||
```lua
|
||||
function starship_preprompt_user_func(prompt)
|
||||
print("🚀")
|
||||
end
|
||||
|
||||
load(io.popen('starship init cmd'):read("*a"))()
|
||||
```
|
||||
|
||||
- To run a custom function right before a command is executed, define a new
|
||||
function called `starship_precmd_user_func`. This function receives
|
||||
the current commandline as a string that you can utilize. For example, to
|
||||
print the command that's about to be executed, you would do
|
||||
|
||||
```lua
|
||||
function starship_precmd_user_func(line)
|
||||
print("Executing: "..line)
|
||||
end
|
||||
|
||||
load(io.popen('starship init cmd'):read("*a"))()
|
||||
```
|
||||
|
||||
## Custom pre-prompt and pre-execution Commands in Bash
|
||||
|
||||
Bash does not have a formal preexec/precmd framework like most other shells.
|
||||
@ -62,7 +94,7 @@ function Invoke-Starship-PreCommand {
|
||||
Some shell prompts will automatically change the window title for you (e.g. to
|
||||
reflect your working directory). Fish even does it by default.
|
||||
Starship does not do this, but it's fairly straightforward to add this
|
||||
functionality to `bash` or `zsh`.
|
||||
functionality to `bash`, `zsh`, `cmd` or `powershell`.
|
||||
|
||||
First, define a window title change function (identical in bash and zsh):
|
||||
|
||||
@ -100,6 +132,16 @@ function set_win_title(){
|
||||
starship_precmd_user_func="set_win_title"
|
||||
```
|
||||
|
||||
For Cmd, you can change the window title using the `starship_preprompt_user_func` function.
|
||||
|
||||
```lua
|
||||
function starship_preprompt_user_func(prompt)
|
||||
console.settitle(os.getenv('USERNAME').."@"..os.getenv('COMPUTERNAME')..": "..os.getcwd())
|
||||
end
|
||||
|
||||
load(io.popen('starship init cmd'):read("*a"))()
|
||||
```
|
||||
|
||||
You can also set a similar output with PowerShell by creating a function named `Invoke-Starship-PreCommand`.
|
||||
|
||||
```powershell
|
||||
@ -121,7 +163,7 @@ not explicitly used in either `format` or `right_format`.
|
||||
Note: The right prompt is a single line following the input location. To right align modules above
|
||||
the input line in a multi-line prompt, see the [fill module](/config/#fill).
|
||||
|
||||
`right_format` is currently supported for the following shells: elvish, fish, zsh.
|
||||
`right_format` is currently supported for the following shells: elvish, fish, zsh, cmd.
|
||||
|
||||
### Example
|
||||
|
||||
|
@ -33,6 +33,12 @@ Equivalently in PowerShell (Windows) would be adding this line to your `$PROFILE
|
||||
$ENV:STARSHIP_CONFIG = "$HOME\.starship\config.toml"
|
||||
```
|
||||
|
||||
Or for Cmd (Windows) would be adding this line to your `starship.lua`:
|
||||
|
||||
```lua
|
||||
os.setenv('STARSHIP_CONFIG', 'C:\\Users\\user\\.starship\\config.toml')
|
||||
```
|
||||
|
||||
### Logging
|
||||
|
||||
By default starship logs warnings and errors into a file named `~/.cache/starship/session_${STARSHIP_SESSION_KEY}.log`, where the session key is corresponding to a instance of your terminal.
|
||||
@ -48,6 +54,12 @@ Equivalently in PowerShell (Windows) would be adding this line to your `$PROFILE
|
||||
$ENV:STARSHIP_CACHE = "$HOME\AppData\Local\Temp"
|
||||
```
|
||||
|
||||
Or for Cmd (Windows) would be adding this line to your `starship.lua`:
|
||||
|
||||
```lua
|
||||
os.setenv('STARSHIP_CACHE', 'C:\\Users\\user\\AppData\\Local\\Temp')
|
||||
```
|
||||
|
||||
### Terminology
|
||||
|
||||
**Module**: A component in the prompt giving information based on contextual information from your OS. For example, the "nodejs" module shows the version of Node.js that is currently installed on your computer, if your current directory is a Node.js project.
|
||||
|
@ -13,6 +13,9 @@ clink.onbeginedit(function ()
|
||||
end)
|
||||
|
||||
clink.onendedit(function (curr_line)
|
||||
if starship_precmd_user_func ~= nil then
|
||||
starship_precmd_user_func(curr_line)
|
||||
end
|
||||
start_time = os.clock()
|
||||
if string.len(string.gsub(curr_line, '^%s*(.-)%s*$', '%1')) == 0 then
|
||||
is_line_empty = true
|
||||
@ -22,6 +25,9 @@ clink.onendedit(function (curr_line)
|
||||
end)
|
||||
|
||||
function custom_prompt:filter(prompt)
|
||||
if starship_preprompt_user_func ~= nil then
|
||||
starship_preprompt_user_func(prompt)
|
||||
end
|
||||
return io.popen("::STARSHIP:: prompt"
|
||||
.." --status="..os.geterrorlevel()
|
||||
.." --cmd-duration="..math.floor(curr_duration*1000)
|
||||
|
Loading…
Reference in New Issue
Block a user