From 4bf2ff19f4567de9e9fa2ee70c21419b072c47a7 Mon Sep 17 00:00:00 2001 From: Gal on Sygin Date: Sat, 29 Jan 2022 17:48:01 -0500 Subject: [PATCH] A lot of changes + PowerLine prompt now shows degraded systemctl state (When Apllicable) + Get-PlexInfo notifies if PLEX creds are needed and are missing + XDG env are setup if missing on linux + vi/vim command will launch editor, but prefer vi/vim/nvim if exists + new chezmoi package: auto loading completion + new flatpak package: List and Kill commands added + new zfs package: zls command added --- PowerLine/Setup-MyPowerLineTheme.ps1 | 8 +++ base.linux/Get-PlexInfo.ps1 | 15 ++++-- base.linux/profile.d/env.ps1 | 9 ++++ base/Aliases/vi.ps1 | 2 +- base/Aliases/vim.ps1 | 2 +- base/Edit-TextFile.ps1 | 77 +++++++++++++++------------- base/prefervi.ps1 | 6 +++ chezmoi/_.package.json | 19 +++++++ chezmoi/profile.d/completion.ps1 | 1 + flatpak/Aliases/lsfpk.ps1 | 1 + flatpak/Get-FlatpakInstance.ps1 | 27 ++++++++++ flatpak/Kill-FlatpakInstance.ps1 | 14 +++++ flatpak/_.package.json | 19 +++++++ zfs/_.package.json | 19 +++++++ zfs/zls.ps1 | 11 ++++ 15 files changed, 188 insertions(+), 42 deletions(-) create mode 100644 base.linux/profile.d/env.ps1 create mode 100644 base/prefervi.ps1 create mode 100644 chezmoi/_.package.json create mode 100644 chezmoi/profile.d/completion.ps1 create mode 100644 flatpak/Aliases/lsfpk.ps1 create mode 100644 flatpak/Get-FlatpakInstance.ps1 create mode 100644 flatpak/Kill-FlatpakInstance.ps1 create mode 100644 flatpak/_.package.json create mode 100644 zfs/_.package.json create mode 100644 zfs/zls.ps1 diff --git a/PowerLine/Setup-MyPowerLineTheme.ps1 b/PowerLine/Setup-MyPowerLineTheme.ps1 index 4282486..f685647 100644 --- a/PowerLine/Setup-MyPowerLineTheme.ps1 +++ b/PowerLine/Setup-MyPowerLineTheme.ps1 @@ -34,6 +34,14 @@ $local:MyPowerLineSetup = [ordered]@{ Prompt = @( { Write-Host "" } # A spaced line without a ColorSeparator { New-PromptText -EBg VioletRed4 $MyInvocation.HistoryId } + { + if( Get-Command systemctl -ErrorAction Ignore ) { + $local:systemState = systemctl is-system-running + if( $systemState -ine 'RUNNING') { + New-PromptText -BackgroundColor VioletRed4 $systemState + } + } + } { Get-SegmentedPath -LengthLimit 37 } { Write-VcsStatus } diff --git a/base.linux/Get-PlexInfo.ps1 b/base.linux/Get-PlexInfo.ps1 index f679500..0868b66 100644 --- a/base.linux/Get-PlexInfo.ps1 +++ b/base.linux/Get-PlexInfo.ps1 @@ -1,10 +1,19 @@ -param([switch]$FetchFromServer,[string]$PlexHost='localhost',[switch]$PassThruOnly) +param([switch]$FetchFromServer,[string]$PlexHost,[PSCredential]$Credential,[switch]$PassThruOnly) $local:content = $(try{(Get-Content /run/plex.stream.counter/sessions -Raw -ErrorAction Stop)}catch{}) if( $FetchFromServer ) { - $local:creds = New-Object System.Management.Automation.PsCredential($env:PLEX_USERNAME,$(ConvertTo-SecureString -AsPlainText $env:PLEX_PASSWORD -Force)) - $local:webResp = Invoke-WebRequest -Headers @{ "Content-Length" = "0"; "X-Plex-Client-Identifier" = "my-app" } -Credential $creds -Method Post -Uri "https://my.plexapp.com/users/sign_in.xml" + if( ! $PlexHost ) { $PlexHost = $env:PLEX_HOST } + if( ! $PlexHost ) { $PlexHost = 'localhost' } + + if( -not Credential ) { $Credential = $PlexPresetCredential } + if( -not Credential ) { + if( -not $env:PLEX_USERNAME -or -not $env:PLEX_PASSWORD ) { + throw "`$env:PLEX_USERNAME AND `$env:PLEX_PASSWORD must be defined in order to read directly from server" + } + $Credential = New-Object System.Management.Automation.PsCredential($env:PLEX_USERNAME,$(ConvertTo-SecureString -AsPlainText $env:PLEX_PASSWORD -Force)) + } + $local:webResp = Invoke-WebRequest -Headers @{ "Content-Length" = "0"; "X-Plex-Client-Identifier" = "my-app" } -Credential $Credential -Method Post -Uri "https://my.plexapp.com/users/sign_in.xml" $local:plexHeaders = @{ "X-Plex-Token" = ([xml]$webResp.Content).user.authenticationToken } $webResp = Invoke-WebRequest -Headers $plexHeaders "http://${PlexHost}:32400" $local:plexVersion = ([xml]$webResp.Content).MediaContainer.version diff --git a/base.linux/profile.d/env.ps1 b/base.linux/profile.d/env.ps1 new file mode 100644 index 0000000..4e3e64d --- /dev/null +++ b/base.linux/profile.d/env.ps1 @@ -0,0 +1,9 @@ +# Setting some defaults based on +# FreeDesktop.org's XDG Base Directory Specification document: +# https://specifications.freedesktop.org/basedir-spec/latest/ar01s03.html +if( -not (Test-Path env:XDG_DATA_HOME) ) { + $env:XDG_DATA_HOME="$HOME/.local/share" +} +if( -not (Test-Path env:XDG_CONFIG_HOME) ) { + $env:XDG_CONFIG_HOME="$HOME/.config" +} \ No newline at end of file diff --git a/base/Aliases/vi.ps1 b/base/Aliases/vi.ps1 index e099b51..e7bd2f7 100644 --- a/base/Aliases/vi.ps1 +++ b/base/Aliases/vi.ps1 @@ -1 +1 @@ -Edit-TextFile +prefervi \ No newline at end of file diff --git a/base/Aliases/vim.ps1 b/base/Aliases/vim.ps1 index e099b51..82edfa8 100644 --- a/base/Aliases/vim.ps1 +++ b/base/Aliases/vim.ps1 @@ -1 +1 @@ -Edit-TextFile +prefervi diff --git a/base/Edit-TextFile.ps1 b/base/Edit-TextFile.ps1 index 6384dff..0c44cde 100644 --- a/base/Edit-TextFile.ps1 +++ b/base/Edit-TextFile.ps1 @@ -1,53 +1,56 @@ [CmdletBinding(SupportsShouldProcess)]param( - [switch]$sudo, - [Parameter(Position = 0, ValueFromPipelineByPropertyName, ValueFromPipeline, ValueFromRemainingArguments = $true)] - [string[]]$Path + [switch]$sudo, + [Parameter(Position = 0, ValueFromPipelineByPropertyName, ValueFromPipeline, ValueFromRemainingArguments = $true)] + [string[]]$Path, + [string[]]$PreferredEditors ) begin { - $global:WaitForEditor = (($WaitForEditor, -1 -ne $null)[0], 5 -ne -1)[0] - $PathForEditor = @() + $global:WaitForEditor = (($WaitForEditor, -1 -ne $null)[0], 5 -ne -1)[0] + $PathForEditor = @() } process { - foreach( $local:P in $Path ) { - $PathForEditor += $P | Get-Path -Expand - } + foreach ( $local:P in $Path ) { + $PathForEditor += $P | Get-Path -Expand + } } end { - $local:editors = $env:EDITOR,'nvim','codium','code' - $local:editor = $null - foreach( $local:testEditor in $editors ) { - if( $(try{Get-Command -ListImported -Type Application $testEditor -ErrorAction SilentlyContinue}catch{}) ) { - $editor = "`"$testEditor`"" - break; + $local:editors = $PreferredEditors + @( + $env:EDITOR, 'nvim', 'vim', 'vi', 'codium', 'code', 'notepad' + ) | Where-Object { $_ } + $local:editor = $null + foreach ( $local:testEditor in $editors ) { + if ( $(try { Get-Command -ListImported -Type Application $testEditor -ErrorAction SilentlyContinue }catch {}) ) { + $editor = "`"$testEditor`"" + break; + } } - } - if( $editor -match 'vim?"$' ) { - $editor += ' -p' - } - - if( $EditorProcessName ) { - if( -not (Get-Process -Name $EditorProcessName -ErrorAction SilentlyContinue) ) { - Invoke-ExpressionEx -sudo:$sudo "& $editor" - while( -not (Get-Process -Name $EditorProcessName -ErrorAction SilentlyContinue) ) { - Start-Sleep -Seconds 1 - } - Write-Host -ForegroundColor DarkCyan "Please wait, initializing editor..." - Start-Sleep -Seconds $WaitForEditor - if( Test-Path variable:EditorWaitStopWatch ) { - $EditorWaitStopWatch.Restart() - } + if ( $editor -match 'vim"$' ) { + $editor += ' -p' } - } - Write-Verbose $($Path | Out-String) + if ( $EditorProcessName ) { + if ( -not (Get-Process -Name $EditorProcessName -ErrorAction SilentlyContinue) ) { + Invoke-ExpressionEx -sudo:$sudo "& $editor" + while ( -not (Get-Process -Name $EditorProcessName -ErrorAction SilentlyContinue) ) { + Start-Sleep -Seconds 1 + } + Write-Host -ForegroundColor DarkCyan "Please wait, initializing editor..." + Start-Sleep -Seconds $WaitForEditor + if ( Test-Path variable:EditorWaitStopWatch ) { + $EditorWaitStopWatch.Restart() + } + } + } - $local:arguments = $PathForEditor | Foreach-Object { "'$_'" } - if( $Path ) { $arguments = "$arguments" } + Write-Verbose $($Path | Out-String) - if( $PSCmdlet.ShouldProcess( "Edit ($editor): $arguments" ) ) { - Invoke-ExpressionEx -sudo:$sudo "& $editor $arguments" - } + $local:arguments = $PathForEditor | Foreach-Object { "'$_'" } + if ( $Path ) { $arguments = "$arguments" } + + if ( $PSCmdlet.ShouldProcess( "Edit ($editor): $arguments" ) ) { + Invoke-ExpressionEx -sudo:$sudo "& $editor $arguments" + } } diff --git a/base/prefervi.ps1 b/base/prefervi.ps1 new file mode 100644 index 0000000..4f18e6b --- /dev/null +++ b/base/prefervi.ps1 @@ -0,0 +1,6 @@ +[CmdletBinding(SupportsShouldProcess)]param( + [switch]$sudo, + [Parameter(Position = 0, ValueFromPipelineByPropertyName, ValueFromPipeline, ValueFromRemainingArguments = $true)] + [string[]]$Path +) +Edit-TextFile -sudo:$sudo -PreferredEditors "nvim","vim","vi" -Path $Path \ No newline at end of file diff --git a/chezmoi/_.package.json b/chezmoi/_.package.json new file mode 100644 index 0000000..40ca0ae --- /dev/null +++ b/chezmoi/_.package.json @@ -0,0 +1,19 @@ +{ + "package": { + "Name": "chezmoi", + "Condition": [ + { + "custom": null, + "System": null, + "Hostname": null, + "Username": null, + "CmdletExists": null, + "ModuleExists": null, + "AppExeExists": [ + "chezmoi" + ], + "Logic": 0 + } + ] + } +} diff --git a/chezmoi/profile.d/completion.ps1 b/chezmoi/profile.d/completion.ps1 new file mode 100644 index 0000000..c9b692a --- /dev/null +++ b/chezmoi/profile.d/completion.ps1 @@ -0,0 +1 @@ +chezmoi completion powershell | Out-String | iex \ No newline at end of file diff --git a/flatpak/Aliases/lsfpk.ps1 b/flatpak/Aliases/lsfpk.ps1 new file mode 100644 index 0000000..8423338 --- /dev/null +++ b/flatpak/Aliases/lsfpk.ps1 @@ -0,0 +1 @@ +Get-FlatpakInstance \ No newline at end of file diff --git a/flatpak/Get-FlatpakInstance.ps1 b/flatpak/Get-FlatpakInstance.ps1 new file mode 100644 index 0000000..0592b1a --- /dev/null +++ b/flatpak/Get-FlatpakInstance.ps1 @@ -0,0 +1,27 @@ +[CmdletBinding()]param( + [string[]]$Instance, [string[]]$Application, [string[]]$Runtime +) + +$local:cols = [ordered]@{ + 'instance'= 'Instance' + 'pid'= 'PID' + 'application'= 'Application' + 'runtime' = 'Runtime' +} + +$local:paks = @( + $($cols.Values -join "`t")) + $( + flatpak ps "--columns=$($cols.Keys -join ',')" + ) -join "`n" | + ConvertFrom-Csv -Delimiter "`t" | + Where-Object { + $local:p = $_ + (-not $Instance -or $( $_ -in $Instance ) + ) -and (-not $Application -or $( + $Application | Where-Object { $($p.Application) -match $_ } + )) -and (-not $Runtime -or $( + $Runtime | Where-Object { $($p.Runtime) -match $_ } + )) + } + +$paks diff --git a/flatpak/Kill-FlatpakInstance.ps1 b/flatpak/Kill-FlatpakInstance.ps1 new file mode 100644 index 0000000..143fb4a --- /dev/null +++ b/flatpak/Kill-FlatpakInstance.ps1 @@ -0,0 +1,14 @@ +[CmdletBinding(SupportsShouldProcess,ConfirmImpact='low')]param( + [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName,ValueFromRemainingArguments)] + [string[]]$Instance +) + +BEGIN {} +PROCESS { + $Instance | ForEach-Object { + if ($PSCmdlet.ShouldProcess("$_", "Kill-FlatpackInstance")) { + Write-Verbose "Performing: flatpak kill $_" + flatpak kill $_ + } + } +} diff --git a/flatpak/_.package.json b/flatpak/_.package.json new file mode 100644 index 0000000..171a65e --- /dev/null +++ b/flatpak/_.package.json @@ -0,0 +1,19 @@ +{ + "package": { + "Name": "flatpak", + "Condition": [ + { + "custom": null, + "System": null, + "Hostname": null, + "Username": null, + "CmdletExists": null, + "ModuleExists": null, + "AppExeExists": [ + "flatpak" + ], + "Logic": 0 + } + ] + } +} diff --git a/zfs/_.package.json b/zfs/_.package.json new file mode 100644 index 0000000..d2a5960 --- /dev/null +++ b/zfs/_.package.json @@ -0,0 +1,19 @@ +{ + "package": { + "Condition": [ + { + "custom": null, + "System": null, + "Hostname": null, + "Username": null, + "CmdletExists": null, + "ModuleExists": null, + "AppExeExists": [ + "zfs" + ], + "Logic": 0 + } + ], + "Name": "zfs" + } +} diff --git a/zfs/zls.ps1 b/zfs/zls.ps1 new file mode 100644 index 0000000..cc156e1 --- /dev/null +++ b/zfs/zls.ps1 @@ -0,0 +1,11 @@ +$local:prev = ""; +zfs list -o name,used,avail,refer,canmount,mounted,mountpoint $args | + grep -v 'docker/' | + ForEach-Object { + $local:tmpPrev = $_ -replace '^(\w+(\W\w+(\W\w+)?)?).*','$1'; + if( $tmpPrev -notmatch "^$prev" -and $prev -notin ('','NAME') ) { + '-----------------' + }; + $prev = $tmpPrev; + $_ + } \ No newline at end of file