Skip to content

Instantly share code, notes, and snippets.

View s3rvac's full-sized avatar

Petr Zemek s3rvac

View GitHub Profile
@s3rvac
s3rvac / copyq-whitespace-trimming.js
Created January 17, 2026 18:47
CopyQ command for removing leading/trailing whitespace upon copying content
//
// A CopyQ (https://github.com/hluk/CopyQ) command for removing leading/trailing
// whitespace upon copying content. Works for both plain and Rich (HTML) text.
//
// Installation:
// - Open CopyQ, go to File -> Commands (or press F6).
// - Click "+ Add", select "New command". Then, select type "Automatic".
// - Copy the following code there and click on "Apply".
//
@s3rvac
s3rvac / undercurl-konsole-tmux-nvim.md
Created February 24, 2024 14:55
Undercurl in Neovim running in Tmux and Konsole

Undercurl in Neovim running in Tmux and Konsole

A HOWTO for getting undercurl support in Neovim running in Tmux and Konsole.

Konsole

You need to be running Konsole v22.11.80 or newer (undercurl support was introduced in this commit on 2022-08-26).

Tmux

@s3rvac
s3rvac / gcc-10-debian-buster.sh
Created October 31, 2020 10:39
Steps to build GCC 10 on Debian Buster.
#!/bin/bash
#
# Steps to build GCC 10 on Debian Buster.
#
set -e -x
# Install all dependencies.
export DEBIAN_FRONTEND=noninteractive
apt update
@s3rvac
s3rvac / python-3.9-debian-buster.sh
Last active October 31, 2020 10:29
Steps to build Python 3.9 on Debian Buster, including all modules.
#!/bin/bash
#
# Steps to build Python 3.9 on Debian Buster, including all modules.
#
set -e -x
# Install all dependencies.
export DEBIAN_FRONTEND=noninteractive
apt update
@s3rvac
s3rvac / limit-virtual-memory-of-subprocess.py
Created May 3, 2018 07:54
Limits the maximal virtual memory for a subprocess in Python on Linux.
#!/usr/bin/env python3
#
# Limits the maximal virtual memory for a subprocess in Python.
#
# Linux only.
#
import subprocess
import resource
@s3rvac
s3rvac / limit-virtual-memory.c
Created December 16, 2017 14:00
Limiting the maximal virtual memory of a process within itself on Linux.
//
// Limits the maximal virtual memory of the process to half of the total
// amount of RAM on the system.
//
// Linux only.
//
// Compilation:
//
// gcc -std=c11 -pedantic limit-virtual-memory.c -o limit-virtual-memory
//
@s3rvac
s3rvac / visit-variant.cpp
Last active December 7, 2023 05:47
Visiting std::variant using lambda expressions in C++17
// $ g++ -std=c++17 -pedantic -Wall -Wextra visit-variant.cpp -o visit-variant
// $ ./visit-variant
// Implementation:
//
// Based on http://en.cppreference.com/w/cpp/utility/variant/visit
#include <variant>
template<typename... Ts> struct make_overload: Ts... { using Ts::operator()...; };
@s3rvac
s3rvac / vec-to-vec-option.rs
Created April 17, 2017 10:01
Converting a Vec<Item> to Vec<Option<Item>> in Rust
// We need to use Vec::into_iter() instead of Vec::iter() to create a consuming
// iterator that moves each value out of the vector.
fn main() {
let v = vec![1, 2, 3];
let v: Vec<Option<i32>> = v.into_iter().map(Some).collect();
assert_eq!(v, [Some(1), Some(2), Some(3)]);
}
@s3rvac
s3rvac / into-string.rs
Created April 16, 2017 15:59
A Rust function that accepts both String or &str and creates a copy of it
// Item::new() accepts both &str and String. When called, it creates a copy of
// the parameter and uses it to initialize the returned item.
//
// The trick is in the use of the Into trait:
// https://doc.rust-lang.org/std/convert/trait.Into.html
struct Item {
id: String
}
@s3rvac
s3rvac / cpp11-string-literal.cpp
Created March 29, 2015 11:28
Emulation of the standard std::string literal from C++14 in C++11.
// $ g++ -std=c++11 -pedantic -Wall -Wextra cpp11-string-literal.cpp -o cpp11-string-literal
// $ ./cpp11-string-literal
#include <string>
// Emulates the standard std::string literal ("..."s) from C++14. Since 's' is
// reserved by the standard, we have to use '_s' instead of 's'.
std::string operator "" _s(const char *str, size_t length) {
return std::string(str, length);
}