V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
systemcall
V2EX  ›  Windows

装了 oh-my-posh 后,启动 powershell 变得非常慢

  •  
  •   systemcall · 2023-03-14 20:04:13 +08:00 · 4307 次点击
    这是一个创建于 402 天前的主题,其中的信息可能已经有所发展或是发生改变。
    PowerShell 7.2.5
    Copyright (c) Microsoft Corporation.
    
    https://aka.ms/powershell
    Type 'help' to get help.
    
       A new PowerShell stable release is available: v7.3.3
       Upgrade now, or check out the release page at:
         https://aka.ms/PowerShell-Release?tag=v7.3.3
    
    Loading personal and system profiles took 20752ms.
    

    差不多这个水平,不知道为什么

    第 1 条附言  ·  2023-03-14 20:56:33 +08:00

    让new bing写了个启动的脚本,替换了之前的

    # 定义一个函数来测试网络连接
    function Test-Network {
        param(
            [string]$Url = '检测的URL',
            [int]$Timeout = 500
        )
        try {
            # 使用 Invoke-WebRequest 命令来发送 HTTP 请求,并设置超时时间
            $response = Invoke-WebRequest -Uri $Url -TimeoutSec ($Timeout / 1000) -UseBasicParsing -ErrorAction Stop
            # 检查响应状态码是否为 204
            if ($response.StatusCode -eq 204) {
                # 网络正常,返回 $true
                return $true
            }
            else {
                # 网络异常,返回 $false
                return $false
            }
        }
        catch {
            # 发生错误,返回 $false
            return $false
        }
    }
    
    # 定义一个函数来设置代理
    function Set-Proxy {
        param(
            [string]$Proxy = 'http://127.0.0.1:7890'
        )
        # 使用 netsh 命令来设置系统级别的代理,并忽略错误输出
        netsh winhttp set proxy $Proxy *>$null 
    }
    
    # 定义一个函数来取消代理
    function Unset-Proxy {
        # 使用 netsh 命令来取消系统级别的代理,并忽略错误输出
        netsh winhttp reset proxy *>$null 
    }
    
    # 定义一个函数来加载模块(根据您的实际情况修改)
    function Load-Modules {
       # 超出附言限制,略
    }
    
    # 调用 Test-Network 函数来测试网络连接情况,并将结果赋值给变量 $networkStatus 
    $networkStatus = Test-Network
    
    # 判断变量 $networkStatus 的值是否为 $true 
    if ($networkStatus) {
        # 如果为 $true ,表示网络正常,直接调用 Load-Modules 函数来加载模块,并结束脚本执行 
        Load-Modules 
        exit 
    }
    else { 
        # 如果为 $false ,表示网络异常,尝试调用 Set-Proxy 函数来设置代理,并再次调用 Test-Network 函数来测试网络连接情况,并将结果赋值给变量 $proxyStatus  
        Set-Proxy  
        $proxyStatus = Test-Network 
    
        # 判断变量 $proxyStatus 的值是否为 $true  
        if ($proxyStatus) {  
            # 如果为 $true ,表示代理有效,调用 Load-Modules 函数来加载模块,并结束脚本执行  
            Load-Modules  
            exit  
        }
        else {  
            # 如果为 $false ,表示代理无效,调用 Unset-Proxy 函数来取消代理,并抛出一个异常,提示网络问题,并不加载模块  
            Unset-Proxy  
            throw 'Network error: cannot access Google with or without proxy.'   
        }   
    }
    

    但是加载模块的过程是同步的,一共要花费2000多ms,有办法继续优化吗?
    感觉甚至不如js

    第 2 条附言  ·  2023-03-14 22:36:47 +08:00
    换了 starship ,按照 catppuccin/starship 来配置了主题,凑合用算了,启动还是不怎么快,只能说比以前快不少,但是比起来 zsh ,pwsh 就是垃圾
    18 条回复    2023-07-07 14:45:16 +08:00
    statumer
        1
    statumer  
       2023-03-14 20:08:17 +08:00
    holy shit, 20s? 别用 pwsh 了。
    Gavrain
        2
    Gavrain  
       2023-03-14 20:16:35 +08:00
    Windows PowerShell
    版权所有( C ) Microsoft Corporation 。保留所有权利。

    安装最新的 PowerShell ,了解新功能和改进! https://aka.ms/PSWindows

    加载个人及系统配置文件用了 872 毫秒。

    不知道题主用的什么环境啥主题,win11 才装的问题不大。
    richardzhao12138
        3
    richardzhao12138  
       2023-03-14 20:21:28 +08:00
    PowerShell 7.4.0-preview.1
    Loading personal and system profiles took 10596ms.
    我的也慢
    gloye
        4
    gloye  
       2023-03-14 20:29:06 +08:00
    应该是网络的原因,看看是否能够正常读取
    https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json
    yaoyao1128
        5
    yaoyao1128  
       2023-03-14 20:30:28 +08:00 via iPhone
    github 没正常连接到吧……
    systemcall
        6
    systemcall  
    OP
       2023-03-14 20:48:11 +08:00
    @gloye #4
    考虑了网络原因,问了一下 new bing ,让它写了个网络判断的脚本,现在一般情况下是 2000 多 ms 可以启动,第二次会快不少,不知道是 new bing 写的代码的问题,还是别的什么问题,还是 pwsh 本来就这么慢,反正是微软那边的问题
    systemcall
        7
    systemcall  
    OP
       2023-03-14 20:57:25 +08:00
    附言写不下了,补充一下我现在的真正的初始化代码:
    Import-Module PSReadLine
    Import-Module posh-git
    oh-my-posh init pwsh --config 'C:\Users\admin\AppData\Local\Programs\oh-my-posh\themes\catppuccin_mocha.omp.json' | Invoke-Expression
    Set-PSReadLineOption -PredictionSource History
    Set-PSReadLineKeyHandler -Key "Ctrl+d" -Function MenuComplete
    Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo
    Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
    Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
    Import-Module DirColors
    不知道哪里有问题
    rpish
        8
    rpish  
       2023-03-14 21:22:12 +08:00
    已经转到 starship ,这个模块加载就是那么耗时,没办法。
    Tumblr
        9
    Tumblr  
       2023-03-14 22:12:17 +08:00
    这个模块不至于这么慢啊,我也有在用这个模块,冷启动大概 2 3 秒,热启动 1 秒左右(使用中偶尔会卡一下,1 秒内)。
    我是这样调用的:oh-my-posh init pwsh -c 'D:\Program Files\oh-my-posh\themes\agnosterplus.omp.json' | Invoke-Expression
    anguliuyun
        10
    anguliuyun  
       2023-03-14 22:17:53 +08:00
    推荐一个 rust 写的跨平台 shell - [nushell]( https://github.com/nushell/nushell)
    FanyFull
        11
    FanyFull  
       2023-03-14 22:48:11 +08:00 via Android
    我也感觉很慢,而且,同样很慢的,还有 windows 上的 neovim.
    LokiSharp
        12
    LokiSharp  
       2023-03-14 23:47:56 +08:00
    pwsh 本来启动就慢 Emmmmm
    billly
        13
    billly  
       2023-03-15 00:25:15 +08:00
    我之前试过,也是很慢,就不用了
    Moha
        14
    Moha  
       2023-03-15 09:55:31 +08:00
    我一直都这么慢,真是醉了...........
    systemcall
        15
    systemcall  
    OP
       2023-03-15 10:04:22 +08:00
    @Tumblr #9
    我也觉得奇怪。我是 12 代 i7 的电脑,不应该这么慢
    hhjj3388
        16
    hhjj3388  
       2023-03-15 11:12:57 +08:00
    我的 i5 9400 的

    PowerShell 7.3.0-preview.8

    A new PowerShell preview release is available: v7.4.0-preview.2
    Upgrade now, or check out the release page at:
    https://aka.ms/PowerShell-Release?tag=v7.4.0-preview.2

    Loading personal and system profiles took 515ms.
          ﮫ 0ms   11:12 AM  
    hez2010
        17
    hez2010  
       2023-03-16 08:51:43 +08:00
    用旧版的 oh-my-posh 2.x ,旧版的 oh-my-posh 是 powershell 写的,加载速度很快,而新版本的 oh-my-posh 是用 go 写的,初始化巨慢无比。
    isajdhs
        18
    isajdhs  
       287 天前
    在项目 issue 见过这个问题,好像是因为启动时 Windows defender 会扫描 oh-my-posh ,加入白名单就行了,不过我现在用的 rust 写的 satrship ,主题较少
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1228 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 17:56 · PVG 01:56 · LAX 10:56 · JFK 13:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.