Skip to content

Instantly share code, notes, and snippets.

@sean-m
sean-m / ToKvText.cs
Last active November 2, 2023 22:45
Takes an object and does a naive pass at turing it into a string that would print well in a console.
public string ToKvText(object entry) {
if (entry == null) return string.Empty;
StringBuilder sb = new StringBuilder();
var properties = entry.GetType().GetProperties();
var longestPropertyLength = properties.Select(x => x.Name.Length).Max();
int indentation = longestPropertyLength + 3;
foreach (var p in properties) {
<?xml version="1.0" encoding="utf-16"?>
<StorableColorTheme xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Keys>
<string>ErrorForegroundColor</string>
<string>ErrorBackgroundColor</string>
<string>WarningForegroundColor</string>
<string>WarningBackgroundColor</string>
<string>VerboseForegroundColor</string>
<string>VerboseBackgroundColor</string>
<string>DebugForegroundColor</string>
@sean-m
sean-m / OrderGroupAtFirstOccurance.cs
Last active October 18, 2023 16:59
Extension method that will yield properties from a specified group of properties if any of them are encountered in the first collection. It's using the group metaphore because I use it for grouping properties together by name in a UI. Yes there's perf issues as things get big, it's your foot.
/// <summary>
/// Let's say you have a collection of things with an indeterminate ordering but when you
/// enumerate the things, you'd like a subset to be grouped together. That's what this
/// can do but there's no guarantee elements in the group parameter exist in the primaryCollection.
/// That can be seen as a bug or a feature, it's up to you.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="primaryCollection"></param>
/// <param name="group"></param>
/// <returns></returns>
@sean-m
sean-m / CsvFileReader.cs
Created August 28, 2023 23:28
Helpers for optimistically loading strongly typed data in C# programs. Useful for bulk loading data for unit tests or dev environments. UglyDbInit will use the CsvFileReader to initlaize an entity framework database context.
using System.Reflection;
using System.Text.RegularExpressions;
using Microsoft.VisualBasic.FileIO;
namespace SMM
{
public class CsvFileReader
{
static string csv_delimiter_pattern = @"(?:^|,)(?=[^""]|("")?)""?((?(1)(?:[^""]|"""")*|[^,""]*))""?(?=,|$)";
static Regex csv_delimiter = new Regex(csv_delimiter_pattern, RegexOptions.Compiled);
@sean-m
sean-m / TimeKeeper.ps1
Last active October 16, 2023 21:54
Helper classes for performance instrumentation in PowerShell scripts. This is not the start and end of performance analysis of scripts, I'm not even saying it's good, it is as consistent and low overhead as I have the energy to make it right now and has helped troubleshoot a real problem at work.
#!/usr/bin/pwsh
#Requires -Version 5
#region TimeKeeper
################################################################################
## TimeKeeper ##
################################################################################
class TimeEvent {
<#
@sean-m
sean-m / StorageConverter.cs
Last active June 5, 2023 21:01
Just sick of looking up this size conversion math. This should be in the BCL.
/*
Usage:
var five = 5000.FromMB();
Console.WriteLine($"{five.ToKB()} KB");
Console.WriteLine($"{five.ToMB()} MB");
Console.WriteLine($"{five.ToGB()} GB");
Console.WriteLine($"{five.ToTB()} TB");
Console.WriteLine($"{five.ToPB()} PB");
@sean-m
sean-m / BeepOnChange.ps1
Created May 8, 2023 15:08
Take pipeline input and beep when the input changes. It will optionally stop when the change occurs.
function BeepOnChange {
<#
.Synopsis
Take pipeline input and beep when the input changes.
It will optionally stop when the change occurs.
.PARAMETER Skip
Number of lines to not consider for comparison. If you're piping
a long running command like ping /t, you want it to skip the
header of the command. Combine ping with the StopOnChange switch
for it to alert when a network connection comes back up and ping
@sean-m
sean-m / jelly-kube.yml
Last active November 5, 2022 02:26
Jellyfin Podman play kubernetes yaml file. Allows using Podman's kubernetes-ish pod deployment for Jellyfin with Intel quicksync. This started as a docker-compose but moved to podman with podman-compose to create the pod then 'podman genenerate kube' to create the yml, then 'podman play kube' to run that config.
# Save the output of this file and use kubectl create -f to import
# it into Kubernetes.
#
# Created with podman-4.0.2
#
# Where you see: /bulk/*, update those paths to correlate with your filesystem. Note: /dev/dri is what
# passes through devices for Intel Quick Sync hardware encoding. For me that dropped CPU usage on 4k
# video from 90% to 2% on 8th gen i5.
#
# After running $ podman play kube jelly-kube.yml, run the following command to generate systemd unit files:
@sean-m
sean-m / TimeEstimator.ps1
Last active December 4, 2023 22:15
A time estimator for use with PowerShell's Write-Progress. Provided a total number of cycles, it tracks how long the last 100 or less itterations of your main loop took and calculates the remaining time based on velocity. Note: when ticks exceed total, it returns a seconds remaining of 0, but continues to track the rate that work is getting done.
<#
A time estimator for use with PowerShell's Write-Progress. Provided a total
number of cycles, it tracks how long the last 100 or less itterations of your
main loop took and calculates the remaining time based on velocity. Note: when
ticks exceed total, it returns a seconds remaining of 0, but continues to track
the rate that work is getting done.
The intended use case is with Write-Progress, calls to that cmdlet are really
slow on PowerShell 2-5, efforts have been made to maintain low overhead. If
you're after performance this is still useful, just log the progress yourself.
For instance, using [System.Diagnostics.Trace]::Write() and watching with
@sean-m
sean-m / LoadUsersIntoLdap.ps1
Created August 5, 2022 18:05
For loading users into an ldap directory from a CSV file. Will create new users and update attributes as needed on existing users.
#Requires -Module ActiveDirectory
[CmdletBinding()]
param ($users_file, [switch]$WhatIf)
if (-not $users_file) {
Write-Warning "Must pass file name as `$users_file"
return 1
}
Write-Host -ForegroundColor Cyan $users_file