rezz-dotfiles/pwsh/Microsoft.PowerShell_profile.ps1

253 lines
7.7 KiB
PowerShell

### PowerShell Profile Refactor
### Version 1.03 - Refactored
# Initial GitHub.com connectivity check with 1 second timeout
$canConnectToGitHub = Test-Connection github.com -Count 1 -Quiet -TimeoutSeconds 1
# Import Modules and External Profiles
# Ensure Terminal-Icons module is installed before importing
if (-not (Get-Module -ListAvailable -Name Terminal-Icons)) {
Install-Module -Name Terminal-Icons -Scope CurrentUser -Force -SkipPublisherCheck
}
Import-Module -Name Terminal-Icons
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile"
}
# Check for Profile Updates
function Update-Profile {
if (-not $global:canConnectToGitHub) {
Write-Host "Skipping profile update check due to GitHub.com not responding within 1 second." -ForegroundColor Yellow
return
}
try {
$url = "https://raw.githubusercontent.com/rezzect/rezz-dotfiles/master/pwsh/Microsoft.PowerShell_profile.ps1"
$oldhash = Get-FileHash $PROFILE
Invoke-RestMethod $url -OutFile "$env:temp/Microsoft.PowerShell_profile.ps1"
$newhash = Get-FileHash "$env:temp/Microsoft.PowerShell_profile.ps1"
if ($newhash.Hash -ne $oldhash.Hash) {
Copy-Item -Path "$env:temp/Microsoft.PowerShell_profile.ps1" -Destination $PROFILE -Force
Write-Host "Profile has been updated. Please restart your shell to reflect changes" -ForegroundColor Magenta
}
} catch {
Write-Error "Unable to check for `$profile updates"
} finally {
Remove-Item "$env:temp/Microsoft.PowerShell_profile.ps1" -ErrorAction SilentlyContinue
}
}
Update-Profile
function Update-PowerShell {
if (-not $global:canConnectToGitHub) {
Write-Host "Skipping PowerShell update check due to GitHub.com not responding within 1 second." -ForegroundColor Yellow
return
}
try {
Write-Host "Checking for PowerShell updates..." -ForegroundColor Cyan
$updateNeeded = $false
$currentVersion = $PSVersionTable.PSVersion.ToString()
$gitHubApiUrl = "https://api.github.com/repos/PowerShell/PowerShell/releases/latest"
$latestReleaseInfo = Invoke-RestMethod -Uri $gitHubApiUrl
$latestVersion = $latestReleaseInfo.tag_name.Trim('v')
if ($currentVersion -lt $latestVersion) {
$updateNeeded = $true
}
if ($updateNeeded) {
Write-Host "Updating PowerShell..." -ForegroundColor Yellow
winget upgrade "Microsoft.PowerShell" --accept-source-agreements --accept-package-agreements
Write-Host "PowerShell has been updated. Please restart your shell to reflect changes" -ForegroundColor Magenta
} else {
Write-Host "Your PowerShell is up to date." -ForegroundColor Green
}
} catch {
Write-Error "Failed to update PowerShell. Error: $_"
}
}
Update-PowerShell
# Admin Check and Prompt Customization
$isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
function prompt {
if ($isAdmin) { "[" + (Get-Location) + "] # " } else { "[" + (Get-Location) + "] $ " }
}
$adminSuffix = if ($isAdmin) { " [ADMIN]" } else { "" }
$Host.UI.RawUI.WindowTitle = "PowerShell {0}$adminSuffix" -f $PSVersionTable.PSVersion.ToString()
# Utility Functions
function Test-CommandExists {
param($command)
$exists = $null -ne (Get-Command $command -ErrorAction SilentlyContinue)
return $exists
}
# Editor Configuration
$EDITOR = if (Test-CommandExists nano) { 'nano' }
elseif (Test-CommandExists notepad++) { 'notepad++' }
else { 'notepad' }
function Edit-Profile {
nano $PROFILE.CurrentUserAllHosts
}
function touch($file) { "" | Out-File $file -Encoding ASCII }
function ff($name) {
Get-ChildItem -recurse -filter "*${name}*" -ErrorAction SilentlyContinue | ForEach-Object {
Write-Output "$($_.directory)\$($_)"
}
}
# Network Utilities
function Get-PubIP { (Invoke-WebRequest http://ifconfig.me/ip).Content }
# System Utilities
function uptime {
if ($PSVersionTable.PSVersion.Major -eq 5) {
Get-WmiObject win32_operatingsystem | Select-Object @{Name='LastBootUpTime'; Expression={$_.ConverttoDateTime($_.lastbootuptime)}} | Format-Table -HideTableHeaders
} else {
net statistics workstation | Select-String "since" | ForEach-Object { $_.ToString().Replace('Statistics since ', '') }
}
}
function reload-profile {
& $profile
}
function unzip ($file) {
Write-Output("Extracting", $file, "to", $pwd)
$fullFile = Get-ChildItem -Path $pwd -Filter $file | ForEach-Object { $_.FullName }
Expand-Archive -Path $fullFile -DestinationPath $pwd
}
function grep($regex, $dir) {
if ( $dir ) {
Get-ChildItem $dir | select-string $regex
return
}
$input | select-string $regex
}
function df {
get-volume
}
function sed($file, $find, $replace) {
(Get-Content $file).replace("$find", $replace) | Set-Content $file
}
function which($name) {
Get-Command $name | Select-Object -ExpandProperty Definition
}
function export($name, $value) {
set-item -force -path "env:$name" -value $value;
}
function pkill($name) {
Get-Process $name -ErrorAction SilentlyContinue | Stop-Process
}
function pgrep($name) {
Get-Process $name
}
function head {
param($Path, $n = 10)
Get-Content $Path -Head $n
}
function tail {
param($Path, $n = 10)
Get-Content $Path -Tail $n
}
# Quick File Creation
function nf { param($name) New-Item -ItemType "file" -Path . -Name $name }
# Directory Management
function mkcd { param($dir) mkdir $dir -Force; Set-Location $dir }
### Quality of Life Aliases
# Navigation Shortcuts
function docs { Set-Location -Path $HOME\Documents }
function dtop { Set-Location -Path $HOME\Desktop }
# Quick Access to Editing the Profile
function ep { nano $PROFILE }
# Simplified Process Management
function k9 { Stop-Process -Name $args[0] }
# Enhanced Listing
function la { Get-ChildItem -Path . -Force | Format-Table -AutoSize }
function ll { Get-ChildItem -Path . -Force -Hidden | Format-Table -AutoSize }
# Git Shortcuts
function gs { git status }
function ga { git add . }
function gc { param($m) git commit -m "$m" }
function gp { git push }
function g { z Github }
function gcom {
git add .
git commit -m "$args"
}
function lazyg {
git add .
git commit -m "$args"
git push
}
# Quick Access to System Information
function sysinfo { Get-ComputerInfo }
# Networking Utilities
function flushdns { Clear-DnsClientCache }
# Clipboard Utilities
function cpy { Set-Clipboard $args[0] }
function pst { Get-Clipboard }
# Alias
Set-Alias -Name btop4win -Value btop
# Vencord Installer
function vencord-install {
Invoke-WebRequest -Uri 'https://github.com/Vencord/Installer/releases/latest/download/VencordInstallerCli.exe' -OutFile VencordInstallerCli.exe
./VencordInstallerClie.exe
Remove-Item VencordInstallerClie.exe
}
# Enhanced PowerShell Experience
Set-PSReadLineOption -Colors @{
Command = 'Yellow'
Parameter = 'Green'
String = 'DarkCyan'
}
## Final Line to set prompt
oh-my-posh init pwsh --config https://raw.githubusercontent.com/rezzect/rezz-dotfiles/master/omp-themes/rezztheme-edit.omp.json | Invoke-Expression
if (Get-Command zoxide -ErrorAction SilentlyContinue) {
Invoke-Expression (& { (zoxide init powershell | Out-String) })
} else {
Write-Host "zoxide command not found. Attempting to install via winget..."
try {
winget install -e --id ajeetdsouza.zoxide
Write-Host "zoxide installed successfully. Initializing..."
Invoke-Expression (& { (zoxide init powershell | Out-String) })
} catch {
Write-Error "Failed to install zoxide. Error: $_"
}
}