Skip to content

Instantly share code, notes, and snippets.

View antfu's full-sized avatar

Anthony Fu antfu

View GitHub Profile
@antfu
antfu / set_win_encoding.py
Last active October 21, 2016 20:25
[Python] set_win_encoding
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''Fix Windows encoding problem'''
import os
import sys
import codecs
def set_win_encoding(codepage=None):
@antfu
antfu / kill_on_port.sh
Created November 3, 2016 05:24
Kill process on port
kill $(lsof -t -i:8080)
# Portable "getch"
try:
from msvcrt import getch
except ImportError:
def getch():
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
@antfu
antfu / disable-antialising.css
Last active November 15, 2016 19:40
[CSS] Disable antialising when scaling images
img {
image-rendering: optimizeSpeed; /* STOP SMOOTHING, GIVE ME SPEED */
image-rendering: -moz-crisp-edges; /* Firefox */
image-rendering: -o-crisp-edges; /* Opera */
image-rendering: -webkit-optimize-contrast; /* Chrome (and eventually Safari) */
image-rendering: pixelated; /* Chrome */
image-rendering: optimize-contrast; /* CSS3 Proposed */
-ms-interpolation-mode: nearest-neighbor; /* IE8+ */
}
/* From http://stackoverflow.com/questions/14068103/disable-antialising-when-scaling-images */
@antfu
antfu / get_json.js
Last active November 17, 2016 18:43
[JS] Vanilla get/post json
function get_json(url, callback) {
callback = callback || function(){}
var request = new XMLHttpRequest()
request.open('GET', url, true)
request.onload = function () {
if (request.status >= 200 && request.status < 400) {
// Success!
var data = JSON.parse(request.responseText)
callback(data)
} else {
@antfu
antfu / parse_query.js
Created November 17, 2016 18:44
[JS] Parse Query string
function parse_query(name, url) {
if (!url)
url = window.location.href
name = name.replace(/[\[\]]/g, "\\$&")
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)")
var results = regex.exec(url)
if (!results) return null
if (!results[2]) return ''
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
@antfu
antfu / get_style.js
Created November 20, 2016 16:02
[JS] Getting All CSS Properties of a DOM
function get_style( dom ) {
var style;
var returns = {};
// FireFox and Chrome way
if(window.getComputedStyle){
style = window.getComputedStyle(dom, null);
for(var i = 0, l = style.length; i < l; i++){
var prop = style[i];
var val = style.getPropertyValue(prop);
returns[prop] = val;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Utils
{
public delegate int keyboardHookProc(int code, int wParam, ref keyboardHookStruct lParam);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace Utils
{
public sealed class KeyboardHook : IDisposable
@antfu
antfu / MathConverter.cs
Created March 23, 2017 17:30
[WPF] BindingMathConverter
// Does a math equation on the bound value.
// Use @VALUE in your mathEquation as a substitute for bound value
// Operator order is parenthesis first, then Left-To-Right (no operator precedence)
public class MathConverter : IValueConverter
{
private static readonly char[] _allOperators = new[] { '+', '-', '*', '/', '%', '(', ')' };
private static readonly List<string> _grouping = new List<string> { "(", ")" };
private static readonly List<string> _operators = new List<string> { "+", "-", "*", "/", "%" };