Skip to content

Instantly share code, notes, and snippets.

View preslavrachev's full-sized avatar

Preslav Rachev preslavrachev

View GitHub Profile
@preslavrachev
preslavrachev / duration.ex
Created April 10, 2021 13:14
Duration until a given date
def time_until_reset() do
today = :erlang.date()
now = :erlang.time()
now_in_sec = :calendar.datetime_to_gregorian_seconds({today, now})
days_until_next_monday = 8 - :calendar.day_of_the_week(today)
midnight = {0, 0, 0}
next_monday_in_sec =
:calendar.datetime_to_gregorian_seconds(
@preslavrachev
preslavrachev / README.md
Last active November 16, 2021 10:19
Generic Go Optionals

Goal

The goal of this experiment is to try and implement the minimum requirements for the existence of a generic-style Option type in Go, similar to what we know from other languages, such as Java or Rust.

Motivation

Provide a fluent approach to error handling in Go which focuses on highlighting the "happy path", while being 100% idiomatic with Go's error handling convention.

NOTE: The core type is an interface called Option, having a single requirement:

@preslavrachev
preslavrachev / do.go
Last active August 4, 2022 13:15
Do is an experiment in mimicking the Result type (known from languages like Rust and Haskell). The idea is to minimize the amount of error checking and let the developer focus on the happy path, without undermining the power of Go's errors. The code below requires Go 1.18+, as it makes use of generic type parameters.
package do
type Void struct{}
type Result[T any] interface {
Unwrap() (T, error)
}
type defaultResult[T any] struct {
val T
@preslavrachev
preslavrachev / claude_tokens.5m.py
Created July 18, 2025 07:38
An xbar plugin that displays your current Claude Code usage on your Mac's toolbar
#!/usr/bin/env python3
# <xbar.title>Claude Token Usage</xbar.title>
# <xbar.version>v1.0</xbar.version>
# <xbar.author>Preslav Rachev</xbar.author>
# <xbar.desc>Shows today's Claude Code token usage in the Mac toolbar</xbar.desc>
import json
import subprocess
import os