Skip to content

Instantly share code, notes, and snippets.

View tjs-w's full-sized avatar

Tejas Wanjari tjs-w

View GitHub Profile
def tabulate(dlist, width=12):
# good enough, could be sick
fmt = f"{{:^{width}}}|" * len(dlist)
sep = f"{{:^{width}}}+" * len(dlist)
print("+" + sep.format(*["-" * width] * len(dlist)))
print("|" + fmt.format(*[str(k).upper() for k, _ in dlist.items()]))
print("+" + sep.format(*["-" * width] * len(dlist)))
list_of_lists = []
max_rows = 0
for _, v in dlist.items():
@tjs-w
tjs-w / fzf_fasd_ed.zsh
Last active January 31, 2019 19:53
zsh widget to open file through fasd in editor (vim)
fzf_fasd_ed () {
local ed=${EDITOR:-vim}
local file=$(fasd -Rlf | \
fzf --height=40% --reverse --prompt="${ed}> ")
[[ -z ${file} ]] && return
# ${ed} ${file} < /dev/tty
BUFFER="${ed} ${file}"
@tjs-w
tjs-w / tcp_csum_core.c
Created February 22, 2016 20:17
TCP Checksum
uint32_t __core_csum_tcp(uint16_t *addr, uint32_t count)
{
register uint32_t sum = 0;
while (count > 1) {
sum += *addr++;
count -= 2;
}
/* Add left-over byte, if any, (zero padding) */
func Append(slice, data []byte) []byte {
l := len(slice)
if l + len(data) > cap(slice) { // reallocate
// Allocate double what's needed, for future growth.
newSlice := make([]byte, (l+len(data))*2)
// The copy function is predeclared and works for any slice type.
copy(newSlice, slice)
slice = newSlice
}
slice = slice[0:l+len(data)]
@tjs-w
tjs-w / goslice.go
Last active February 19, 2016 00:19
Golang Slice Behavior Example
package main
import "fmt"
func printSlice(s string, x []int) {
fmt.Printf("Slice %s len=%d cap=%d %v\n", s, len(x), cap(x), x)
}
func main() {
fmt.Println("Go Slice Example: Copy-on-Capacity-Overload\n")
@tjs-w
tjs-w / qotd.go
Created January 18, 2016 10:05
Quote of the Day
// qotd: Quote of the Day
package main
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"strings"
@tjs-w
tjs-w / stacktrace.go
Created January 10, 2016 07:56
Golang: Stack Trace on Panic
func stackDump(err *error, f interface{}) {
fname := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
_, file, line, _ := runtime.Caller(4) // this skips the first 4 that are called under log.Panic()
if r := recover(); r != nil {
fmt.Printf("%s (recover): %v\n", fname, r)
if err != nil {
*err = fmt.Errorf("%v", r)
}
} else if err != nil && *err != nil {
fmt.Printf("%s : %v\n", fname, *err)
@tjs-w
tjs-w / tcpmd5_server6.c
Created January 5, 2016 23:11
TCP_MD5SIG socketopt in Linux
/**
* tcpmd5_server6.c: TCP_MD5SIG
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
@tjs-w
tjs-w / lexer_config.flex
Last active January 5, 2016 11:04
LEX based Config Reader
%{
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include "config.h"
static struct config *cfg;
static unsigned char key[64], value[64];
@tjs-w
tjs-w / create-rootdisk-kernel-dbg
Last active February 23, 2016 03:49
Debugging Linux Kernel using QEMU
#!/bin/bash
# Copyright (C) Tejas Wanjari (twanjari@andrew.cmu.edu)
# GNU/GPL
# -------
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.