Skip to content

Instantly share code, notes, and snippets.

import math
def findfrac(val=math.sqrt(2)):
ratio={}
for i in range(1,100):
for j in range(1,100):
ratio[i*100+j]=math.fabs(i/j-val)
mk,mv=None,10000
for k,v in ratio.items():
if v<mv:
@dinhngtu
dinhngtu / Get-CommitHistory.ps1
Created July 18, 2023 19:53
Get-CommitHistory.ps1
[CmdletBinding()]
param (
[Parameter(Position = 0)]$Range,
[Parameter()][switch]$Long
)
function GetWeekOfYear() {
param($d)
$cal = [cultureinfo]::InvariantCulture.Calendar
return $cal.GetWeekOfYear($d, [System.Globalization.CalendarWeekRule]::FirstFullWeek, [System.DayOfWeek]::Monday)
" Derived from the Earendel colorscheme by Georg Dahn:
" http://vim.sourceforge.net/scripts/script.php?script_id=2188
hi clear
if exists("syntax_on")
syntax reset
endif
let colors_name = "paperwork"
#!/bin/sh
avail=$(sed -ne 's/^MemAvailable:\s*\([0-9]\+\) .*/\1/p' /proc/meminfo)
high=$(expr $avail \* 70 / 100)
max=$(expr $avail \* 85 / 100)
systemd-run --user --slice limake.slice --scope --same-dir --collect -p MemoryHigh=${high}K -p MemoryMax=${max}K /usr/bin/make "$@"
@dinhngtu
dinhngtu / main.c
Last active April 6, 2023 12:15
iconv example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <uchar.h>
#include <iconv.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
@dinhngtu
dinhngtu / percentage.cpp
Last active March 22, 2023 16:42
pointlessly complicated percentage formatter
std::string format_percentage(uintmax_t a, uintmax_t b, unsigned int precision = 0) {
if (b == 0)
return "??%"s;
else if (a == 0)
return "0%"s;
else if (a == b)
return "100%"s;
else if (a > b)
return ">100%"s;
#include <iostream>
#include <unistd.h>
#include <sys/eventfd.h>
int main(int argc, char *argv[]) {
int ret;
bool nonblock = false;
if (argc == 2 && std::string("--nonblock") == argv[1]) {
std::cout << "enabling nonblock\n";
nonblock = true;
@dinhngtu
dinhngtu / ppt.c
Created November 24, 2022 12:06
efficiency mode
#include <Windows.h>
int main() {
PROCESS_POWER_THROTTLING_STATE pi = {
PROCESS_POWER_THROTTLING_CURRENT_VERSION,
PROCESS_POWER_THROTTLING_EXECUTION_SPEED,
PROCESS_POWER_THROTTLING_EXECUTION_SPEED
};
SetProcessInformation(GetCurrentProcess(), ProcessPowerThrottling, &pi, sizeof(pi));
SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
@dinhngtu
dinhngtu / NanaWix.wxs
Last active August 30, 2022 20:12
NanaZip WiX setup
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product
Id="*"
Name="NanaZip"
Language="!(bind.fileLanguage.NanaZip.exe)"
Version="!(bind.fileVersion.NanaZip.exe)"
Manufacturer="Kenji Mouri"
UpgradeCode="951cc21a-fbaa-44b1-82e2-c4f8eb57af02">
@dinhngtu
dinhngtu / Makefile
Created July 4, 2022 21:58
minimal multitarget Makefile
CPPFLAGS+=-D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -MMD -MP
CFLAGS+=-Wall -Wextra -Wformat=2 -std=c11
CXXFLAGS+=-Wall -Wextra -Wformat=2 -std=c++20
# optional, add dependencies if needed
CPPFLAGS+=-pthread
LDLIBS+=-pthread
ifeq ($(HARDENING), 1)
CPPFLAGS+=-D_FORTIFY_SOURCE=2