Skip to content

Instantly share code, notes, and snippets.

View Jaykul's full-sized avatar
😀
Learning

Joel Bennett Jaykul

😀
Learning
View GitHub Profile
@Jaykul
Jaykul / WrapString.ps1
Created October 5, 2024 17:35
String Wrapping. Again.
# Borrowed this one from https://github.com/chalk/ansi-regex
$script:AnsiPattern = "[\u001b\u009b][[\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\d\/#&.:=?%@~_]+)*|[a-zA-Z\d]+(?:;[-a-zA-Z\d\/#&.:=?%@~_]*)*)?(?:\u001b\u005c|\u0007))|(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-nq-uy=><~]))"
$script:AnsiRegex = [Regex]::new($AnsiPattern, "Compiled");
function MeasureString {
[CmdletBinding()]
param(
[string]$InputObject
)
$AnsiRegex.Replace($InputObject, '').Length
@Jaykul
Jaykul / gcixel.ps1
Created September 1, 2024 04:55
ImageMagick and Sixels -- this is not fast, but it works even across ssh
<#
.SYNOPSIS
gcixels is like gci, but with sixels...
.DESCRIPTION
Shows thumbnails of images with titles, directly in terminal.
However, it's done with ImageMagick montage, so it's awfully slow.
Just sharing it for your inspiration.
.NOTES
Requires ImageMagick and a Sixel terminal (like Windows Terminal 1.22+ or WezTerm)
#>
@Jaykul
Jaykul / PowerShellConfig.psm1
Last active September 11, 2024 02:31
Functions for powershell.config.json
#requires -Modules Metadata
function Get-PowerShellConfig {
<#
.SYNOPSIS
Get the PowerShell.config.json as a hashtable
.EXAMPLE
Get-PowerShellConfig
Returns the user's PowerShell Config (usually just what experiments you've enabled)
.EXAMPLE
@Jaykul
Jaykul / Start-Application.ps1
Created August 16, 2024 05:40
So I remember how to find an Appx application by (partial) name
function Start-Application {
param($Name)
if (!($Command = @(Get-Command $Name -CommandType Application -ErrorAction Ignore)[0])) {
$Command = foreach ($Package in Get-AppxPackage *$Name*) {
($Package | Get-AppxPackageManifest).Package.Applications.Application.Executable | % {
Join-Path -Path $Package.InstallLocation -ChildPath $_
} | Get-Command
}
}
& $Command
@Jaykul
Jaykul / Convert-RTTTL.ps1
Last active August 9, 2024 05:24
Convert Nokia RTTL to VT256 DEC PS (PlaySound) escape sequences
function Convert-RTTTL {
<#
.SYNOPSIS
Convert Nokia RTTL to VT256 DEC PS (PlaySound) escape sequences for Windows Terminal, for instance.
.EXAMPLE
Convert-RTTTL 'Mario World Theme:d=4,o=5,b=125:a,8f.,16c,16d,16f,16p,f,16d,16c,16p,16f,16p,16f,16p,8c6,8a.,g,16c,a,8f.,16c,16d,16f,16p,f,16d,16c,16p,16f,16p,16a#,16a,16g,2f,16P,8a.,8f.,8c,8a.,f,16g#,16f,16c,16p,8g#.,2g,8a.,8f.,8c,8a.,f,16g#,16f,8c,2c6' -InformationAction Continue
Plays the Mario World Theme song (and show the name in the console).
.EXAMPLE
Convert-RTTTL 'HauntHouse: d=4,o=6,b=108: 2a5, 2e, 2d#, 2b5, 2a5, 2c, 2d, 2a#5, 2e., e, 1f5, 1a5, 1d#, 2e., d, 2c., b5, 1a5, 1p, 2a5, 2e, 2d#, 2b5, 2a5, 2c, 2d, 2a#5, 2e., e, 1f5, 1a5, 1d#, 2e., d, 2c., b5, 1a5' | Set-Clipboard

This is barely worth sharing, but you can, for instance, get everything pinned to the taskbar, and unpin it from the start menu

Get-Application -PinnedToTaskbar | UnpinStart

Huge Caveat: I haven't figured this all out.

  1. I can't figure out a way to get the order of the taskbar items...
  2. Many items don't have the "pin to taskbar" verb when called this way (I don't know why) and you have to do something ridiculous like making a shortcut to them first (thus, the task bar folder) and then pin that.
@Jaykul
Jaykul / EngineEvent.ps1
Last active April 5, 2024 16:45
How to inject text into the NEXT command prompt
$global:Suggestion = Get-Completion "This is my question"
$global:InjectCommand = Register-EngineEvent -SourceIdentifier PowerShell.OnIdle {
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($global:Suggestion)
Stop-Job $global:InjectCommand # This removes the event
}
@Jaykul
Jaykul / Measure-CommandUsage.ps1
Created November 29, 2023 06:03
Let's have a look at what commands we use
[CmdletBinding()]
param(
# Limit the number of results
[int]$Top
)
$Tokens = @()
$Names = @{}
foreach ($history in [Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems()) {
$null = [System.Management.Automation.Language.Parser]::ParseInput($history.CommandLine, [ref]$Tokens, [ref]$null)

When I'm having a bad day, I can spend hours just fiddling with colors and recursion ...

Today I wrote an HslEnumerator class which is fun because if you output it to the PowerShell terminal it just goes on producing colors forever:

[HslEnumerator][PoshCode.Pansies.RgbColor]"Goldenrod" | Format-Wide 30 # I do have a wide screen

But I also made something more fun. Out-Colors will colorize tables or lists with fun HSL rainbows...

@Jaykul
Jaykul / Invoke-Native.psm1
Last active January 9, 2024 13:40
Calling native executables that write non-error to stderr (with prefixes like "Warning:" and "Error:") ...
class EncodingAttribute : System.Management.Automation.ArgumentTransformationAttribute {
[object] Transform([System.Management.Automation.EngineIntrinsics]$engineIntrinsics, [object] $inputData) {
if ( $inputData -is [string] ) {
return [Text.Encoding]::GetEncoding($inputData)
} else {
return $inputData
}
}
}