Skip to content

Instantly share code, notes, and snippets.

@maximousblk
Last active November 7, 2022 14:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maximousblk/956d7a89a2cd4f5192ec2cb7d8d3fae5 to your computer and use it in GitHub Desktop.
Save maximousblk/956d7a89a2cd4f5192ec2cb7d8d3fae5 to your computer and use it in GitHub Desktop.
My PowerShell Profile
### Prompt
Invoke-Expression (&starship init powershell)
### Aliases
Set-Alias -Name "c" -Value "code"
Set-Alias -Name "e" -Value "explorer"
Set-Alias -Name "~" -Value "Enter-Home"
Set-Alias -Name "which" -Value "Find-Command"
Set-Alias -Name "scpy" -Value "Start-Scrcpy"
### Modules
if ($host.Name -eq 'ConsoleHost') { Import-Module PSReadLine }
# PSReadLine keybindings
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
# PSReadLine options
Set-PSReadLineOption -BellStyle None
Set-PSReadLineOption -EditMode Windows
Set-PSReadLineOption -HistoryNoDuplicates
Set-PSReadLineOption -PredictionSource History
# Set-PSReadLineOption -PredictionViewStyle ListView
Set-PSReadLineOption -HistorySearchCursorMovesToEnd
### Custom Functions
function Enter-Home { Set-Location ~ }
function Find-Command {
Param(
[Parameter(Position = 0, Mandatory)][string[]]$Names,
[Parameter()][Switch][Alias('r')]$Raw
)
foreach ($Name in $Names) {
if (!$Raw) { Write-Host "" }
$Commands = Get-Command $Name -all -ErrorAction:Ignore
if (!$Commands) {
$Commands = [psobject]@{
Name = $Name;
}
}
foreach ($Command in $Commands) {
if ($Raw) {
if ($Command.Path -or $Command.Source) {
Write-Output ($Command.Path ?? $Command.Source)
}
elseif ($Command.ResolvedCommand) {
Write-Output $Command.ResolvedCommand
}
else {
Write-Output "(???)"
}
}
else {
if ($Command.CommandType) {
Write-Host "[$($Command.CommandType)] " -NoNewline -ForegroundColor Green
}
else {
Write-Host "[Missing] " -NoNewline -ForegroundColor Red
}
Write-Host "$($Command.Name ?? $Name) " -NoNewline
if ($Command.Path -or $Command.Source) {
Write-Host "($($Command.Path ? $Command.Path : $Command.Source))" -ForegroundColor DarkGray
}
elseif ($Command.ResolvedCommand) {
Write-Host "($($Command.ResolvedCommand))" -ForegroundColor DarkGray
}
else {
Write-Host "(???)" -ForegroundColor DarkGray
}
}
}
}
}
function Start-Scrcpy {
Param(
[Parameter(Position = 0)][string][Alias('s')]$Serial,
[Parameter()][Switch][Alias('l')]$ListDevices,
[Parameter()][Switch][Alias('r')]$Record,
[Parameter()][Switch][Alias('n')]$NoControl,
[Parameter()][Switch][Alias('t')]$AlwaysOnTop
)
if ($ListDevices) {
adb devices -l
return
}
[string[]]$Arguments = "--lock-video-orientation"
if ($Serial) {
$Arguments += "--serial=$($Serial)"
}
if ($Record) {
$Date = Get-Date -Format "dd-MM-yyyy_HH-mm-ss-fff"
if ($Date) {
$Arguments += "--record=$($env:USERPROFILE)\Videos\Captures\scrcpy-recording_$($Date).mkv"
}
else {
Write-Host "Failed to get date" -ForegroundColor Red
}
}
if ($NoControl) {
$Arguments += "--no-control"
}
else {
$Arguments += "--turn-screen-off", "--stay-awake", "--show-touches"
}
if ($AlwaysOnTop) {
$Arguments += "--always-on-top"
}
Start-Process -FilePath "scrcpy" -ArgumentList $Arguments -ErrorVariable $ScrcpyError
if ($ScrcpyError) {
Write-Host "Failed to start scrcpy" -ForegroundColor Red
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment