Skip to content

Instantly share code, notes, and snippets.

View rberenguel's full-sized avatar

Ruben Berenguel rberenguel

View GitHub Profile
@rberenguel
rberenguel / reddi.py
Last active July 7, 2019 11:22
*reddi.py*: Browse reddit from the command line (you'll have to change the location of your python *reddit*: Bindings to use reddipy from within the acme text editor from plan9ports (Plan 9 from User Space) reddit assumes reddi.py is in the same folder. Change it to suit your tastes. To read a little more about the shell reddit script that drive…
#!/opt/local/Library/Frameworks/Python.framework/Versions/2.6/bin/python
import urllib,json
import argparse
parser = argparse.ArgumentParser()
parser.add_argument( 'subreddit', nargs=1,help="Subreddit to browse, f.e. programming")
args= parser.parse_args()
subreddit = args.subreddit[0]
a = urllib.urlopen('http://www.reddit.com/r/'+str(subreddit)+'/.json')
try:
@rberenguel
rberenguel / friendly_reddit_button.js
Last active December 23, 2015 10:49
A slightly better reddit upvote/downvote button
jQuery(document).ready(function() {
// This is the poor man's way of doing an straightforward, async load.
// I needed the reddit button in our pages, and our technical guy asked me
// to get an async version, since we have a very fast website and we'd rather
// keep it. Since reddit does not offer an async version this is the best I
// could come up after giving it 5 minutes thought. I could probably get
// something better which is actually async. Waiting for document.ready means that
// if something else is "very" blocking, the reddit button may never appear. But
// I'd rather settle for good enough than spend an stupidly long amount of time
// looking for the perfect solution to a minor problem.
@rberenguel
rberenguel / PoorMan$.js
Last active December 25, 2015 17:29
A very poor man's HTML element selector. As much as I tried I couldn't fit it in a tweet, though. The best I could minify it has been to 154 bytes (as $$ below, it would be cool if it could be called ¢...)
function $(s){
z="getElement";n="Name";
a={"#" : z+"ById",
"." : z+"sByClass"+n,
}[s[0]];
a=typeof a==="undefined"?[z+"sByTag"+n,0]:[a,1];
return document[a[0]](s.slice(a[1]));
}
function $$(s){return S="slice",d=document,t=s[0],g="getElement",n="Name",t=="#"?d[g+"ById"](s[S](1)):t=="."?d[g+"sByClass"+n](s[S](1)):d[g+"sByTag"+n](s)}
@rberenguel
rberenguel / view_html.el
Created December 22, 2013 17:51
Small emacs lisp snippet to open email as HTML in default browser. Found it somewhere over the net, but it is so awesome I need to save it for posterity. In a summary buffer use v h (think of it as "view html") to switch to HTML view. It works by generating a temporary file and stripping the html part of the current message into it.
(defun my-gnus-summary-view-html-alternative ()
"Display the HTML part of the current multipart/alternative MIME message
in current default browser."
(interactive)
(save-current-buffer
(gnus-summary-show-article)
(set-buffer gnus-article-buffer)
(let ((file (make-temp-file "html-message-" nil ".html"))
(handle (nth 3 (assq 1 gnus-article-mime-handle-alist))))
(mm-save-part-to-file handle file)
@rberenguel
rberenguel / evil-capitalise.el
Created January 29, 2014 10:35
Defining an operator in evil is easy. Example: evil-capitalise (bound to gc, then followed by a motion or text object)
(evil-define-operator evil-capitalise (beg end type)
"Convert text to lower case."
(if (eq type 'block)
(evil-apply-on-block #'evil-capitalise beg end nil)
(capitalize-region beg end)))
(define-key evil-normal-state-map "gc" 'evil-capitalise)
@rberenguel
rberenguel / mu4e_niceties.el
Last active August 29, 2015 14:08
The nicest would be :ft, which shows from/to depending on the source, hence making "sent" a more useful folder (since I'm pretty sure the "From" field in "Sent" is "me"
(setq mu4e-bookmarks
'( ("flag:unread AND NOT flag:trashed AND date:today..now AND maildir:/INBOX" "Today's inbox" ?i)
("date:today..now AND NOT maildir:/INBOX.Sent" "Today's messages" ?t)
("date:30d..now AND maildir:/INBOX.Sent" "Sent last 30 days" ?s)
("date:7d..now" "Last 7 days" ?w)
("mime:image/*" "Messages with images" ?p)
("size:1M..500M" "Big messages" ?b))
)
(add-to-list 'mu4e-header-info-custom
@rberenguel
rberenguel / Instapaper_dumper
Last active August 29, 2015 14:22
Dump (if correctly cleaned up) instapaper links from a folder-category to a folder
csvfix find -e 'Videos' -f 4 ~/Downloads/instapaper-export.csv | csvfix edit -e "s/[ ,.|:'\?]/_/g" -f 2 | csvfix order -f 2,1,3,4 | csvfix printf -fmt "wget --directory-prefix='./%s/' -T 60 -q -p -k %s" > x;cat x;chmod +x x;./x;rm ./x
@rberenguel
rberenguel / SafariTabSwitcher.lua
Last active April 1, 2022 15:45
An interactive tab chooser for Safari written with AppleScript (yikes) for HammerSpoon (Lua scripting framework for Mac OS automation)
----
-- A tab chooser for Safari written with AppleScript (yikes) and HammerSpoon
----
----
-- Known issues:
-- If a tab has double quotes, unsure about what will happen, the tab
-- list creation is brittle. Single quote is fixed though
@rberenguel
rberenguel / fun_with_ast.py
Last active January 8, 2017 17:57
Having some quick fun with the ast (and being really impressed by how grumpy (https://github.com/google/grumpy/tree/master/compiler) manages to do it so neatly for python-go, this is a quick hack to play with ast). Enters a simple (almost hardcoded for this example) class, outputs a scala-valid class that you can try on an interpreter.
from __future__ import print_function
import ast
"""
Save the following in test.py, then execute fun_with_ast.py.
You can paste the output in a Scala interpreter
class something(object):
def __init__(self, value1, value2):
@rberenguel
rberenguel / git_complete.sh
Last active September 3, 2017 09:02
Fzf (see https://github.com/junegunn/fzf) completion for git checkout (which I have aliased to co). See (a previous version without origin removal) in action here: https://asciinema.org/a/DCZH8q4mZHr79uVGZcM5RCtRb
_fzf_complete_git() {
ARGS="$@"
local branches
branches=$(git branch -vv --all | sed "s/remotes\/origin\///g" | sed "s/\*/ /g")
if [[ $ARGS == 'git co'* ]]; then
_fzf_complete "--reverse --multi" "$@" < <(
echo $branches
)
else
eval "zle ${fzf_default_completion:-expand-or-complete}"