IDEA 通過腳本配置終端提示符樣式的方法
更新時間:2025年08月19日 11:16:02 作者:zimoyin
這篇文章給大家介紹IDEA通過腳本配置終端提示符樣式的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
某天閑得無聊想要通過 powershell 的腳本修改 powershell 的提示符,讓 IDEA 的終端顯示更加鮮艷簡潔,最終做到如下效果
- 修改提示符顏色
- 修改提示符提示普通用戶和管理員權(quán)限
- 修改路徑表現(xiàn)形式
- 修改項(xiàng)目根路徑以 ~ 表示

這是以管理員形式啟動的IDEA,這時候終端是有管理員權(quán)限的,所以樣式被改變了

實(shí)現(xiàn)流程
- 創(chuàng)建一個
ztp_function_idea.ps1文件(該文件需要在配置在環(huán)境變量,如果沒有則需要使用的時候鍵入完整路徑)
function prompt {
$dir = (Get-Location).Path;
if ($null -eq $env:WORK) {
$env:WORK = $dir
}
$name = $env:USERNAME.ToLower()
# 定義路徑映射:每個元素是路徑表達(dá)式和對應(yīng)簡稱的元組
$PathAliases = @(
@{ Path = $env:USERPROFILE; Alias = 'home' }
@{ Path = $env:WORK; Alias = '~' }
@{ Path = [Environment]::GetFolderPath('Desktop'); Alias = 'Desktop' }
@{ Path = [Environment]::GetFolderPath('MyDocuments'); Alias = 'Documents' }
@{
# Personal 文件夾且不等于 MyDocuments(避免重復(fù))
Path = { [Environment]::GetFolderPath('Personal') };
Condition = { $dir -eq $_.Invoke() -and $dir -ne [Environment]::GetFolderPath('MyDocuments') }
Alias = 'Documents'
}
@{ Path = [Environment]::GetFolderPath('Startup'); Alias = 'Startup' }
@{ Path = [Environment]::GetFolderPath('StartMenu'); Alias = 'StartMenu' }
@{ Path = [Environment]::GetFolderPath('CommonStartup'); Alias = 'Startup' }
@{ Path = [Environment]::GetFolderPath('CommonStartMenu'); Alias = 'StartMenu' }
@{ Path = [Environment]::GetFolderPath('Favorites'); Alias = 'Favorites' }
@{ Path = [Environment]::GetFolderPath([Environment+SpecialFolder]::ApplicationData); Alias = 'Roaming' }
@{ Path = [Environment]::GetFolderPath('LocalApplicationData'); Alias = 'LocalAppData' }
@{ Path = ${env:ProgramFiles(x86)}; Alias = 'ProgramFiles(x86)' }
@{ Path = [Environment]::GetFolderPath('Templates'); Alias = 'Templates' }
@{ Path = [Environment]::GetFolderPath('Recent'); Alias = 'Recent' }
@{ Path = [Environment]::GetFolderPath('SendTo'); Alias = 'SendTo' }
@{ Path = [Environment]::GetFolderPath([Environment+SpecialFolder]::NetworkShortcuts); Alias = 'NetHood' }
@{ Path = [Environment]::GetFolderPath([Environment+SpecialFolder]::PrinterShortcuts); Alias = 'PrintHood' }
@{ Path = [Environment]::GetFolderPath([Environment+SpecialFolder]::MyMusic); Alias = 'Music' }
@{ Path = [Environment]::GetFolderPath([Environment+SpecialFolder]::MyPictures); Alias = 'Pictures' }
@{ Path = [Environment]::GetFolderPath([Environment+SpecialFolder]::MyVideos); Alias = 'Videos' }
@{
Path = { (New-Object -ComObject Shell.Application).Namespace('shell:Downloads').Self.Path }
Alias = 'Downloads'
}
@{ Path = "$env:USERPROFILE\OneDrive"; Alias = 'OneDrive' }
@{ Path = "$env:PROGRAMFILES\WindowsApps"; Alias = 'WindowsApps' }
@{ Path = [Environment]::GetFolderPath('System'); Alias = 'System' }
@{ Path = [Environment]::GetFolderPath('CommonApplicationData'); Alias = 'ProgramData' }
@{ Path = [Environment]::GetFolderPath('ProgramFiles'); Alias = 'ProgramFiles' }
@{ Path = $env:WINDIR; Alias = 'Windows' }
)
$flag = 0
# 遍歷數(shù)組查找匹配項(xiàng)
foreach ($item in $PathAliases) {
$targetPath = if ($item.Path -is [scriptblock]) { $item.Path.Invoke() } else { $item.Path }
if ($dir.StartsWith($targetPath, [System.StringComparison]::OrdinalIgnoreCase)) {
if ($item.Condition) {
if($item.Condition.Invoke()){}
}
$relative = $( $suffix = $dir.Substring($targetPath.Length).TrimStart('\'); "/$suffix".TrimEnd('/') )
$dir = $item.Alias + $relative
$result = if ($item.Alias -eq '~') { '' } else { '/' }
$dir = $result + $dir.Replace('\', '/').Replace(':', '/') -replace '/+', '/'
$flag = 1
break
}
}
$dir = $dir.ToLower()
if ($flag -eq 0) {
# $dir = '/' + $dir.ToLower().Replace('\', '/').Replace(':', '/') -replace '/+', '/'
$dir = '/' + $dir.Replace('\', '/').Replace(':', '/') -replace '/+', '/'
$dir = $dir -replace '/$',''
}
# ? ANSI 控制字符
$ESC = [char]27
$GREEN_BOLD = "$ESC[1;32m" # 加粗 + 綠色
$BLUE = "$ESC[34m" # 藍(lán)色
$RESET = "$ESC[0m" # 重置(關(guān)閉所有樣式)
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
$userPart = "root@$name"
$pathPart = $dir
$promptChar = "# "
if (!$isAdmin) {
$userPart = "$name@$name"
$promptChar = "$ "
}
# 組合輸出:綠色加粗用戶名 + 重置后加冒號 + 藍(lán)色路徑 + 重置 + 提示符
"$GREEN_BOLD$userPart$RESET`:$BLUE$pathPart$RESET$promptChar"
# "root@$name`:$dir# "
}
# todo 啟動一個終端并應(yīng)用上面的函數(shù) %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit chcp 65001- 修改 IDEA 終端的路徑
c:\windows\system32\windowspowershell\v1.0\powershell.exe -NoExit -Command "chcp 65001; . 'ztp_function_idea'"

到此這篇關(guān)于IDEA 通過腳本配置終端提示符樣式的方法的文章就介紹到這了,更多相關(guān)idea終端提示符內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)輸出任意整數(shù)的每一位
這篇文章主要介紹了java實(shí)現(xiàn)輸出任意整數(shù)的每一位,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
詳解Mybatis-plus(MP)中CRUD操作保姆級筆記
本文主要介紹了Mybatis-plus(MP)中CRUD操作保姆級筆記,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
IDEA?2021.3?使用及idea2021.3.1激活使用方法
IDEA?全稱?IntelliJ?IDEA,是java語言開發(fā)的集成環(huán)境,IntelliJ在業(yè)界被公認(rèn)為最好的java開發(fā)工具之一,今天通過本文給大家介紹idea2021.3.1激活及使用教程,感興趣的朋友一起看看吧2022-01-01

