20240415 日記 nushell つづき

### nushellつづき

#### プロンプトにgitステータスを表示したい
nushell向けのgit-promptがあるのでは？と思ったがみつからない
starshipを使うのかもしれないがいまいちわからない
途方にくれてたら[nushell/awesome-nu: A curated list of awesome tools that work within the nu language ecosystem e.g. nushell, scripts, nana, etc.](https://github.com/nushell/awesome-nu)がみつかった
ためしに`cargo install nu_plugin_gstat`を実行したがエラーがでた
<details><summary>エラーが長いので折りたたんでいる</summary>
<div>

```nushell
> cargo install nu_plugin_gstat                                                            2024年04月14日 22時10分59秒
    Updating crates.io index
  Downloaded nu_plugin_gstat v0.92.2
  Downloaded 1 crate (18.1 KB) in 0.44s
  Installing nu_plugin_gstat v0.92.2
    Updating crates.io index
  Downloaded bincode v1.3.3
  Downloaded textwrap v0.16.1
  Downloaded windows-targets v0.52.5
  Downloaded vte_generate_state_changes v0.1.1
  Downloaded windows-result v0.1.1
  Downloaded unicode-width v0.1.11
  Downloaded unicase v2.7.0
  Downloaded wyz v0.5.1
  Downloaded byteorder v1.5.0
  Downloaded windows-core v0.54.0
  Downloaded toml_edit v0.21.1
  Downloaded url v2.5.0
  Downloaded sys-locale v0.3.1
  Downloaded winnow v0.5.40
  Downloaded vcpkg v0.2.15
  Downloaded simdutf8 v0.1.4
  Downloaded seahash v4.1.0
  Downloaded num-traits v0.2.18
  Downloaded serde_derive v1.0.197
  Downloaded semver v1.0.22
  Downloaded bytes v1.6.0
  Downloaded rayon-core v1.12.1
  Downloaded nu-plugin v0.92.2
  Downloaded windows_x86_64_msvc v0.52.5
  Downloaded serde v1.0.197
  Downloaded rand v0.8.5
  Downloaded miette v7.2.0
  Downloaded ntapi v0.4.1
  Downloaded rkyv v0.7.44
  Downloaded rust_decimal v1.35.0
  Downloaded serde_json v1.0.115
  Downloaded nu-protocol v0.92.2
  Downloaded sysinfo v0.30.10
  Downloaded pure-rust-locales v0.8.1
  Downloaded rayon v1.10.0
  Downloaded syn v1.0.109
  Downloaded syn v2.0.58
  Downloaded regex-syntax v0.8.3
  Downloaded idna v0.5.0
  Downloaded windows-sys v0.48.0
  Downloaded libssh2-sys v0.3.0
  Downloaded regex-automata v0.4.6
  Downloaded libc v0.2.153
  Downloaded git2 v0.18.3
  Downloaded chrono v0.4.37
  Downloaded bitvec v1.0.1
  Downloaded aho-corasick v1.1.3
  Downloaded hashbrown v0.12.3
  Downloaded libgit2-sys v0.16.2+1.7.2
  Downloaded fancy-regex v0.13.0
  Downloaded cc v1.0.94
  Downloaded borsh v1.4.0
  Downloaded allocator-api2 v0.2.18
  Downloaded ryu v1.0.17
  Downloaded getrandom v0.2.14
  Downloaded crossbeam-epoch v0.9.18
  Downloaded libz-sys v1.1.16
  Downloaded rmp-serde v1.1.2
  Downloaded quote v1.0.36
  Downloaded rmp v0.8.12
  Downloaded proc-macro-error v1.0.4
  Downloaded owo-colors v4.0.0
  Downloaded ppv-lite86 v0.2.17
  Downloaded nu-engine v0.92.2
  Downloaded nu-ansi-term v0.50.0
  Downloaded jobserver v0.1.30
  Downloaded thiserror-impl v1.0.58
  Downloaded thiserror v1.0.58
  Downloaded rand_core v0.6.4
  Downloaded erased-serde v0.4.4
  Downloaded crossterm_winapi v0.9.1
  Downloaded crossbeam-deque v0.8.5
  Downloaded byte-unit v5.1.4
  Downloaded borsh-derive v1.4.0
  Downloaded supports-hyperlinks v3.0.0
  Downloaded rkyv_derive v0.7.44
  Downloaded rend v0.4.2
  Downloaded proc-macro-error-attr v1.0.4
  Downloaded unicode-normalization v0.1.23
  Downloaded terminal_size v0.3.0
  Downloaded tap v1.0.1
  Downloaded supports-color v3.0.0
  Downloaded strip-ansi-escapes v0.2.0
  Downloaded smawk v0.3.2
  Downloaded rand_chacha v0.3.1
  Downloaded radium v0.7.0
  Downloaded pkg-config v0.3.30
  Downloaded percent-encoding v2.3.1
  Downloaded proc-macro-crate v3.1.0
  Downloaded paste v1.0.14
  Downloaded nu-utils v0.92.2
  Downloaded windows v0.54.0
  Downloaded nu-system v0.92.2
  Downloaded nu-glob v0.92.2
  Downloaded miette-derive v7.2.0
  Downloaded lscolors v0.17.0
  Downloaded windows v0.52.0
  Downloaded lru v0.12.3
  Downloaded windows-core v0.52.0
  Downloaded vte v0.11.1
  Downloaded uuid v1.8.0
  Downloaded unicode-bidi v0.3.15
  Downloaded ptr_meta_derive v0.1.4
  Downloaded nu-path v0.92.2
  Downloaded inventory v0.3.15
  Downloaded funty v2.0.0
  Downloaded form_urlencoded v1.2.1
  Downloaded either v1.11.0
  Downloaded dirs-sys-next v0.1.2
  Downloaded dirs-next v2.0.0
  Downloaded chrono-humanize v0.2.3
  Downloaded bytecheck v0.6.12
  Downloaded typetag-impl v0.2.16
  Downloaded typetag v0.2.16
  Downloaded tinyvec v1.6.0
  Downloaded omnipath v0.1.6
  Downloaded syn_derive v0.1.8
  Downloaded supports-unicode v3.0.0
  Downloaded ptr_meta v0.1.4
  Downloaded unicode-linebreak v0.1.5
  Downloaded toml_datetime v0.6.5
  Downloaded utf8-width v0.1.7
  Downloaded tinyvec_macros v0.1.1
  Downloaded is_ci v1.2.0
  Downloaded cfg_aliases v0.1.1
  Downloaded bytecheck_derive v0.6.12
  Downloaded bitflags v2.5.0
  Downloaded bit-vec v0.6.3
  Downloaded bit-set v0.5.3
  Downloaded ahash v0.7.8
  Downloaded 130 crates (40.6 MB) in 31.68s (largest was `windows` at 11.8 MB)
   Compiling proc-macro2 v1.0.79
   Compiling unicode-ident v1.0.12
   Compiling windows_x86_64_msvc v0.52.5
   Compiling serde v1.0.197
   Compiling winapi v0.3.9
   Compiling version_check v0.9.4
   Compiling autocfg v1.2.0
   Compiling cfg-if v1.0.0
   Compiling windows_x86_64_msvc v0.48.5
   Compiling jobserver v0.1.30
   Compiling crossbeam-utils v0.8.19
   Compiling libc v0.2.153
   Compiling cc v1.0.94
   Compiling pkg-config v0.3.30                                                                                                                          
error: linker `link.exe` not found                                                                                                                       
  |
  = note: program not found

note: the msvc targets depend on the msvc linker but `link.exe` was not found                                                                            

note: please ensure that Visual Studio 2017 or later, or Build Tools for Visual Studio were installed with the Visual C++ option.

note: VS Code is a different product, and is not sufficient.

   Compiling num-traits v0.2.18                                                                                                                          
   Compiling once_cell v1.19.0
   Compiling vcpkg v0.2.15                                                                                                                               
error: could not compile `windows_x86_64_msvc` (build script) due to previous error                                                                      
warning: build failed, waiting for other jobs to finish...
error: could not compile `serde` (build script) due to previous error
error: could not compile `proc-macro2` (build script) due to previous error
error: could not compile `crossbeam-utils` (build script) due to previous error
error: could not compile `winapi` (build script) due to previous error
error: could not compile `windows_x86_64_msvc` (build script) due to previous error
error: could not compile `libc` (build script) due to previous error
error: could not compile `num-traits` (build script) due to previous error
error: failed to compile `nu_plugin_gstat v0.92.2`, intermediate artifacts can be found at `C:\Users\(名前)\AppData\Local\Temp\cargo-installpaoRrF`.
To reuse those artifacts with a future compilation, set the environment variable `CARGO_TARGET_DIR` to that path.
```
</div>
</details>

まずrustupの更新をしよう
```
> scoop update rustup                                                                      2024年04月14日 22時21分27秒
Updating Scoop...
  :
  :
rustup: 1.26.0 -> 1.27.0
WARN  Scoop uses 'aria2c' for multi-connection downloads.
WARN  Should it cause issues, run 'scoop config aria2-enabled false' to disable it.
WARN  To disable this warning, run 'scoop config aria2-warning-enabled false'.
Updating one outdated app:
Updating 'rustup' (1.26.0 -> 1.27.0)
Downloading new version
Starting download with aria2 ...
Download: Download Results:
Download: gid   |stat|avg speed  |path/URI
Download: ======+====+===========+=======================================================
Download: b0f7c7|OK  |   0.9MiB/s|D:/Users/(名前)/scoop/cache/rustup#1.27.0#https_static.rust-lang.org_rustup_archive_1.27.0_x86_64-pc-windows-msvc_rust
up-init.exe
Download: Status Legend:
Download: (OK):download completed.
Checking hash of rustup-init.exe ... ok.
Uninstalling 'rustup' (1.26.0)
Unlinking ~\scoop\apps\rustup\current
Installing 'rustup' (1.27.0) [64bit] from main bucket
Loading rustup-init.exe from cache.
Running installer script...
Linking ~\scoop\apps\rustup\current => ~\scoop\apps\rustup\1.27.0
Persisting .cargo
Persisting .rustup
'rustup' (1.27.0) was installed successfully!                                                                           
Notes
-----
This package defaults to using the MSVC toolchain in new installs; use "rustup set default-host" to configure it
(existing installs may be using the GNU toolchain by default)
According to https://doc.rust-lang.org/book/ch01-01-installation.html#installing-rustup-on-windows
Microsoft C++ Build Tools is needed and can be downloaded here: https://visualstudio.microsoft.com/visual-cpp-build-tools/
When installing build tools, these two components should be selected:
- MSVC - VS C++ x64/x86 build tools
- Windows SDK
```
MSVCのを使うような感じに書いてあるが、MinGWを使うやり方がStackoverflowにあったのでそれでやってみよう
参考：[compiler errors - Unable to compile Rust hello world on Windows: linker link.exe not found - Stack Overflow](https://stackoverflow.com/questions/55603111/unable-to-compile-rust-hello-world-on-windows-linker-link-exe-not-found)
```
> rustup toolchain install stable-x86_64-pc-windows-gnu                                    2024年04月14日 22時29分16秒
  ：
  ：
  ：
  stable-x86_64-pc-windows-gnu installed - rustc 1.77.2 (25ef9e3d8 2024-04-09)

info: checking for self-update
> rustup default stable-x86_64-pc-windows-gnu                                              2024年04月14日 22時31分29秒
info: using existing install for 'stable-x86_64-pc-windows-gnu'
info: default toolchain set to 'stable-x86_64-pc-windows-gnu'

  stable-x86_64-pc-windows-gnu unchanged - rustc 1.77.2 (25ef9e3d8 2024-04-09)
> 
```
はいったのかな、、、？
```
> cargo install nu_plugin_gstats
    Updating crates.io index
  Installing nu_plugin_gstat v0.92.2
    Updating crates.io index
  Downloaded winapi-x86_64-pc-windows-gnu v0.4.0
  ：
  ：
  Installing D:\Users\(名前)\scoop\persist\rustup\.cargo\bin\nu_plugin_gstat.exe
   Installed package `nu_plugin_gstat v0.92.2` (executable `nu_plugin_gstat.exe`)
```

はいったっぽい、

```
> nu_plugin_gstat.exe .                                                                      2024年04月14日 23時34分12秒
D:\Users\(名前)\scoop\apps\rustup\current\.cargo\bin\nu_plugin_gstat.exe: This plugin must be run from within Nushell.
If you are running from Nushell, this plugin may be incompatible with the version of nushell you are using.
```
あれ、だめだ
```
> register D:\Users\(名前)\scoop\apps\rustup\current\.cargo\bin\nu_plugin_gstat.exe
> exit
> nu
> plugin list
╭───────┬───────────┬────────────────┬─────────┬──────────────────────────────────────────────────────────────────────────┬───────────┬──────────────────╮
│     # │   name    │   is_running   │   pid   │                                 filename                                 │   shell   │     commands     │
├───────┼───────────┼────────────────┼─────────┼──────────────────────────────────────────────────────────────────────────┼───────────┼──────────────────┤
│     0 │ gstat     │ false          │         │ D:\Users\*******\scoop\persist\rustup\.cargo\bin\nu_plugin_gstat.exe     │           │ ╭───┬───────╮    │
│       │           │                │         │                                                                          │           │ │ 0 │ gstat │    │
│       │           │                │         │                                                                          │           │ ╰───┴───────╯    │
╰───────┴───────────┴────────────────┴─────────┴──────────────────────────────────────────────────────────────────────────┴───────────┴──────────────────╯
```
わからん、、、しかたない、あきらめよう

ところでこれをunregisterする方法もわからない、プラグインアンインストールしたらきえるかな、と思ったが消えない
なんだかんだやってたら`$nu.plugin-path`に設定が入っていることに気づいたのでそれを消したら、`plugin list`で出てこなくなった。

#### starship登録
`.cache/starship/init.nu`つくる方法が一般的みたいだけど、`$nu.default-config-dir`配下につくることにした
まず設定ファイルの作成
```
> starship init nu | save -f $"($nu.default-config-dir)/starship.nu"
```
`config.nu`に追加
```
# starship
# starship init nu | save -f $"($nu.default-config-dir)/starship.nu"
use $"($nu.default-config-dir)/starship.nu"
```
で、starshipの設定でgitの状態をプロンプトに表示できるので、それをこんどやろうと思う

#### git の補完どうするのか
git-completionが使えないような気がするのでなやんでいる
[nu_scripts/custom-completions/git/git-completions.nu at main · nushell/nu_scripts](https://github.com/nushell/nu_scripts/blob/main/custom-completions/git/git-completions.nu)が使えるのかなあ？

```
> cd $nu.default-config-dir
> git clone https://github.com/nushell/nu_scripts.git
```
でnushellのスクリプトを全部コピーして`$nu.config-path`に以下の設定を追加
```nu:$nu.config-path
# git complition
source $"($nu.default-config-dir)/nu_scripts/custom-completions/git/git-completions.nu"
```
うん、なんかちゃんと動いた！
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3740499/ca02e920-8a95-250a-9f94-05a8865e9010.png)

ただし、`git clone`はなぜか補完が用意されてない

completionsのディレクトリにはscoopとかあるので便利そうです
ほかにもいろいろあって便利そう

### powershellのアップデート
`cmd.exe`開いて`scoop update powershell`してもpwsh.exeが起動中と怒られる
cmd.exeからpowershell.exe（ver 5.1）を起動して、`scoop update powershell`すると更新できた