Skip to content

Instantly share code, notes, and snippets.

View koonix's full-sized avatar
🦆
honk

koonix

🦆
honk
View GitHub Profile
@koonix
koonix / withcloser_reader.go
Last active September 18, 2024 17:30
Go package that adds Close to Reader/Writer
package withcloser
import "io"
type reader struct {
io.Reader
close func() error
}
func (r reader) Close() error {
@koonix
koonix / addlicense.sh
Created September 5, 2024 12:33
Add license headers to source files. This example adds the Apache header to Golang files.
#!/bin/bash
set -eu -o pipefail
# This script ensures source code files
# have copyright license headers.
#
# It modifies all source files in place
# and avoids adding a license header
# to any file that already has one.
@koonix
koonix / seen.go
Created September 2, 2024 16:12
Go unique value tester.
package seen
type Seen[T comparable] struct {
Map map[T]struct{}
}
func New[T comparable](size int) Seen[T] {
return Seen[T]{
Map: make(map[T]struct{}, size),
}
@koonix
koonix / random_bytes.go
Last active September 18, 2024 15:08
Go random byte generator
func RandomBytes(length int) []byte {
const charset = "abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\t\n "
b := make([]byte, length)
for i := range b {
b[i] = charset[rand.Intn(len(charset))]
}
return b
}
@koonix
koonix / random_string.go
Last active September 18, 2024 15:08
Go random string generator
package rndstr
import (
"crypto/rand"
"encoding/base64"
)
func RndStr(n int) (string, error) {
rnd := make([]byte, n + 10)
_, err := rand.Read(rnd)
@koonix
koonix / surfboard.sh
Created September 10, 2022 22:00
like surfraw, but not a million fucking lines long.
#!/bin/bash
# like surfraw, but not a million fucking lines long.
# requires dmenu.
# the default search engine is shown at first.
# to select another search engine, press enter when
# the prompt is empty in dmenu.
# name of the default search engine
default='Searx'
@koonix
koonix / split-video-by-chapters.sh
Created September 10, 2022 21:29
split the given video/audio files by chapters using ffmpeg.
#!/bin/sh -efu
# split the given video/audio files by chapters using ffmpeg.
for file; do
ffprobe -print_format csv -show_chapters -- "$file" | cut -d, -f5,7,8 |
while IFS=, read start end chapter; do
ffmpeg -hide_banner -nostdin -ss "$start" -to "$end" -i "$file" \
-c copy -map 0 -map_chapters -1 -- "${file%.*}-$chapter.${file##*.}"
done
done
@koonix
koonix / libpulse-async-playback-example.c
Created September 10, 2022 21:17
example of using libpulse's async API for playback.
/* source: https://stackoverflow.com/questions/29977651/how-can-the-pulseaudio-asynchronous-library-be-used-to-play-raw-pcm-data#29980624
* pcm-playback: pcm-playback.c
* gcc -o pcm-playback pcm-playback.c `pkg-config --cflags --libs libpulse` */
#include <stdio.h>
#include <assert.h>
#include <pulse/pulseaudio.h>
#define FORMAT PA_SAMPLE_U8
#define RATE 44100
@koonix
koonix / magick-convert-example.c
Created September 10, 2022 21:15
Bare minimal test of a direct call to ImageMagick library's ConvertImageCommand().
/* source: https://legacy.imagemagick.org/discourse-server/viewtopic.php?p=80803#p80803
* requires the "opencl-headers" package
* compile:
* cc ConvertExample.c -o convert $(pkg-config --libs --cflags MagickCore MagickWand) */
/*
Bare minimal test of a direct call to ConvertImageCommand()
Actually this is basically what the "convert" command does.
Compile with...
@koonix
koonix / dwm-spawncwd-incomplete.c
Created September 10, 2022 21:12
find the cwd of the currently focused window (in order to spawn another command in that cwd) in dwm. not finished yet.
#include <stdio.h>
#include <glob.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define LENGTH(X) (sizeof(X) / sizeof(X[0]))
void getproccwd(const unsigned int pid, char *cwd, const unsigned int size);
void getdesc(const unsigned int pid, int *desc, const unsigned int size);