Skip to content

Instantly share code, notes, and snippets.

Sun, sand, and C

Saagar Jha saagarjha

Sun, sand, and C
Block or report user

Report or block saagarjha

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
saagarjha / webserver.service
Created Oct 6, 2019
systemd service to run a webserver
View webserver.service
Description=Python Web Server
ExecStart=/usr/bin/python3 -m http.server 80
saagarjha /
Created Aug 6, 2019
Grab timestamps for all your Hacker News comments
#!/usr/bin/env python3
import json
import re
import urllib.request
if __name__ == "__main__":
comments = json.load(urllib.request.urlopen(""))["submitted"][::-1]
for comment in comments:
timestamp = json.load(urllib.request.urlopen("" + str(comment) + ".json"))["time"]
saagarjha /
Created May 26, 2019
OpenRC init script to run a webserver
start() {
start-stop-daemon --start --chdir /share --background --exec /usr/bin/python3 -- -m http.server 80
stop() {
start-stop-daemon --stop --chdir /share --exec /usr/bin/python3 -- -m http.server
saagarjha /
Last active Mar 26, 2019
iTerm2 Script to change the theme automatically based on the system appearance
#!/usr/bin/env python3
import iterm2
async def set_theme(connection, theme):
# Themes have space-delimited attributes, one of which will be light or dark.
parts = theme.split(" ")
if "dark" in parts:
preset = await iterm2.ColorPreset.async_get(connection, "Fixed Solarized Dark")
saagarjha / darknano.c
Last active Mar 22, 2019
Disables bright colors in nano on macOS
View darknano.c
#include <string.h>
int overridden_strncasecmp(const char *s1, const char *s2, size_t n) {
if (n != 6 || strncmp(s2, "bright", n)) {
return strncasecmp(s1, s2, n);
} else {
return !0;
saagarjha / iterm-set-profile.m
Last active Jun 6, 2018
Set the correct iTerm profile based on whether dark mode is enabled or not
View iterm-set-profile.m
// Compile with clang -F/System/Library/PrivateFrameworks -framework SkyLight iterm-set-profile.m
#import <Foundation/Foundation.h>
BOOL SLSGetAppearanceThemeLegacy(void);
int main() {
if (SLSGetAppearanceThemeLegacy()) {
printf("\033]50;SetProfile=Solarized Dark\a");
} else {
printf("\033]50;SetProfile=Solarized Light\a");
View MyDocument.swift
// Logic necessary to implement a "closing handler" for a NSDocument
// This is inspired by:
// -
// -
// Keith's supporting documentation for his radar submission:
// Documentation:
// -
// Paragraph called: "Advice for Overriders of Methods that Follow the delegate:didSomethingSelector:contextInfo: Pattern"
saagarjha / AppKit Abusers
Created May 21, 2018
Apps that have special-case workarounds in Apple's core frameworks, sorted by number of exceptions (from
View AppKit Abusers
9 com.adobe.Photoshop
saagarjha / calculate.swift
Created May 7, 2018
A small command line calculator for macOS, using Calculate.framework
View calculate.swift
import Foundation
let handle = dlopen("/System/Library/PrivateFrameworks/Calculate.framework/Calculate", RTLD_LAZY)
let CalculatePerformExpression = unsafeBitCast(dlsym(handle, "CalculatePerformExpression"), to:
(@convention(c) (UnsafePointer<CChar>, Int, Int, UnsafePointer<CChar>) -> Bool).self)
let expression = Array(CommandLine.arguments.dropFirst()).joined(separator: " ")
let answer = Array<CChar>(repeating: 0, count: 100)
if CalculatePerformExpression(expression, 16, 1, answer) {
print(String(cString: answer))
saagarjha / pause4debug.c
Last active Mar 22, 2019
Simple dynamic library that, when injected, pauses programs during initialization
View pause4debug.c
// Dynamic library that pauses a short lived program during launch so that a
// debugger can attach to it. To use it, compile it on macOS:
// clang -dynamiclib pause4debug.c -o pause4debug.dylib
// On Linux:
// gcc -shared -fPIC pause4debug.c -o
// To use it, make the dynamic linker inject it using DYLD_INSERT_LIBRARIES or
// LD_PRELOAD, depending on your platform. On macOS:
// DYLD_INSERT_LIBRARIES=/path/to/pause4debug.dylib debugme
// On Linux,
// LD_PRELOAD=/path/to/ debugme
You can’t perform that action at this time.