Skip to content

Instantly share code, notes, and snippets.

View oprypin's full-sized avatar

Oleh Prypin oprypin

View GitHub Profile
@oprypin
oprypin / instructions.md
Last active April 14, 2024 04:39
systemd user unit + timer example

Save these files as ~/.config/systemd/user/some-service-name.*

Run this now and after any modifications: systemctl --user daemon-reload

Try out the service (oneshot): systemctl --user start some-service-name

Check logs if something is wrong: journalctl -u --user-unit some-service-name

Start the timer after this user logs in: systemctl --user enable --now some-service-name.timer

module Indexable(T)
def threadpool_map(workers : Int = 8, chunk_size : Int? = nil, &func : T -> R) forall T, R
mutex = Thread::Mutex.new
cs = chunk_size || (self.size**0.5).ceil.to_i
Array(R).build(self.size) do |result|
index = 0
threads = Array.new(workers) {
Thread.new do
a = b = 0
loop do
@oprypin
oprypin / script.js
Last active December 1, 2023 16:12
Add a selector with checkboxes to https://docs.astral.sh/ruff/rules/
// Create checkbox to hide preview rules
var checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.checked = true;
checkbox.addEventListener("change", function () {
for (var span of document.querySelectorAll(
'span[title="Rule is in preview"]',
)) {
span.closest("tr").style.display = event.currentTarget.checked
? null
@oprypin
oprypin / diffcolor.py
Last active October 22, 2023 01:34
Diffs with syntax highlight
import difflib
import pygments
import pygments.lexers
import pygments.formatters
def read_file(fn):
with open(fn) as f:
return f.read()
@oprypin
oprypin / merge_branch.sh
Created September 8, 2023 13:05
Merge cimgui docking_inter branch into master and re-generate
#!/bin/bash
set -ex
to_merge="${1:-docking_inter}"
imgui_branch="${2:-master}"
git merge --no-ff --no-commit "$to_merge" || true # Always fails
# But double-check that we really ended up in a merge status
git rev-list -1 MERGE_HEAD
# Reset everything to be like the merged branch
@oprypin
oprypin / __init__.py
Created September 2, 2022 22:17
Disallow unexpected logging messages in Python unittest
import logging
class DisallowLogsHandler(logging.Handler):
def emit(self, record):
raise AssertionError(f'Unexpected log: "{self.format(record)}"')
logging.lastResort = DisallowLogsHandler(level=logging.WARNING)
@oprypin
oprypin / xonshrc.py
Last active March 17, 2022 22:14
Display execution time of last command
last_duration = 0
_orig = __xonsh_history__.append
def _append(cmd):
nonlocal last_duration
last_duration = cmd['ts'][1] - cmd['ts'][0]
_orig(cmd)
__xonsh_history__.append = _append
@oprypin
oprypin / prompt_return_code.py
Last active March 17, 2022 22:14
Xonsh formatter dict: last return code, duration of last command
last_duration = last_return = 0
orig_append = __xonsh_history__.append
def append(cmd):
nonlocal last_duration, last_return
last_return = cmd['rtn']
a, b = cmd['ts']
last_duration = b - a
orig_append(cmd)
__xonsh_history__.append = append
@oprypin
oprypin / S3 config api.json
Last active December 14, 2021 22:49 — forked from straight-shoota/S3 config api
Crystal S3 Config
{
"IndexDocument": {
"Suffix": "index.html"
},
"ErrorDocument": {
"Key": "api/1.2.2/404.html"
},
"RoutingRules": [
{
"Condition": {
@oprypin
oprypin / parraleln.cr
Last active March 5, 2021 02:23
Crystal parallel jobs from an array
def paralleln(items : Indexable(T), &block : T -> R) forall T, R
results = Array(R).new(items.size) { r = uninitialized R }
done = Channel(Exception?).new
items.each_with_index do |item, i|
spawn do
begin
results[i] = block.call(item)
rescue e
done.send e