Skip to content

Instantly share code, notes, and snippets.

View DavideGalilei's full-sized avatar
🐝

Davide Galilei DavideGalilei

🐝
View GitHub Profile
@DavideGalilei
DavideGalilei / with.nim
Last active December 8, 2021 16:26
Nim with macro
import macros
proc replaceIdents(node: NimNode, name, to: string): NimNode =
result = node
for i in 0 ..< len(result):
if result[i].kind == nnkIdent and eqIdent(result[i], name):
result[i] = ident(to)
else:
result[i] = result[i].replaceIdents(name, to)
@DavideGalilei
DavideGalilei / timeout.nim
Last active April 5, 2024 11:44
Nim await future with timeout
import std / asyncdispatch
type TimeoutError* = ref object of CatchableError
timeout*: int
proc timeoutFuture*[T](future: Future[T], timeout: int): Future[T] {.async.} =
let timeoutFuture = sleepAsync(timeout * 1000)
timeoutFuture.callback = proc =
if not future.finished:
future.fail(TimeoutError(msg: "The future didn't complete in time", timeout: timeout))
@DavideGalilei
DavideGalilei / arp_request.nim
Created April 9, 2022 18:37
ARP network request in Nim (local ip address to mac address)
# Credits to:
# https://stackoverflow.com/questions/16710040/arp-request-and-reply-using-c-socket-programming
# https://stackoverflow.com/questions/55203086/how-to-use-raw-socket-send-and-receive-arp-package-in-python
# https://www.howtogeek.com/70374/how-to-geek-explains-what-is-wake-on-lan-and-how-do-i-enable-it/
# https://datatracker.ietf.org/doc/html/rfc826
import std/[net, nativesockets, strutils, streams, strformat]
import posix
var
@DavideGalilei
DavideGalilei / pyrogram_stream.py
Last active May 19, 2022 15:11
Stream and reupload a file with pyrogram on the fly
import os
from typing import BinaryIO
from pyrogram import Client, filters
from pyrogram.types import Message
bot = Client(
"stream",
api_id=int(os.getenv("API_ID")),
api_hash=os.getenv("API_HASH"),
@DavideGalilei
DavideGalilei / combine.py
Last active June 19, 2022 14:30
Combine Telegram animated stickers (.tgs) in a single one
import sys
import gzip
import json
from collections import defaultdict
if len(sys.argv) < 3:
print("Wrong args. Usage: python combine.py file1.tgs file2.tgs ...")
files = map(lambda f: open(f, "rb"), sys.argv[1:])
@DavideGalilei
DavideGalilei / broken_hashmap.c
Last active May 29, 2022 18:59
C hashmap (broken: too many collissions)
// https://stackoverflow.com/a/838413
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#define CAP 10000
#define Dict(keyT, valueT) struct { \
int set[CAP]; \
@DavideGalilei
DavideGalilei / image_to_braille.nim
Created May 30, 2022 15:23
Image to braille (without dithering)
import std / [os, unicode]
import pkg / pixie
const Root = currentSourcePath().parentDir
var im = readImage(Root / "bird512.png")
im = im.resize(80, 80)
im = im.resize(im.width - (im.width mod 2), im.height - (im.height mod 4))
const start = 0x2800 # "⠀" base braille char codepoint
@DavideGalilei
DavideGalilei / sequence.c
Last active March 9, 2023 12:52
C type safe sequence
#define SEQ_H_IMPLEMENTATION
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Seq(T) \
struct { \
T *data; \
T temp; \
size_t size; \
@DavideGalilei
DavideGalilei / speed.py
Created June 14, 2022 11:30
Speed up Telegram .tgs stickers, using a lottie precomposition layer
import sys
import gzip
import json
if len(sys.argv) < 3:
print("Wrong args. Usage: python speed.py file.tgs 150%")
sticker = json.load(gzip.open(sys.argv[1]))
stretch = 100 / float(sys.argv[2].strip(" %"))
@DavideGalilei
DavideGalilei / Makefile
Last active June 16, 2022 14:04
Mini XML parser in C (EOF are not handled correctly)
debug:
gcc -o main --std=c99 -Wall -Wextra -Werror -pedantic -g main.c && gdb ./main
run:
gcc -o main --std=c99 -Wall -Wextra -Werror -pedantic main.c && ./main