Skip to content

Instantly share code, notes, and snippets.

View jamescurran's full-sized avatar

James Curran jamescurran

View GitHub Profile
public static int AskGunSkill(IIO io)
{
io.Display("How good a shot are you with your rifle?");
io.Display(" (1) Ace Marksman, (2) Good Shot, (3) Fair to Middlin'");
io.Display(" (4) Need more practice, (5) Shaky Knees");
io.Display("Enter one of the above . The better you claim you are, the");
return io.InputValue("faster you'll have to be with your gun to be successful... ", 1, "Enter 1-5", 5,
"Enter 1-5");
}
@jamescurran
jamescurran / ObjectUpdater3.cs
Created December 14, 2023 20:05
Update an object to one of two values, taken from a string
public static void UpdateFromText<T>(this T target, string changes, bool trueCase = true)
where T : class
{
var propsTarget = target.GetType().GetProperties();
var parts = changes.Split('=');
if (parts.Length != 2)
throw new ArgumentOutOfRangeException(parts[0], $"{parts[0]} is not a property of the target");
var pT = propsTarget.FirstOrDefault(t => t.Name == parts[0]);
if (pT == null)
@jamescurran
jamescurran / ObjectUpdater2.cs
Created December 14, 2023 19:55
Update an Object using a dictionary with the properties to change
public static void UpdateFromText<T>(this T target, IDictionary<string, object> changes)
where T : class
{
foreach (var kvp in changes)
{
var propsTarget = typeof(T).GetProperty(kvp.Key, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
if (propsTarget == null)
//throw new ArgumentOutOfRangeException(nameof(changes), $"{kvp.Key} is not a property of the target");
continue;
@jamescurran
jamescurran / ObjectUpdater1.cs
Created December 14, 2023 19:15
Update an object using an anonymous object with the fields to change
public static void Update<T>(this T target, object changes)
where T : class
{
var propsTarget = typeof(T).GetProperties();
foreach (var pC in changes.GetType().GetProperties())
{
var pT = propsTarget.FirstOrDefault(t => t.Name == pC.Name);
if (pT == null)
throw new ArgumentOutOfRangeException(pC.Name, $"{pC.Name} is not a property of the target");
@jamescurran
jamescurran / ListResizeTest.cs
Created October 20, 2022 15:33
Tests the effect of pre-allocating an list size to a percentage of the final size. https://intodot.net/performance-gains-when-specifying-a-lists-capacity-part-2/
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
var summary = BenchmarkRunner.Run<ListResize>();
public class ListResize
{
static readonly int itemsToAdd = int.MaxValue / 100;
[Params(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)]
function global:prompt {
$Success = $?
Write-Host -Object $pwd -ForegroundColor Magenta
$date = Get-Date -Format "dd-MMM-yyyy HH:mm:ss"
Write-Host -Object $date -ForegroundColor Green
$quote = get-content "C:\Users\zames\OneDrive\Documents\times\$(Get-Date -Format "HH_mm").json" | ConvertFrom-Json | Get-random
Write-Host -Object $quote.Quote_first.Replace("<br>", "`n") -ForegroundColor Blue -NoNewLine
Write-Host -Object $quote.Quote_time_case -ForegroundColor Yellow -NoNewLine
Write-Host -Object $quote.Quote_last.Replace("<br>", "`n") -ForegroundColor Blue -NoNewLine
{
"Time": "00:01",
"Quote_first": "With the appointed execution time of ",
"Quote_time_case": "one minute past midnight",
"Quote_last": " just seconds away, I knocked on the metal door twice. The lock turned and the door swiftly swung open.",
"Title": "Death at Midnight",
"Author": "Donald A. Cabana",
"Sfw": "yes"
}
@jamescurran
jamescurran / prompt_2line.ps
Created August 13, 2022 09:40
Simple PowerShell prompt (current folder & date/time on separate lines)
function global:prompt {
$Success = $?
Write-Host -Object $pwd -ForegroundColor Magenta
$date = Get-Date -Format "dd-MMM-yyyy HH:mm:ss"
Write-Host -Object $date -ForegroundColor Green
return "> "
}
// Copyright (c) 2019-2020 James M. Curran/Novel Theory LLC. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
namespace NovelTheory.Common
{
// Copyright (c) 2019-2020 James M. Curran/Novel Theory LLC. All rights reserved.
// Licensed under the Apache License, Version 2.0.
// See License.txt in the project root for license information.
using System;
using System.Diagnostics;
using Microsoft.Extensions.Logging;
namespace NovelTheory.Common
{