Skip to content

Instantly share code, notes, and snippets.

View IQAndreas's full-sized avatar

Andreas Renberg IQAndreas

View GitHub Profile
@IQAndreas
IQAndreas / gist:0a8c4994d68649ba231f
Created August 19, 2014 02:27
`switch(true)` abuse. This is a neat trick, but should obviously never actually be done. See http://stackoverflow.com/q/25373912/617937
switch (true)
{
case (!isset($_GET['action']):
require('menu.html');
break;
case ($_GET['action'] == 'debug'):
require('core/actions/debug.php');
break;
case ($_GET['action'] == 'submit'):
require('core/actions/submit.php');
@IQAndreas
IQAndreas / get-console-encoding.py
Last active August 29, 2015 14:05
Get the encoding of the console the script is currently running in (plus a few other lines for debugging)
#!/usr/bin/env python3
import sys, locale;
print(sys.stdout.encoding);
print(locale.getpreferredencoding());
# http://www.macfreek.nl/memory/Encoding_of_Python_stdout#StreamWriter_Wrapper_around_Stdout
if sys.stdout.encoding != 'UTF-8':
sys.stdout = codecs.getwriter('utf-8')(sys.stdout.buffer, 'strict')
if sys.stderr.encoding != 'UTF-8':
sys.stderr = codecs.getwriter('utf-8')(sys.stderr.buffer, 'strict')
@IQAndreas
IQAndreas / caesar-cipher.sh
Last active August 30, 2023 09:39
A really simple Caesar Cipher in Bash (or Shell) using `tr`, can also easily be adjusted to encrypt/decrypt ROT13 instead.
# Caesar cipher encoding
echo "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" | tr '[A-Z]' '[X-ZA-W]'
# output: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD
# Caesar cipher decoding
echo "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD" | tr '[X-ZA-W]' '[A-Z]'
# output: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
# Can also be adjusted to ROT13 instead
@IQAndreas
IQAndreas / look-and-see-ugly.js
Created August 11, 2014 12:25
The "Look and Say Sequence" as if it were written by a regular JavaScript developer.
function lookAndSay(num) {
// TODO: Needs more JQuery
return num.toString().replace(/(1+|2+|3+)/g, function(match) { return match.length.toString() + match.substring(0, 1); });
}
@IQAndreas
IQAndreas / look-and-say.js
Created August 11, 2014 11:39
The "Look and Say Sequence" in JavaScript. See: https://www.youtube.com/watch?v=ea7lJkEhytA
function lookAndSay(num) {
var look = /(1+|2+|3+)/g;
function say(match, position) {
return match.length.toString() + match.substring(0, 1);
}
return num.toString().replace(look, say);
}
@IQAndreas
IQAndreas / .bashrc
Last active August 29, 2015 14:02
Displays the status of the previous command run from the terminal. Place somewhere inside of your `.bashrc` file. Result: http://i.stack.imgur.com/r7YwR.png
# Shows the exit value of the last executed command
function previous_command_status()
{
local exit_code=$?;
case "$exit_code" in
0) printf "\033[1;4;32m%-${COLUMNS}s\033[00m" "Command successful";;
126) printf "\033[1;4;31m%-${COLUMNS}s\033[00m" "Permission problem or command is not an executable";;
127) printf "\033[1;4;31m%-${COLUMNS}s\033[00m" "Command not found";;
130) printf "\033[1;4;31m%-${COLUMNS}s\033[00m" "Script canceled";;
*) printf "\033[1;4;31m%-${COLUMNS}s\033[00m" "Command failed with exit code $exit_code";;
$ time last > /dev/null
real 0m0.006s
user 0m0.004s
sys 0m0.004s
$ time last -i > /dev/null
real 0m0.005s
user 0m0.008s
sys 0m0.000s
@IQAndreas
IQAndreas / replace-html-entities.sh
Created May 31, 2014 03:46
Replace HTML entities with the actual unicode characters with Bash
# There are more, but these were all I needed for the current project
sed -i 's/“/“/g' *.md
sed -i 's/”/”/g' *.md
sed -i 's/©/©/g' *.md
# Check if we have any HTML entities remaining (requires some manual filtering by eye)
grep '&' *.md
RED="\[\033[0;31m\]"
RESET="\[\033[00m\]"
if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
#host="@${RED}medtech${RESET}"
host="@\h"
fi
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u${host}\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
@IQAndreas
IQAndreas / .bashrc
Created May 20, 2014 09:10
Snippet from "~/.bashrc"
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi