Skip to content

Instantly share code, notes, and snippets.

View kristopherjohnson's full-sized avatar
💭
Huh?

Kristopher Johnson kristopherjohnson

💭
Huh?
View GitHub Profile
@kristopherjohnson
kristopherjohnson / git-push-new-branch.sh
Last active August 4, 2016 19:01
bash/zsh function for pushing a new git branch to remote
# Get the current git branch name
git-branch-name() {
git symbolic-ref --short HEAD
}
# Push new branch to origin and set up tracking
git-push-new-branch() {
git push -u origin $(git-branch-name)
}
public sealed class TrulyObservableCollection<T> : ObservableCollection<T>
where T : INotifyPropertyChanged
{
public TrulyObservableCollection()
{
CollectionChanged += FullObservableCollectionCollectionChanged;
}
public TrulyObservableCollection(IEnumerable<T> pItems) : this()
{
@kristopherjohnson
kristopherjohnson / roman.swift
Last active June 2, 2016 11:40
Swift: Generate Roman numerals
private let onesSymbols = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"]
private let tensSymbols = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"]
private let hundredsSymbols = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"]
/// Generate Roman numeral representation of a value.
///
/// - parameter value: Numeric value to be represented.
/// - returns: `String` (possibly empty)
public func romanNumeralForValue(value: Int) -> String {
if value <= 0 {
@kristopherjohnson
kristopherjohnson / pretty_backtrace.rb
Last active October 17, 2018 14:04
Ruby: Pretty-printed exception backtrace
# Generate a backtrace string for given exception.
# Generated string is a series of lines, each beginning with a tab and "at ".
def pretty_backtrace(exception)
"\tat #{exception.backtrace.join("\n\tat ")}"
end
# Generate a string containing exception message followed by backtrace.
def pretty_exception(exception)
"#{exception.message}\n#{pretty_backtrace(exception)}"
end
@kristopherjohnson
kristopherjohnson / timestamp.rb
Last active September 13, 2022 14:54
Ruby: Generate a timestamp string
# Return 'YYYY-MM-DD HH:MM:SS' string for current time, using local timezone.
def timestamp()
Time.now.strftime '%Y-%m-%d %H:%M:%S'
end
@kristopherjohnson
kristopherjohnson / prettyjson.swift
Last active February 11, 2017 05:29
Reads JSON from standard input and writes pretty-printed JSON to standard output
#!/usr/bin/swift
import Cocoa
let stdin = FileHandle.standardInput
let stdout = FileHandle.standardOutput
let stderr = FileHandle.standardError
do {
let inputData = stdin.readDataToEndOfFile()
@kristopherjohnson
kristopherjohnson / hexdump.swift
Last active May 29, 2020 06:31
Swift: Generate hex dumps for arrays/sequences of bytes.
// hexdump.swift
//
// This file contains library functions for generating hex dumps.
//
// The functions intended for client use are
//
// - `printHexDumpForBytes(_:)`
// - `printHexDumpForStandardInput()`
// - `hexDumpStringForBytes(_:)`
// - `logHexDumpForBytes(_:)`
@kristopherjohnson
kristopherjohnson / git_hg_prompt.zsh-theme
Last active February 26, 2016 12:10
Personal oh-my-zsh theme
# If in a Mercurial repository, output "<branch:bookmark/qtop+*->", where
# "branch" is the current branch
# ":bookmark" is the active bookmark, if any
# "/qtop" is the current patch, if any
# "+" if any files have been added
# "-" if any files have been removed
# "*" if any files have been modified
# "?" if any files exist that have not been added
#
# If not in a Mercurial repository, produce no output.
@kristopherjohnson
kristopherjohnson / dotNetMutex.cs
Created February 17, 2016 15:08
Example of using a Mutex to determine if .NET application is already running
bool mutexIsNew;
using (System.Threading.Mutex m =
new System.Threading.Mutex(true, uniqueName, out mutexIsNew))
{
if (mutexIsNew)
// This is the first instance. Run the application.
else
// There is already an instance running. Exit!
}
@kristopherjohnson
kristopherjohnson / IsNotNilOrEmptyValueTransformer.swift
Created February 8, 2016 22:21
Value transformer that returns true for strings that are non-empty
import Foundation
/// Given a `String?`, return an `NSNumber` Boolean value that is true if the value is non-nil and not empty.
///
/// This is useful for binding the `enabled` property of a button to a string value that is bound to a text field.
final class IsNotNilOrEmptyValueTransformer: NSValueTransformer {
override class func transformedValueClass() -> AnyClass {
return NSNumber.self