Skip to content

Instantly share code, notes, and snippets.

@bugventure
bugventure / raiseterminal.sh
Last active August 29, 2015 14:07
Bash script to start a new or focus any open terminal window in Ubuntu
if
ps aux | grep "[g]nome-terminal" > /dev/null
then
xdotool windowactivate `xdotool search --onlyvisible --class gnome-terminal`
else
gnome-terminal &
fi
@bugventure
bugventure / .bashrc
Last active September 15, 2016 23:05
cwd-only bash prompt with posh-git-sh
# https://github.com/lyze/posh-git-sh
# posh-git-sh (symbolic link from the posh-git-sh github repo is required first)
source ~/git-prompt.sh
# PROMPT_COMMAND='__posh_git_ps1 "\u@\h:\w" "\\\$ "'
PROMPT_COMMAND='__posh_git_ps1 "[\w]" "\\\$ "'
@bugventure
bugventure / uuid.js
Last active August 1, 2024 22:54
UUID regex matching in node.js
function createUUID() {
return uuid.v4();
}
// version 4
// createUUID.regex = '^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[89abAB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$';
createUUID.regex = '^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$';
createUUID.is = function (str) {
return new RegExp(createUUID.regex).test(str);
@bugventure
bugventure / chain.js
Last active July 2, 2019 03:02
Express middleware chain implementation
module.exports = function chain() {
var steps = Array.apply(null, arguments);
return function middleware(req, res, next) {
(function dequeue() {
var step = steps.shift(),
callback = steps.length ? function (err) {
if (err) {
return next(err);
}
@bugventure
bugventure / requiredir.js
Created October 20, 2014 21:28
node.js requiredir()
function requiredir(dir) {
var fullpath = path.resolve(__dirname, dir),
ret = {},
filepath,
stat,
key;
fs.readdirSync(fullpath).forEach(function forEachFile(file) {
filepath = path.join(fullpath, file);
stat = fs.statSync(filepath);
function type(obj) {
var str = Object.prototype.toString.call(obj);
return str.substr(8, str.length - 9).toLowerCase();
}
function isInteger(obj) {
return (obj | 0) === obj; // jshint ignore: line
}
@bugventure
bugventure / base58.js
Created September 13, 2016 15:06
Base58 encode/decode a decimal number
var alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
var base = alphabet.length; // base is the length of the alphabet (58 in this case)
// utility function to convert base 10 integer to base 58 string
function encode(num) {
var encoded = '';
while (num){
var remainder = num % base;
num = Math.floor(num / base);
encoded = alphabet[remainder].toString() + encoded;
// Source: http://stackoverflow.com/questions/2593637/how-to-escape-regular-expression-in-javascript
RegExp.quote = function(str) {
return (str+'').replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&");
};
if
ps aux | grep "[c]hrome" > /dev/null
then
xdotool windowactivate `xdotool search --onlyvisible --class google-chrome`
else
google-chrome &
fi
if
ps aux | grep "[s]ublime_text" > /dev/null
then
xdotool windowactivate `xdotool search --onlyvisible --class sublime_text`
else
subl &
fi