Skip to content

Instantly share code, notes, and snippets.

@WingTillDie
WingTillDie / winpath.bash
Last active July 30, 2023 07:04
wslpath -w for new file
#!/usr/bin/env bash
# Useful for specifing new WSL file path to save by vscode
function winpath() {
file_path=$(wslpath -a "$1") # Absolute path (full path)
file_basename=$(basename "${file_path}")
file_dirname=$(dirname "${file_path}")
if [ "$#" -eq 0 ]; then
wslpath -w .
elif [ "$#" -eq 1 ]; then
# Also works for new files
@WingTillDie
WingTillDie / 0_return-multi-level.cc
Last active July 27, 2023 15:33
Return multiple levels up in the call stack
#if false
g++ $0 && ./a.out
exit
#endif
// Modified code that demonstrates a code with multi-level return
#include <stdio.h>
void print_enter(const char* function_name) {
@WingTillDie
WingTillDie / toggle-volume.cc
Last active July 28, 2023 02:18
Toggle sound volume via C++
#if false
x86_64-w64-mingw32-g++ $0 -lole32 -static && ./a.exe
exit
#endif
// Above code makes it run as script
#include <cstdio>
#include <iostream>
#include <Windows.h>
@WingTillDie
WingTillDie / toggle-volume.ps1
Last active July 28, 2023 11:09
Toggle sound volume via Powershell
# Example applications
# * Use with Windows Clock UWP app (the alarm volume depends on system sound volume, you might want to maximize it)
# * Avoids accidental unmute by setting sound volume to the lowest level
function Toggle-Volume {
if ( [audio]::Volume -ne 0 ) {
# Minimize-Volume
[audio]::Volume = 0
} else {
# Maximize-Volume
[audio]::Volume = 1
@WingTillDie
WingTillDie / toggle-screen-brightness-tray.ps1
Created July 26, 2023 19:07
Toggle Screen Brightness via an icon in System Tray
# Right click to dispose
# Load necessary .NET assembly
Add-Type -AssemblyName System.Windows.Forms
# Define the functions for Get-Brightness, Set-Brightness, and Toggle-Brightness
function Get-Brightness {
# Create CIM Session
$namespaceName = "root/wmi"
@WingTillDie
WingTillDie / toggle-screen-brightness.ps1
Last active July 28, 2023 11:08
Toggle Screen Brightness via resuming an app
# Example applications:
# * Improves privacy by dimming screen to lowest brightness immediately
# * Extend battery life by toggling screen brightness (as a faster alternative to sleep/wake that doesn't locks the computer)
# ** Reduces carbon footprint
# Written in Powershell Core
# Load necessary .NET assembly
Add-Type -AssemblyName System.Windows.Forms
# Define the functions for Get-Brightness, Set-Brightness, and Toggle-Brightness
@WingTillDie
WingTillDie / webview-document-title.py
Last active July 22, 2023 20:05
Pywebview with set title from document.title
#!/usr/bin/env python3
import argparse
import webview
def set_title():
window = webview.windows[0]
result = window.evaluate_js('document.title')
window.set_title(result)
if __name__ == '__main__':
@WingTillDie
WingTillDie / kwarg-nargs.py
Last active July 16, 2023 23:41
Parse arbitrary number of keyword arguments
#!/usr/bin/env python3
import argparse
def parse_args():
usage = 'kwarg-nargs.py [-h] [pos_args ...] [-dict_args [value] ... ] ...'
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('pos_args', nargs='*')
@WingTillDie
WingTillDie / as_script.cc
Created July 16, 2023 21:25
Run C++ as script by gcc, tcc, clang
#if 0
gcc -x c++ "$0"
if [ $? -eq 0 ]; then
./a.out "$@"
fi
exit
clang -x c++ "$0"
if [ $? -eq 0 ]; then
./a.out "$@"
@WingTillDie
WingTillDie / bash_wrapper.py
Created July 16, 2023 21:14
Python wrapper for bash
#!/usr/bin/env python3
import subprocess
import sys
bash_script = '''
echo "$0"
echo "$@"
a=3
<<<$a cat
'''