Skip to content

Instantly share code, notes, and snippets.

View ptitfred's full-sized avatar

Frédéric Menou ptitfred

View GitHub Profile
@ptitfred
ptitfred / gist:5090629
Last active December 14, 2015 12:59
Relative time builder in Ruby - requires ActiveSupport Time extensions
require "active_support/time"
# in 3 days
Time.fluent("+3d")
# Tomorrow at 10 am
Time.fluent("+1d @10h")
class Time
def self.fluent(at)
@ptitfred
ptitfred / thread_local.rb
Created November 23, 2012 12:29
ThreadLocal en Ruby
class ThreadLocal
def [](key)
Thread.current[key]
end
def []=(key, value)
Thread.current[key] = value
end
end
@ptitfred
ptitfred / .vimrc
Created August 29, 2012 07:56
Pretty format your JSON in VIM
function! PrettyJSON()
silent %!python -mjson.tool | sed -e 's/\s*$//'
endfunction
command! Fjson call PrettyJSON()
@ptitfred
ptitfred / .vimrc
Created August 20, 2012 11:40
Ruby VIM configuration
" F9 key will trigger syntax validation
autocmd FileType ruby map <F9> :w<CR>:!ruby -c %<CR>
" Edition configuration (tabs=2spaces & autoindent)
fun! SetupRuby()
set shiftwidth=2
set softtabstop=2
set expandtab
set autoindent
endfun
@ptitfred
ptitfred / .vimrc
Created August 20, 2012 08:37
Pretty format your XML in VIM
function! PrettyXML()
silent %!xmllint --format --encode UTF-8 --recover - 2>/dev/null
endfunction
command! Fxml call PrettyXML()
"-- format xml when trigger Ctrl-Shift-F (only if filetype=xml)
autocmd FileType xml map <C-S-F> :Fxml<CR>
@ptitfred
ptitfred / relative-load.sh
Created May 22, 2012 12:23
Expresses the load relative to processors count (in percents)
/usr/bin/printf %0.f%%\\n $(echo $(cat /proc/loadavg | cut -d" " -f1) / $(grep -c processor /proc/cpuinfo) "* 100" | bc -l)
@ptitfred
ptitfred / git-rm-others.sh
Created April 12, 2012 15:05
Remove new files, preserve ignored files
#!/bin/bash
# This can be used this way : git rm-others <rm-options>
# It will remove files with the status "?" and preserve files ignored.
# It won't remove empty directories unlike git clean -fdx
# This file should be renamed to git-rm-others and appended to the PATH
# The file is named git-rm-others.sh to enable gist syntax colorization
RM_OPTIONS=$*
@ptitfred
ptitfred / git-incr-build.sh
Created March 23, 2012 10:48
Incremental Maven Build for Modules based Maven project
#!/bin/bash -e
# NB: the file is named git-incr-build.sh to let Gist colorize, but you should name it git-incr-build
# Put this script in your PATH and call it this way:
# git incr-build clean install
# Can be combined with git pull --rebase and git push to make an "fast & safe push" script
function branchName {
git rev-parse --symbolic-full-name --abbrev-ref $1
}
@ptitfred
ptitfred / Display.java
Created February 21, 2012 15:23
Draw some BufferedImage in a 'GridLayout'ed JPanel
public class Display extends JPanel {
public Display(BufferedImage[] images) {
super(new GridLayout(0, 3));
for (BufferedImage im : images) {
add(new Drawer(im));
}
}
}
@ptitfred
ptitfred / wait-tcp.sh
Created February 9, 2012 18:19
Wait for a TCP port to be open
#!/bin/bash
function help {
echo "Usage: wait-for port [timeout]"
echo " port is a TCP port number, or the service name (for instance http, ssh)"
echo " timeout is expressed in seconds"
echo " optional (defaulted to 30)"
echo " if <= 0, no timeout"
exit 1
}