Skip to content

Instantly share code, notes, and snippets.

View peheje's full-sized avatar

Peter peheje

View GitHub Profile
@peheje
peheje / battery.sh
Last active July 21, 2024 15:20
linux auto-switch battery profile based on AC/Battery status
#!/bin/bash
get_power_source() {
if [ -f /sys/class/power_supply/ACAD/online ]; then
cat /sys/class/power_supply/ACAD/online
else
echo "Unable to determine power source"
exit 1
fi
}
@peheje
peheje / toggle-auto-hide-taskbar.ahk
Created December 26, 2023 21:25
Windows 11 toggle auto hide start menu
#!D::HideShowTaskbar()
HideShowTaskbar() {
static ABM_SETSTATE := 0xA, ABS_AUTOHIDE := 0x1, ABS_ALWAYSONTOP := 0x2
static hide := 0
hide := !hide
APPBARDATA := Buffer(size := 2*A_PtrSize + 2*4 + 16 + A_PtrSize, 0)
NumPut("UInt", size, APPBARDATA), NumPut("Ptr", WinExist("ahk_class Shell_TrayWnd"), APPBARDATA, A_PtrSize)
NumPut("UInt", hide ? ABS_AUTOHIDE : ABS_ALWAYSONTOP, APPBARDATA, size - A_PtrSize)
DllCall("Shell32\SHAppBarMessage", "UInt", ABM_SETSTATE, "Ptr", APPBARDATA)
@peheje
peheje / touchpad-three-finger-drag.ahk
Last active December 9, 2023 23:07
Windows AHK touchpad script for three finger drag lock
; Use AHK v2.0. In Windows set Touchpad settings > Advanced Gestures > Configure three-finger gestures > Tap: Middle mouse button
; Then use this AHK script:
toggle := false
#HotIf toggle
LButton::
{
Click "Up"
global toggle
@peheje
peheje / app.nim
Created August 30, 2023 19:12
Few concepts in Nim in few lines of code
import std/httpclient
import std/strutils
import std/json
import std/random
proc toJson(o: auto): string =
result = $(%*o)
type
MyBody = object
@peheje
peheje / ThreadPoolWrapper.fs
Created February 9, 2023 21:55
Wrap the dotnet thread pool with a Go inspired "waitgroup"
open System.Threading
type WorkerGroup =
{ wait: unit -> unit
release: unit -> unit
waitGroup: unit -> unit }
let initWorkerGroup count =
let semaphore = new SemaphoreSlim(count)
@peheje
peheje / AccurateInterval.js
Last active January 3, 2023 16:29
Accurate interval
function AccurateInterval(intervalMs, callback) {
var repeat;
var expected;
var timer;
function step() {
var err = Date.now() - expected;
callback();
console.log("error was", err, "ms");
@peheje
peheje / Monte Carlo Goat.fs
Created January 2, 2023 22:32
Goat problem
let pi = System.Math.PI
let sqrt x = System.Math.Sqrt x
let inline squared x = x * x
let radiusA = 1.0
let radiusB = 1.158728 //should be "1.158728"
let area = pi*(radiusA**2.0)
let randr min max = System.Random.Shared.NextDouble() * (max - min) + min
let rand () = randr -1.0 1.0
@peheje
peheje / evo.go
Last active September 18, 2022 19:43
Differential evolution in Go
// Keep updated here https://gist.github.com/peheje/ea1d067803c8bb2a9fdbbc17b763174a
package main
import (
"fmt"
"math"
"math/rand"
"time"
@peheje
peheje / csvReader.fs
Last active September 10, 2022 18:43
Simple CSV reader F#
open Microsoft.VisualBasic.FileIO
let read (path: string) =
let rec loop (parser: TextFieldParser) output =
if parser.EndOfData then output |> List.rev
else loop parser ((parser.ReadFields()) :: output)
let parser = new TextFieldParser(path, Delimiters = [|","|], HasFieldsEnclosedInQuotes = true)
loop parser []
fun main() {
}
typealias Function = (x: Double) -> Double
typealias Rule = (f: Function, x: Double, h: Double) -> Double
fun leftRectangle(f: Function, x: Double, h: Double) = f(x)
fun midRectangle(f: Function, x: Double, h: Double) = f(x + h / 2.0)
fun rightRectangle(f: Function, x: Double, h: Double) = f(x + h)