Skip to content

Instantly share code, notes, and snippets.

View IQAndreas's full-sized avatar

Andreas Renberg IQAndreas

View GitHub Profile
@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";;
@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 / 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 / caesar-cipher.sh
Last active July 22, 2025 22:46
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 / 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 / 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 / static-redeclare.php
Created August 21, 2014 14:17
Unable to create a PHP function with the same name as a static function. Results in the error message listed below. See: http://stackoverflow.com/q/25428887/617937
<pre><?php
class Module
{
// ---- Static methods ----
private static $config = array();
public static function get_config($module_name, $field)
{
if (!isset(Module::$config[$module_name]))
@IQAndreas
IQAndreas / gist:08cd95ac2c497bdeda56
Created November 28, 2014 04:12
How to flip a 2x3 matrix
[ a b ]
[ c d ]
[ e f ]
[ e c a ]
(╯°□°)╯︵ [ f d b ]
@IQAndreas
IQAndreas / exit.coffee
Created December 16, 2014 00:53
Hubot script to help the user if they tried to exit the chat room using the wrong command.
// Alerts the user if they typed one of the following (with our without a preceeding backslash)
// exit
// quit
module.exports = (robot) ->
robot.respond /([^:]+): \\?(exit|quit)/i, (msg) ->
user = msg.match[1]
msg.send("#{user}: Try /exit")
@IQAndreas
IQAndreas / not-a-shell.coffee
Created December 16, 2014 00:57
Hubot script which yells at users who try to treat [`ssh-chat`](https://github.com/shazow/ssh-chat) as if it were a shell
// Yells at users for thinking this is an SSH shell
// Responds to the following commands (case sensitive, obviously):
// ls
// rm
// cat
module.exports = (robot) ->
robot.respond /([^:]+): (ls|rm|cat)/, (msg) ->
user = msg.match[1]
msg.send("Damn it, #{user}. This is a chat room, not a shell!")