Skip to content

Instantly share code, notes, and snippets.

@yyogo
yyogo / hexdump.py
Last active September 20, 2016 14:26
Hex dumper for Python (I really care about my hex dumps, okay?!)
class HexDump(object):
"""
HexDump(data, width=16, groups=(2,4), prefix='')
Dump hex.
width - Number of bytes per dump line
groups - How to group the bytes, i.e where to put spaces. Ex:
(1,) - space after every byte
@yyogo
yyogo / bint.py
Last active September 22, 2016 14:31
Convert longs to / from bytes, mimicking behavior of Python 3.6 int.from/to_bytes
def bytes2int(data, byteorder='little', signed=False):
""" replicate int.from_bytes from python3 """
if byteorder == 'little':
data = reversed(data)
elif byteorder != 'big':
raise ValueError("byteorder must be either 'little' or 'big'")
bl = bytearray(data)
@yyogo
yyogo / fzf-tmux-select.zsh
Created June 18, 2018 16:00
fzf completion widget for zsh which completes words from the currently displayed tmux panes
__get_tmux_pane_contents() {
local cmd='for pane in `tmux list-panes -F "#{pane_id}"`; do tmux capture-pane -p -t $pane; done'
for word in `eval "$cmd"`; do
if [ ${#word} -ge ${FZF_MIN_WORD_LENGTH:-4} ]; then
echo "$word"
fi
done | awk '{ print length, $0 }' | sort -rn | cut -d' ' -f2- | uniq
}
@yyogo
yyogo / cleanup.sh
Created October 11, 2018 12:46
Cleanup stuff
#!/bin/bash
# remove shit from downloads
shopt -s nullglob dotglob
current_time=`date +%s`
max_age_days=${1:-30}
max_age_days=$((max_age_days))
""" ASN1 DER/BER symmetric parser/encoder. """
import enum
def decode_varint(bytearr):
value = bytearr[0] & 0x7f
while bytearr.pop(0) & 0x80:
value = (value << 7) | (bytearr[0] & 0x7f)
return value
def encode_varint(n):
@yyogo
yyogo / f_strings.py
Last active October 28, 2019 15:55
""backported"" string-interpolation for Python 2/3.5- (What do you mean, 'why'? Why not?)
import inspect
try:
unichr
except NameError:
unichr = chr
def f(s):
# find all {}'s
stack = []
@yyogo
yyogo / to_array.rs
Created November 20, 2020 19:33
rust traits for converting iterators to arrays with no extra allocations
#![feature(min_const_generics,maybe_uninit_extra)]
// VERY UNTESTED AND NOT REVIEWED
// THIS CODE USES UNSAFE RUST AND COULD LEAD TO UB
use std::mem::{self, MaybeUninit};
#[derive(Clone,Debug)]
enum ToArrayError {
TooShort(usize, usize),
@yyogo
yyogo / take_while_inclusive.rs
Last active December 27, 2020 20:13
rust iterators: take_while_inclusive() - take_while including the first non-matching element
enum TakeWhileInclusive<I, P> {
Taking(I, P),
Done,
}
impl<I, P> Iterator for TakeWhileInclusive<I, P>
where
P: FnMut(&I::Item) -> bool,
I: Iterator,
@yyogo
yyogo / keybase.md
Created January 12, 2021 16:00
keybase identity proof

Keybase proof

I hereby claim:

  • I am yyogo on github.
  • I am yyogo (https://keybase.io/yyogo) on keybase.
  • I have a public key ASBoezTXmapIvgewk-XhFriszvIc-VfNpPd7VJ9AZhiYIgo

To claim this, I am signing this object:

@yyogo
yyogo / spliterr.sh
Last active January 23, 2024 10:37
Split stderr from comand to separate tmux pane
#!/bin/bash
set -m
showhelp() {
echo "Usage: $0 [-ohvrc] <command>"
echo "Pipes stderr from command to a new tmux pane."
echo " -h | -v split horizontally or vertically (default vertically)"
echo " -r send stdout to pane instead of stderr"
echo " -o send both stdout and stderr to new panes"
echo " -c close pane automatically after program terminates"