Skip to content

Instantly share code, notes, and snippets.

@baweaver
baweaver / ruby_tricks.rb
Created February 14, 2018 07:50
Having fun with M and Q
# A few fun tricks right quick. Note that more tricks like this are over at:
# https://medium.com/rubyinside/triple-equals-black-magic-d934936a6379
#
# I'll probably condense this into a blog post later, but for now we'll have our fun.
# 1 - "Pattern Matching" with case
# We make a new lambda named M for brevity. Because it can be called with `[]` it
# looks quite natural in flow with a case statement.
#
import Foundation
enum Either<A,B> {
case left(A)
case right(B)
}
// Works only using Swift 4.1
extension Either: Codable where A: Codable, B: Codable {
enum CodingKeys: CodingKey {
@scriptingosx
scriptingosx / os_version.sh
Last active April 15, 2023 10:04
Sample bash script to show how to parse the macOS version
#!/bin/bash
# use argument 1 as the version or get it from sw_vers
os_ver=${1-:$(sw_vers -productVersion)}
# string comparison
if [[ "$os_ver" == 10.13.* ]]; then
echo "macOS High Sierra"
elif [[ "$os_ver" == 10.12.* ]]; then
echo "macOS Sierra"
@masnick
masnick / trim.scpt
Created November 8, 2017 18:06
Trim whitespace with AppleScript
on trimThis(pstrSourceText, pstrCharToTrim, pstrTrimDirection)
-- http://macscripter.net/viewtopic.php?id=18519
-- pstrSourceText : The text to be trimmed
-- pstrCharToTrim : A list of characters to trim, or true to use default
-- pstrTrimDirection : Direction of Trim left, right or any value for full
set strTrimedText to pstrSourceText
-- If undefinied use default whitespaces
if pstrCharToTrim is missing value or class of pstrCharToTrim is not list then
@rocarvaj
rocarvaj / prename.pl
Created October 31, 2017 16:26
Perl version of the rename script
#!/usr/bin/perl
use strict;
use warnings;
=head1 NAME
rename - renames multiple files
=head1 SYNOPSIS
// Originally inspired by David Walsh (https://davidwalsh.name/javascript-debounce-function)
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// `wait` milliseconds.
const debounce = (func, wait) => {
let timeout;
// This is the function that is returned and will be executed many times
// We spread (...args) to capture any number of parameters we want to pass
@henrik
henrik / wip_private_ruby.vim
Created September 2, 2017 17:30
WIP: Vim highlight private section of Ruby
function! Snake()
ruby << EOF
b = Vim::Buffer.current
line_count = b.length
indexed_lines = line_count.times.map { |i| [ i, b[i + 1] ] }
matches = indexed_lines.select { |i, line| line.match(/^\s+private$/) }
matches.each { |priv_i, priv_text|
re = /^#{priv_text.sub(" private", "end")}$/
// Swift's untyped errors are a goddam PiTA. Here's the pattern I use to try to work around this.
// The goal is basically to try to guarantee that every throwing function in the app throws an
// ApplicationError instead of some unknown error type. We can't actually enforce this statically
// But by following this convention we can simplify error handling
enum ApplicationError: Error, CustomStringConvertible {
// These are application-specific errors that may need special treatment
case specificError1
case specificError2(SomeType)

Exporting your 2FA tokens from Authy to transfer them into another 2FA application

IMPORTANT - Update regarding deprecation of Authy desktop apps

Past August 2024, Authy stopped supported the desktop version of their apps:
See Authy is shutting down its desktop app | The 2FA app Authy will only be available on Android and iOS starting in August for details.

And indeed, after a while, Authy changed something in their backend which now prevents the old desktop app from logging in. If you are already logged in, then you are in luck, and you can follow the instructions below to export your tokens.

If you are not logged in anymore, but can find a backup of the necessary files, then restore those files, and re-install Authy 2.2.3 following the instructions below, and it should work as expected.

require 'spec_helper'
# /$$ /$$
# | $$ |__/
# /$$$$$$$ /$$$$$$ /$$$$$$ /$$ /$$$$$$$ /$$$$$$
# /$$_____/|_ $$_/ /$$__ $$| $$| $$__ $$ /$$__ $$
# | $$$$$$ | $$ | $$ \__/| $$| $$ \ $$| $$ \ $$
# \____ $$ | $$ /$$| $$ | $$| $$ | $$| $$ | $$
# /$$$$$$$/ | $$$$/| $$ | $$| $$ | $$| $$$$$$$