Skip to content

Instantly share code, notes, and snippets.

View dnmfarrell's full-sized avatar

David Farrell dnmfarrell

View GitHub Profile
@dnmfarrell
dnmfarrell / switch-wifi
Created December 1, 2023 20:32
Bash script to switch to the strongest wifi connection using nmcli
#!/bin/bash
# switches to strongest known wifi connection using nmcli
set -euo pipefail
# returns "name:signal:in-use" in order of signal strength
conn_strength() {
conns=()
while IFS= read -r line; do
conns+=("$line")
done < <(nmcli -t -f name connection show)
@dnmfarrell
dnmfarrell / json.pl
Created March 21, 2023 02:01
Scryer-Prolog definite clause grammar for JSON
% https://www.json.org/json-en.html
:- use_module(library(dcgs)).
:- use_module(library(lists)).
json --> element.
value --> object.
value --> array.
value --> string.
@dnmfarrell
dnmfarrell / mit.txt
Last active February 13, 2023 15:41
An 80 column-friendly MIT License. Only uses 77 columns so it can be prefixed with a line comment
Copyright <YEAR> <COPYRIGHT HOLDER>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
@dnmfarrell
dnmfarrell / uri-enc.asm
Last active November 22, 2022 16:13
URI encoder of non-reserved chars written in x86 NASM
; To compile:
; nasm -f elf32 -o uri-enc.o uri-enc.asm
; ld -m elf_i386 -o uri-enc uri-enc.o
section .bss
LenInp equ 1024
BufInp: resb LenInp
LenOut equ 4096
BufOut: resb LenOut
@dnmfarrell
dnmfarrell / trycopy.go
Last active May 13, 2022 16:19
trycopy - uses a try monad to copy a file
package try // https://github.com/dnmfarrell/try
import (
"fmt"
"io"
"os"
)
// based on:
// https://go.googlesource.com/proposal/+/master/design/go2draft-error-handling-overview.md
@dnmfarrell
dnmfarrell / charging.sh
Created March 14, 2022 13:41
Is your laptop battery charging?
#!/bin/sh
grep '^Charging$' /sys/class/power_supply/BAT0/status >/dev/null
@dnmfarrell
dnmfarrell / pss-demo.pl
Created February 11, 2022 20:19
Use /proc/smaps to show the difference between resident and proportional set size memory
#!/usr/bin/env perl
use strict;
use warnings;
use Linux::Smaps;
sub print_memusage {
my $smaps = Linux::Smaps->new;
printf "% 6s % 9d % 9d KB % 9d KB\n", $_[0], $$, $smaps->rss, $smaps->pss;
}
@dnmfarrell
dnmfarrell / wchar.c
Created January 21, 2022 14:51
Demo to read a wide character with ncurses and print it to the screen
/* wchar.c - read a wide character with ncurses and print it on the screen
* gcc -Wall $(pkg-config --cflags ncursesw) -o wchar wchar.c $(pkg-config --libs ncursesw)
* ./wchar
*/
#include <curses.h>
#include <locale.h>
int main(void)
{
setlocale(LC_ALL, "");
@dnmfarrell
dnmfarrell / sierpinski-main.go
Last active January 20, 2022 02:53
Generate Sierpiński's triangle in golang
// Generate Sierpiński's triangle
// 0********************************************************************2
// ****** ****** ****** ****** ****** ******* ****** ******
// ************ ************ ************ ************
// ********* ********* ******** *********
// ***** ****** ****** ******
// ******************** *******************
// **************** ******** ********
// ***** **** ***** ****
// ********** ***********
@dnmfarrell
dnmfarrell / qsort.c
Last active October 22, 2020 02:17
quicksort on a linked list "gcc -o qsort qsort.c"
#include <stdio.h>
#include <stdlib.h>
typedef struct List {
int n;
void *next;
} List;
List* ListNew (int n) {
List *l = malloc(sizeof(List));