Skip to content

Instantly share code, notes, and snippets.

Avatar

Jeremy Black jerblack

View GitHub Profile
@jerblack
jerblack / 1. Detecting dark mode change in Windows 10.md
Last active Oct 1, 2020
Golang: Detect dark mode change in Windows 10
View 1. Detecting dark mode change in Windows 10.md

I wanted to be able to detect when Dark mode was turned on or off in my Go program so I can swap icons in my tray app on Windows so I wrote this.
When Dark Mode is turned on or off, it writes to a pair of registry keys, both of which Windows allows you to set independently in Settings > Personalization > Colors.

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize  

SystemUsesLightTheme  DWORD 0 or 1 // Changes the taskbar and system tray color  
AppsUseLightTheme     DWORD 0 or 1 // Changes app colors  

Mine is a tray app, so I am monitoring the first one. I did not want to just periodically poll the registry key and I wanted to be able to react immediately when the registry key is changed, so I am using the win32 function RegNotifyChangeKeyValue in advapi32.dll to tell the OS to notify my app so I can swap out the icon for a color-appropriate one.
I start the monitor function in main, which loads the RegNotifyChangeKeyValue function from advapi32 and starts a gorouti

@jerblack
jerblack / 1. Dark Mode in OSX with Go.md
Last active Apr 21, 2020
Golang: Detect dark mode change in OSX
View 1. Dark Mode in OSX with Go.md

I wanted to be able to detect when Dark mode was turned on or off in my Go program so I can swap icons in my tray app, but the Apple-blessed methods all use Swift and ObjectiveC. To work around this I figured out the following:

  • When dark mode is set, a property called "AppleInterfaceStyle" is set to "Dark" for the user.
  • When dark mode is disabled, that property is removed.
  • You can see this by running "defaults read -g AppleInterfaceStyle"
// Dark Mode On  
defaults read -g AppleInterfaceStyle  
 Dark  
@jerblack
jerblack / PendingFileRenameOperations.go
Created Dec 1, 2019
GoLang: Using PendingFileRenameOperations in the Windows Registry to delete/rename files on reboot with Go.
View PendingFileRenameOperations.go
package main
import (
"fmt"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
"log"
"os"
"strings"
"syscall"
@jerblack
jerblack / tee.go
Last active Jan 27, 2020
Golang: Mirror all writes to stdout and stderr in program to log file
View tee.go
package main
import (
"fmt"
"io"
"log"
"os"
)
func main() {
@jerblack
jerblack / Elevate when needed in Go.md
Last active Dec 3, 2020
Relaunch Windows Golang program with UAC elevation when admin rights needed.
View Elevate when needed in Go.md

I'm buiding a command line tool in Go that has an option to install itself as a service on Windows, which it needs admin rights for. I wanted to be able to have it reliably detect if it was running as admin already and if not, relaunch itself as admin. When the user runs the tool with the specific switch to trigger this functionality (-install or -uninstall in my case) they are prompted by UAC (User Account Control) to run the program as admin, which allows the tool to relaunch itself with the necessary rights.

To detect if I was admin, I tried the method described here first:
https://coolaj86.com/articles/golang-and-windows-and-admins-oh-my/
This wasn't accurately detecting that I was elevated, and was reporting that I was not elevated even when running the tool in CMD prompt started with "Run as Administrator" so I needed a more reliable method.

I didn't want to try writing to an Admin protected area of the filesystem or registry because Windows has the ability to transparently virtualize those writes

@jerblack
jerblack / StreamURI.md
Created Sep 2, 2019 — forked from chrisjmendez/StreamURI.md
Triton Audio streams for Classical KUSC and Classical KDFC
View StreamURI.md

Stream URL's for Classical KUSC and Classical KDFC

New URL Pattern:

http://[bit rate].[encoder format].[file format].[call letters].live

Old URL Pattern:

@jerblack
jerblack / args.py
Created Jul 8, 2019
Parse command line arguments in python using only built-in packages
View args.py
import sys, re
'''
parse command line arguments in python
parse for known flags
support arbitrary number of arguments and values per argument
'-' specifies parameter, but also handle '--','/','\'
-x 30 -args 2 3 4 -d -c
--becomes->
{'x': '30', 'args': ['2', '3', '4'], 'd': None, 'c': None}
@jerblack
jerblack / timestamp.py
Last active May 16, 2019
Convert a Postgres style timestamp to a datetime object in Python
View timestamp.py
import datetime
import re
timestamp = '2019-05-15 22:37:55.276407'
def parse_timestamp(timestamp):
return datetime.datetime(*[int(x) for x in re.findall(r'\d+', timestamp)])
# returns datetime.datetime(2019, 5, 15, 22, 37, 55, 276407)
@jerblack
jerblack / Installing Python 3.7 from source on Ubuntu 18.04.md
Last active Oct 25, 2020
Installing Python 3.7 from source on Ubuntu 18.04
View Installing Python 3.7 from source on Ubuntu 18.04.md

Installing Python 3.7 from source on Ubuntu 18.04

# update system
sudo apt update && sudo apt upgrade -y

# install build tools and python prerequisites
sudo apt install build-essential libssl-dev zlib1g-dev libncurses5-dev libncursesw5-dev libreadline-dev libsqlite3-dev libgdbm-dev libdb5.3-dev libbz2-dev libexpat1-dev liblzma-dev tk-dev libffi-dev

# download and extract python
@jerblack
jerblack / print_table.py
Last active Jul 3, 2018
Python table printing function which will print a structured table with headers, defined column widths, automatic text wrapping in columns, and automatic table sizing to fit the table within the terminal width.
View print_table.py
"""
print_table will print a structured table with headers, defined column widths, and automatic text wrapping in columns.
It will also determine the terminal width and reduce the column widths automatically so they all fit without wrapping outside of the table
print_table requires 3 parameters
headers: A list or tuple of headers (strings)
rows: A list of lists or tuples with table row information as strings
each entry in inner list maps to a column.
For a 3 column table, provide a list of lists or tuples with a length of 3
sizes: A list or tuple of integers specifying maximum column width for each column. Must provide a value for each column.
You can’t perform that action at this time.