Skip to content

Instantly share code, notes, and snippets.

@masak
masak / explanation.md
Last active April 10, 2025 16:42
How is git commit sha1 formed

Ok, I geeked out, and this is probably more information than you need. But it completely answers the question. Sorry. ☺

Locally, I'm at this commit:

$ git show
commit d6cd1e2bd19e03a81132a23b2025920577f84e37
Author: jnthn <[email protected]>
Date:   Sun Apr 15 16:35:03 2012 +0200

When I added FIRST/NEXT/LAST, it was idiomatic but not quite so fast. This makes it faster. Another little bit of masak++'s program.

@h1k3r
h1k3r / hostname.lua
Created June 25, 2014 19:52
Lua - get hostname
local _M = {}
function _M.getHostname()
local f = io.popen ("/bin/hostname")
local hostname = f:read("*a") or ""
f:close()
hostname =string.gsub(hostname, "\n$", "")
return hostname
end
return _M
@RLovelett
RLovelett / posix_spawnp.swift
Created April 21, 2016 02:22
Spawn a sub-process from Swift
import Glibc
var action = posix_spawn_file_actions_t()
posix_spawn_file_actions_init(&action);
defer { posix_spawn_file_actions_destroy(&action) }
posix_spawn_file_actions_addopen(&action, 1, "/tmp/foo-log", (O_WRONLY | O_CREAT | O_TRUNC), 0644)
posix_spawn_file_actions_adddup2(&action, 1, 2)
let args = [
"ffmpeg",
@ismyrnow
ismyrnow / mac-clear-icon-cache.sh
Created May 5, 2017 19:28
Clear the icon cache on a Mac when you start seeing generic icons in Finder or the Dock
sudo rm -rfv /Library/Caches/com.apple.iconservices.store; sudo find /private/var/folders/ \( -name com.apple.dock.iconcache -or -name com.apple.iconservices \) -exec rm -rfv {} \; ; sleep 3;sudo touch /Applications/* ; killall Dock; killall Finder
@bszwej
bszwej / echo.js
Last active February 7, 2025 15:06
Pure Node.js echo server, that logs all incoming http requests (method, path, headers, body).
const http = require('http');
const server = http.createServer();
server.on('request', (request, response) => {
let body = [];
request.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
#!/usr/bin/env xcrun -sdk macosx swift
// Displays UI in an NSWindow which can interact with the commandline
// Usage: `echo "Bar" | ./swift-ui-commandline-tool.swift`
import Foundation
import SwiftUI
extension CommandLine {
static let input: String = { AnyIterator { readLine() }.joined() }()
#! /usr/bin/swift
//
// - This is just some AppKit boilerplate to launch a window.
//
import AppKit
@available(OSX 10.15, *)
class AppDelegate: NSObject, NSApplicationDelegate {
let window = NSWindow()
let windowDelegate = WindowDelegate()
@BrentMifsud
BrentMifsud / Image+Data.swift
Last active July 14, 2024 12:50
SwiftUI Image from data
import Foundation
import SwiftUI
#if canImport(UIKit)
import UIKit
#elseif canImport(AppKit)
import AppKit
#endif
extension Image {
/// Initializes a SwiftUI `Image` from data.
@hroi
hroi / fish_jj_prompt.fish
Last active April 19, 2025 05:21
Jujutsu (jj) prompt for fish-shell
# Place me in ~/.config/fish/functions
# Then add me to `fish_vcs_prompt`: `funced fish_vcs_prompt` and save it to
# your personal config: `funcsave fish_vcs_prompt;`
function fish_jj_prompt --description 'Write out the jj prompt'
# Is jj installed?
if not command -sq jj
return 1
end
function jj-latest-description --description "Get latest non-empty commit description and bookmarks"
jj log -r 'latest(ancestors(@, 10) ~ (empty()~merges()))' \
--quiet --no-pager --no-graph --ignore-working-copy --color always \
-T "surround('(', ')', separate(' ', \
self.local_bookmarks(), \
coalesce(self.description().first_line(), '...'), \
self.change_id().shortest(5)\
))" 2>/dev/null
end