2024/04/10

Windows PowerShell Theme

無意間看到了這個影片How to set up PowerShell prompt with Oh My Posh on Windows 11,沒想到 PowerShell 也可以搞得這麼漂亮,於是參照裡面的設定,幫自己的 PowerShell 做了喜歡的畫面改動。

  1. 安裝 Scoop
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Invoke-RestMethod -Uri https://get.scoop.sh | Invoke-Expression
  1. 安裝 Nerd Fonts
scoop bucket add nerd-fonts
scoop install Hack-NF


安裝 Nerd Fonts 後,設定 PowerShell 的字體為 Hack Nerd Font,字型大小 11,透明度 85%。

  1. 安裝 Terminal Icons
scoop bucket add extras
scoop install terminal-icons
  1. 安裝 Oh My Posh
scoop install https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/oh-my-posh.json
  1. 安裝 fzf
scoop install fzf
Install-Module -Name PSFzf
  1. 安裝 ZLocation
Install-Module -Name ZLocation -Force
  1. 配置個人環境變數
nvim $PROFILE.CurrentUserAllHosts

Config 內容

Import-Module -Name Terminal-Icons
Import-Module -Name ZLocation

oh-my-posh init pwsh --config '~/AppData/Local/Programs/oh-my-posh/themes/remk.omp.json' | Invoke-Expression

Function Get-LongListingll {
    ~/scoop/shims/ls.exe -l @args
}

Set-Alias ll Get-LongListing
Set-Alias ls ~/scoop/shims/ls.exe

Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+t' -PSReadlineChordReverseHistory 'Ctrl+r's ~/scoop/shims/ls.exe

我選擇了 remk 這個 theme,安裝了套件 busybox 讓他更像 Linux,也同時安裝了 versions

Final Theme
於是我就得到了美美的 terminal。

reference

2024/03/25

Delete None Keeping Files Part II

之前寫過這篇 Delete None Keeping Files,在某個目錄依照建日保留最新的 N 個檔案,有個新改良的寫法或許更好懂。

#!/bin/bash

KEEP=10
DIRECTORY=/data/

find "$DIRECTORY" -maxdepth 1 -type f -name "*.log" -printf "%T@ %p\n" | sort -r | tail -n +$(($KEEP+1)) | awk '{print $NF}' | xargs rm -f

來解釋各段參數:

  • find "$DIRECTORY":尋找目標目錄
  • -maxdepth 1:只找該目錄第一層
  • -type f:只找尋檔案,不列入目錄
  • -name '*.log':找尋副檔名為 log 的檔案
  • -printf "%T@ %p\n":使用 printf 輸出,輸出方式為建立的時間戳 + 檔名
  • sort -r:依照時間戳逆排序
  • tail -n +$(($KEEP+1)):列出你要保留的檔案數以外的檔案
  • awk '{print $NF}':只抓出檔案路徑
  • xargs rm -f:刪除該檔案

這樣的寫法應該是萬無一失了。