Skip to content

Instantly share code, notes, and snippets.

@t-mat
t-mat / vsprintfToVector.cpp
Created February 8, 2025 08:53
[C++]vsprintf to std::vector<char>
#include <vector> // std::vector
#include <stdarg.h> // va_list, va_start, va_end, va_copy
size_t getVsprintfByteLength(const char *fmt, va_list vlist) {
va_list tmpVlist;
va_copy(tmpVlist, vlist);
size_t const byteLength = vsnprintf(nullptr, 0, fmt, tmpVlist);
va_end(tmpVlist);
return byteLength;
}
@t-mat
t-mat / echo-path-as-url.md
Last active January 23, 2025 09:15
[Windows] echo clickable path as URL in pwsh, PowerShell and cmd.exe

Echo Ctrl + click-able hyperlink to the Windows Terminal

PowerShell & pwsh (PowerShell 5 & 7)

$my_path = $pwd
Write-Output "file:///$($my_path -replace '\\','/')"

$my_url = $my_path -replace '\\','/'
@t-mat
t-mat / image-magick-color-separation-and-compose.md
Last active January 12, 2025 21:39
ImageMagick basics: color component separation and compose

Separate each color component, save them as a PNG32 (R8G8B8A8).

magick input.png -channel R -separate PNG32:component_r.png
magick input.png -channel G -separate PNG32:component_g.png
magick input.png -channel B -separate PNG32:component_b.png
magick input.png -channel A -separate PNG32:component_a.png

Reconstruct a image from separated components, save it as a PNG32.

@t-mat
t-mat / hello-ylt-struct_pack.cpp
Created November 13, 2024 15:39
[C++]ylt/struct_pack
// struct_pack : https://alibaba.github.io/yalantinglibs/en/struct_pack/struct_pack_intro.html
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <ylt/struct_pack.hpp>
struct person
{
int64_t id;
@t-mat
t-mat / setup-caddy.md
Created October 2, 2024 07:49
[Win32]Setup Caddy HTTP file server
pwsh.exe
cd ~/../Public
mkdir TestCaddy
cd    TestCaddy
curl.exe -JOL https://github.com/caddyserver/caddy/releases/download/v2.8.4/caddy_2.8.4_windows_amd64.zip
tar.exe -xf caddy_2.8.4_windows_amd64.zip

mkdir public_html
echo HELLO &gt; public_html\index.html
@t-mat
t-mat / TestURLDownloadToFile.cpp
Created September 3, 2024 12:11
[Win32] URLDownloadToFile
// Should I use URLDownloadToFile?
// Answered by Mike Caron : https://stackoverflow.com/a/5185008
#define _CRT_SECURE_NO_WARNINGS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <Urlmon.h>
#include <string>
#include <functional>
#pragma comment(lib, "User32.lib")
@t-mat
t-mat / ControlWindowsTerminalProgressBar.md
Last active January 18, 2025 05:45
[Windows][Windows Terminal] Control Windows Terminal's progress bar from batch file

Default PowerShell

: Idle (default)
powershell -command " Write-Host -NoNewline ([char]27 + \"]9;4;3\" + [char]7) "

: WIP (rotation)
powershell -command " Write-Host -NoNewline ([char]27 + \"]9;4;0\" + [char]7) "
@t-mat
t-mat / build-nethack.cmd
Created June 1, 2024 07:48
[Windows] Download and build NetHack
@rem Download and build script for NetHack for Windows x64 Desktop, VC++2022
@echo off
setlocal EnableDelayedExpansion
set /a errorno=1
for /F "delims=#" %%E in ('"prompt #$E# & for %%E in (1) do rem"') do set "esc=%%E"
set "ROOT=%CD%"
:
: Check prerequisites
:
@t-mat
t-mat / AlpineWSL.run.bat
Created May 4, 2024 17:16
[Windows] Download and run Alpine Linux on WSL
@echo off
setlocal EnableDelayedExpansion
set /a errorno=1
for /F "delims=#" %%E in ('"prompt #$E# & for %%E in (1) do rem"') do set "_ESC=%%E"
cd /d "%~dp0"
set /a _E =0
set /a _E+=1 && if not exist "Alpine.zip" ( curl -JOL https://github.com/yuk7/AlpineWSL/releases/download/3.18.4-0/Alpine.zip )
set /a _E+=1 && if not exist "Alpine.zip" goto :ERROR
set /a _E+=1 && if not exist "Alpine.exe" ( tar -xkf .\Alpine.zip )
@t-mat
t-mat / winget-error-0x8a15005e.md
Created April 28, 2024 13:46
[Windows] `winget` keep reporting mysterious error 0x8a15005e

Problem: winget CLI command keep reporting a mysterious error code 0x8a15005e

Solution: Update winget.exe via aka.ms/getwinget

pwsh.exe
cd $env:USERPROFILE\Desktop
curl.exe -JOL https://aka.ms/getwinget
Add-AppxPackage Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.txt.msixbundle