Skip to content

Instantly share code, notes, and snippets.

Avatar

Andreas Alin aalin

  • Stockholm, Sweden / Loreto, Peru
View GitHub Profile
@aalin
aalin / pre-commit
Created Aug 9, 2017
Run prettier on staged files
View pre-commit
#!/bin/bash
prettier=$(npm bin)/prettier
git diff --cached --name-status src/ | while read -r status filename; do
# skip deleted files
if [[ "$status" == "D" ]]; then continue; fi
if [[ "$filename" =~ \.jsx?$ ]]; then
"$prettier" --single-quote --no-bracket-spacing --write "$filename" || exit 1
View gradient.js
function lerp(a, b, t) {
return a + t * (b - a);
}
function mix(a, b, t) {
return a.map((v, i) => lerp(v, b[i], t));
}
function clamp(x, min, max) {
if (x < min) { return min; }
@aalin
aalin / lsformat.rb
Created Nov 29, 2016
Format files in a table like ls
View lsformat.rb
require 'io/console'
require 'pp'
files = Dir["*"].sort
MAX_COLUMNS = 9
MAX_COLUMNS.downto(1) do |i|
columns = files.each_slice(i).map { |x| Array.new(i) { |n| x[n] } }
rows = columns.transpose
View now_playing.rb
#!/usr/bin/env ruby
require 'shellwords'
FIELDS = %w(artist album name duration spotify\ url).map { |x| "#{x} of current track" }.join(' & "\\n" & ')
SCRIPT =<<EOF
tell application "Spotify"
#{FIELDS}
end tell
View xm.js
'use strict';
const fs = require("fs");
function leftPad(str, length, chr) {
str = str.toString();
if (str.length >= length) {
return str;
}
View tmuxinator.yml
# Put this in your project root and create a symlink from ~/.tmuxinator/projectname.yml
name: <%= File.basename(path, '.*') %>
root: <%=
begin
File.dirname(File.readlink(path))
rescue
puts "#{path} should be symlinked from your project root."
exit 1
end
@aalin
aalin / prepare-commit-msg
Last active Oct 16, 2015
Git hook for adding issue numbers to commits
View prepare-commit-msg
#!/usr/bin/env ruby
filename = ARGV[0]
branch_name = `git rev-parse --abbrev-ref HEAD`
issue = branch_name[/^[A-Z]+-\d+/]
if issue
contents = File.read(filename)
File.write(filename, "#{ issue }:\n#{ contents }")
end
View debugArgsPatch.js
var debugArgsPatch = (object, property) => {
if (typeof object[property] !== 'function') {
throw `Object does not have the function ${property}`;
}
var origFunc = object[property];
object[property] = (...args) => {
console.log(`Call ${property}:`, args);
var ret = origFunc.apply(object, args);
@aalin
aalin / blamify.rb
Created Mar 2, 2015
Git blame with different colors depending when each line was changed. Darker = older, lighter = newer.
View blamify.rb
require 'shellwords'
class Blamify
PORCELAIN_COMMIT_HASH_RE = /^(?<hash>\h{40})\s/
PORCELAIN_LINE_RE = /^\t(?<line>.*)$/
COLORS = (237..255).to_a # Greyscale gradient
def initialize(filename)
@filename = filename
end
View Dockerfile
FROM node:latest
MAINTAINER Andreas Alin
ADD package.json /app/package.json
RUN cd /app && npm install
RUN npm install node-sass
COPY . /app
WORKDIR /app
You can’t perform that action at this time.