Oh-my-posh 테마 랜덤으로 사용하기 본문

ETC

Oh-my-posh 테마 랜덤으로 사용하기

miensoap 2024. 7. 5. 15:33

1. C:\Users\your 경로에 themes.txt 만들기

zash
1_shell
M365Princess
bubbles
bubblesline

이 곳 에서 원하는 테마를 찾아 이름을 추가하면 됩니다.

`#` 으로 주석처리하면 선택되지 않습니다.

 

2. 파워쉘 터미널에 code $PROFILE 입력

저는 vscode 사용하므로 code 입력, notepad 입력으로 메모장 사용 가능

3. profile 파일에 다음 함수 추가

function Get-ValidThemes {
    param (
        [string]$FilePath
    )
    # 주석과 공백 라인 제거
    Get-Content -Path $FilePath | Where-Object { $_.Trim() -ne '' -and $_ -notmatch '^\s*#' }
}

# 랜덤 테마로 설정하는 함수
function Set-RandomOhMyPoshTheme {
    $themes = Get-ValidThemes -FilePath "C:\Users\your\themes.txt"
    $selected_theme = $themes | Get-Random
    Set-OhMyPoshTheme $selected_theme
}

# 입력한 테마로 설정하는 함수
function Set-OhMyPoshTheme {
    param (
        [string]$ThemeName
    )

    $base_url = "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/"
    $terminal_theme_url = "${base_url}${ThemeName}.omp.json"

    Set-Item -Path Env:TERMINAL_THEME -Value $terminal_theme_url

    Write-Output "Oh My Posh Theme: $ThemeName"

    oh-my-posh init pwsh --config $env:TERMINAL_THEME | Invoke-Expression
}

 

4. 시작시 설정하는 명령어 실행 추가

# 터미널 시작시 랜덤 테마 설정
Set-Alias -Name random-theme -Value Set-RandomOhMyPoshTheme
Set-RandomOhMyPoshTheme

5. (선택) 이외 명령어 추가

function Show-OhMyPoshThemesList {
    $themes = Get-ValidThemes -FilePath "C:\Users\your\themes.txt"
    $themes
}

Set-Alias -Name list-themes -Value Show-OhMyPoshThemesList
Set-Alias -Name set-theme -Value Set-OhMyPoshTheme

 

6. (선택) 파일 분리

  1. 위 내용을 담은 OhMyPoshFunctions.ps1 파일 생성
  2. Microsoft.PowerShell_profile.ps1 (profile 파일) 에 다음 내용 추가
. "C:\path\to\Scripts\OhMyPoshFunctions.ps1"

외부 스크립트 파일을 로드합니다


실사용

  • 터미널 시작시 랜덤 테마 적용

  • 원하는 테마 적용

 

'ETC' 카테고리의 다른 글

PowerShell 스크립트로 빠르게 gist 생성하기  (0) 2024.07.15
소중단 배포  (4) 2024.06.03