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
This commit is contained in:
Gal on Sygin 2022-01-29 17:48:01 -05:00
parent cdabbc7858
commit 4bf2ff19f4
15 changed files with 188 additions and 42 deletions

View File

@ -34,6 +34,14 @@ $local:MyPowerLineSetup = [ordered]@{
Prompt = @( Prompt = @(
{ Write-Host "" } # A spaced line without a ColorSeparator { Write-Host "" } # A spaced line without a ColorSeparator
{ New-PromptText -EBg VioletRed4 $MyInvocation.HistoryId } { 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 } { Get-SegmentedPath -LengthLimit 37 }
{ Write-VcsStatus } { Write-VcsStatus }

View File

@ -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{}) $local:content = $(try{(Get-Content /run/plex.stream.counter/sessions -Raw -ErrorAction Stop)}catch{})
if( $FetchFromServer ) { if( $FetchFromServer ) {
$local:creds = New-Object System.Management.Automation.PsCredential($env:PLEX_USERNAME,$(ConvertTo-SecureString -AsPlainText $env:PLEX_PASSWORD -Force)) if( ! $PlexHost ) { $PlexHost = $env:PLEX_HOST }
$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 = '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 } $local:plexHeaders = @{ "X-Plex-Token" = ([xml]$webResp.Content).user.authenticationToken }
$webResp = Invoke-WebRequest -Headers $plexHeaders "http://${PlexHost}:32400" $webResp = Invoke-WebRequest -Headers $plexHeaders "http://${PlexHost}:32400"
$local:plexVersion = ([xml]$webResp.Content).MediaContainer.version $local:plexVersion = ([xml]$webResp.Content).MediaContainer.version

View File

@ -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"
}

View File

@ -1 +1 @@
Edit-TextFile prefervi

View File

@ -1 +1 @@
Edit-TextFile prefervi

View File

@ -1,53 +1,56 @@
[CmdletBinding(SupportsShouldProcess)]param( [CmdletBinding(SupportsShouldProcess)]param(
[switch]$sudo, [switch]$sudo,
[Parameter(Position = 0, ValueFromPipelineByPropertyName, ValueFromPipeline, ValueFromRemainingArguments = $true)] [Parameter(Position = 0, ValueFromPipelineByPropertyName, ValueFromPipeline, ValueFromRemainingArguments = $true)]
[string[]]$Path [string[]]$Path,
[string[]]$PreferredEditors
) )
begin { begin {
$global:WaitForEditor = (($WaitForEditor, -1 -ne $null)[0], 5 -ne -1)[0] $global:WaitForEditor = (($WaitForEditor, -1 -ne $null)[0], 5 -ne -1)[0]
$PathForEditor = @() $PathForEditor = @()
} }
process { process {
foreach( $local:P in $Path ) { foreach ( $local:P in $Path ) {
$PathForEditor += $P | Get-Path -Expand $PathForEditor += $P | Get-Path -Expand
} }
} }
end { end {
$local:editors = $env:EDITOR,'nvim','codium','code' $local:editors = $PreferredEditors + @(
$local:editor = $null $env:EDITOR, 'nvim', 'vim', 'vi', 'codium', 'code', 'notepad'
foreach( $local:testEditor in $editors ) { ) | Where-Object { $_ }
if( $(try{Get-Command -ListImported -Type Application $testEditor -ErrorAction SilentlyContinue}catch{}) ) { $local:editor = $null
$editor = "`"$testEditor`"" foreach ( $local:testEditor in $editors ) {
break; if ( $(try { Get-Command -ListImported -Type Application $testEditor -ErrorAction SilentlyContinue }catch {}) ) {
$editor = "`"$testEditor`""
break;
}
} }
}
if( $editor -match 'vim?"$' ) { if ( $editor -match 'vim"$' ) {
$editor += ' -p' $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()
}
} }
}
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 { "'$_'" } Write-Verbose $($Path | Out-String)
if( $Path ) { $arguments = "$arguments" }
if( $PSCmdlet.ShouldProcess( "Edit ($editor): $arguments" ) ) { $local:arguments = $PathForEditor | Foreach-Object { "'$_'" }
Invoke-ExpressionEx -sudo:$sudo "& $editor $arguments" if ( $Path ) { $arguments = "$arguments" }
}
if ( $PSCmdlet.ShouldProcess( "Edit ($editor): $arguments" ) ) {
Invoke-ExpressionEx -sudo:$sudo "& $editor $arguments"
}
} }

6
base/prefervi.ps1 Normal file
View File

@ -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

19
chezmoi/_.package.json Normal file
View File

@ -0,0 +1,19 @@
{
"package": {
"Name": "chezmoi",
"Condition": [
{
"custom": null,
"System": null,
"Hostname": null,
"Username": null,
"CmdletExists": null,
"ModuleExists": null,
"AppExeExists": [
"chezmoi"
],
"Logic": 0
}
]
}
}

View File

@ -0,0 +1 @@
chezmoi completion powershell | Out-String | iex

View File

@ -0,0 +1 @@
Get-FlatpakInstance

View File

@ -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

View File

@ -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 $_
}
}
}

19
flatpak/_.package.json Normal file
View File

@ -0,0 +1,19 @@
{
"package": {
"Name": "flatpak",
"Condition": [
{
"custom": null,
"System": null,
"Hostname": null,
"Username": null,
"CmdletExists": null,
"ModuleExists": null,
"AppExeExists": [
"flatpak"
],
"Logic": 0
}
]
}
}

19
zfs/_.package.json Normal file
View File

@ -0,0 +1,19 @@
{
"package": {
"Condition": [
{
"custom": null,
"System": null,
"Hostname": null,
"Username": null,
"CmdletExists": null,
"ModuleExists": null,
"AppExeExists": [
"zfs"
],
"Logic": 0
}
],
"Name": "zfs"
}
}

11
zfs/zls.ps1 Normal file
View File

@ -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;
$_
}