某天闲得无聊想要通过 PowerShell 的脚本修改 powershell 的提示符,让 IDEA 的终端显示更加鲜艳简洁,最终做到如下效果
- 修改提示符颜色
- 修改提示符提示普通用户和管理员权限
- 修改路径表现形式
- 修改项目根路径以 ~ 表示
这是以管理员形式启动的IDEA,这时候终端是有管理员权限的,所以样式被改变了
实现流程
- 创建一个
ztp_function_idea.ps1
文件(该文件需要在配置在环境变量,如果没有则需要使用的时候键入完整路径)
function prompt { $dir = (Get-Location).Path; if ($null -eq $env:WORK) { $env:WORK = $dir } $name = $env:USERNAME.ToLower() # 定义路径映射:每个元素是路径表达式和对应简称的元组 $PathAliases = @( @{ Path = $env:USERPROFILE; Alias = 'home' } @{ Path = $env:WORK; Alias = '~' } @{ Path = [Environment]::GetFolderPath('Desktop'); Alias = 'Desktop' } @{ Path = [Environment]::GetFolderPath('MyDocuments'); Alias = 'Documents' } @{ # Personal 文件夹且不等于 MyDocuments(避免重复) Path = { [Environment]::GetFolderPath('Personal') }; Condition = { $dir -eq $_.Invoke() -and $dir -ne [Environment]::GetFolderPath('MyDocuments') } Alias = 'Documents' } @{ Path = [Environment]::GetFolderPath('Startup'); Alias = 'Startup' www.devze.com} @{ Path = [Environment]::GetFolderPath('StartMenu'); Alias = 'StartMenu' } @{ Path = [Environment]::GetFolderPath('CommonStartup'); Alias = 'Startup' } @{ Path = [Environment]javascript::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]::GetFolderPhttp://www.devze.comath('SendTo'); Alias = 'SendTo' } @{ Path = [Environment]::GetFolderPath([Environment+SpecialFolder]::NetworkShortcuts); Alias = 'NetHood' } @{ Path = [Environment]::GetFolderPath([Environment+SpecialFolder]::PrphpinterShortcuts); 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 # 遍历数组查找匹配项 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" # 蓝色 $RESET = "$ESC[0m" # 重置(关闭所有样式) $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 = "$ " } # 组合输出:绿色加粗用户名 + 重置后加冒号 + 蓝色路径 + 重置 + 提示符 "$GREEN_BOLD$userPart$RESET`:$BLUE$pathPart$RESET$promptChar" # "root@$name`:$dir# " } # todo 启动一个终端并应用上面的函数 %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'"
到此这篇关于IDEA 通过脚本配置终端提示符样式的方法的文章就介绍到这了,更多相关idea终端提示符内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面python的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论