Skip to content

Instantly share code, notes, and snippets.

View macu's full-sized avatar

Matt Cudmore macu

  • Halifax, NS
View GitHub Profile
@macu
macu / star–and-moon.txt
Last active September 1, 2022 01:49
Star and Moon ASCII Text Art
★°..    . ☾°☆ .*●¸.   ★ °:. .•○°★ . * .     .
 ° .●.    °☾°☆ ¸.●.  ★  ★°☾☆¸.¸ ★ :. .•○°★ . * .
 .  ¸.  ° ¸.*●¸.    °☾° ¸.●¸.  ★ °:. .•° . * :. . ¸.
●¸   ★  ★☾°★.    . °☆ .●¸.   ★ °. •○°★ .
       * . ☾° ¸.*●¸    °☾°☆ .*¸.   ★ 
★°..    . ☾°☆ .*●¸.   ★ °:. .•○°★ . * .    . ° .
●.    °☾°☆ ¸.●.  ★  ★°☾☆¸.¸ ★ :. .•○°★ . * . .
 .  ¸.  ° ¸.*●¸.  °☾° ¸.●¸.  ★ °:. .•°
@macu
macu / timeout.swift
Last active February 22, 2022 08:03
Timeout utility in Swift, with the ability to cancel a deferred callback.
import Foundation
/// Timeout wrapps a callback deferral that may be cancelled.
///
/// Usage:
/// Timeout(1.0) { println("1 second has passed.") }
///
class Timeout: NSObject
{
private var timer: NSTimer?
$.fn.isBefore = function(e2) {
// var all = $(context || document).find("*");
// return all.index(this[0]) < all.index($(sel));
let e1 = $(this).get(0);
e2 = $(e2).get(0);
let p1 = $(e1).parents(); // returns a jQuery object
let p2 = $(e2).parents(); // ordrered from the immediate parent up
for (var i1 = 0; i1 < p1.length; i1++) {
let i2 = p2.index(p1[i1]);
if (i2 >= 0) {
@macu
macu / colour-utils.js
Last active December 23, 2020 05:57
Functions to convert between RGB and HSL HTML colour strings and invert HSL (can be used to find a high-contrast colour with the same hue): https://codepen.io/macu89/pen/PowjrEQ
// Thanks https://css-tricks.com/converting-color-spaces-in-javascript/
export const rRGB = /^rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)$/;
export const rHSL = /^hsl\((\d{1,3}), (\d{1,3})%, (\d{1,3})%\)$/;
export const rHTML = /^#[0-9a-f]{6}$/i;
export function toRgb(input) {
if (input && typeof input === 'object') {
if (input.hsl) {
return hslToRgb(input);
@macu
macu / autoscroll.js
Last active June 27, 2020 04:52
Utility method to scroll the viewport when the mouse approaches the top or bottom edge, for example while dragging items vertically
// Call when dragging starts, returns handler.
// Call handler.stop() when dragging stops.
export function startAutoscroll() {
const GUTTER_SIZE = 70; // distance from edge of viewport where scrolling starts
const SCALE_RANGE = 8; // higher value gives potential for faster scrolling
const $window = $(window);
let requestId = null;
let clientY = null; // cursor position within viewport
@macu
macu / extract-video-id-from-embed.txt
Last active March 25, 2020 19:24
Extract video IDs from YouTube and Vimeo embed codes
YouTube embed regex:
/<iframe\s+[^>]*?src="(?:(?:https?:)?\/\/)?(?:(?:www|m)\.)?(?:youtube\.com|youtu.be)(?:\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)[^">]*?"[^>]*>[^<]*<\/iframe>/
Vimeo embed regex:
/<iframe\s+[^>]*?src="(?:(?:https?:)?\/\/)?(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/[^\/]+\/videos\/|album\/\d+\/video\/|video\/|)(\d+)\/?(?:[?]?[^">]*)"[^>]*>[^<]*<\/iframe>/
@macu
macu / jQuery-POST-handle-errors.js
Created October 29, 2019 13:53
Detect AJAX response error codes using jQuery
$.post('/ajax/test', {
field,
}).then(response => {
// Success
}).fail((jqXHR) => {
if (jqXHR.status === 409) {
alert('Conflict');
} else {
alert('Error code: ' + jqXHR.status);
}
@macu
macu / blob-save-demo.html
Last active March 30, 2017 08:11
Demo saving a blob from a webpage
<!doctype html>
<html>
<body>
<textarea rows=4 cols=40></textarea>
<br/>
<button onclick="saveTextarea()">Save text</button>
<br/>
<br/>
<button onclick="save256Bytes()">Save example typed array as binary file</button>
@macu
macu / toggle-demo.html
Created December 8, 2016 15:36
Saves toggle state in localStorage and restores on next page load. Note this does not allow some plugins to calculate positions and dimensions of elements in the hidden area.
<a data-toggle=".demo-layout .demo-target" href="#">[-]</a>
<div class="demo-layout">
<div class="demo-target">
<p>Content hidden or displayed</p>
</div>
</div>
@macu
macu / copy-database-to-vm.sh
Created October 13, 2016 01:09
Copy database from actual device to VM for testing
# Ensure all AVDs are closed, and your Android device is connected.
# To make a backup of the ca.mattcudmore.day2day app:
~/Library/Android/sdk/platform-tools/adb backup -f backup.ab ca.mattcudmore.day2day
# Convert the backup file to TAR:
dd if=backup.ab bs=1 skip=24 | python -c "import zlib,sys;sys.stdout.write(zlib.decompress(sys.stdin.read()))" > backup.tar
# Extract the TAR to access files.
tar -xvf backup.tar
# Now disconnect Android device and launch AVD where files will be copied to.