Skip to content

Instantly share code, notes, and snippets.

View umurgdk's full-sized avatar

Umur Gedik umurgdk

View GitHub Profile
@umurgdk
umurgdk / fd_over_socket.zig
Last active November 7, 2024 10:15
Sharing file descriptors over unix domain sockets in zig
const std = @import("std");
const posix = std.posix;
const c = std.c;
pub const SCM_MAX_FD = 253;
// Taken from https://git.musl-libc.org/cgit/musl/tree/include/sys/socket.h
pub const SCM_RIGHTS = 0x01;
pub const SCM_CREDENTIALS = 0x02;
@umurgdk
umurgdk / String+compactOccurences.swift
Created December 27, 2023 17:08
Compact occurrences of characters to given maximum in Swift
// Created by Umur Gedik on 27.12.2023.
import Foundation
public extension StringProtocol {
func compact(occurencesOf needle: Character, to maxNeedles: Int) -> String {
var parts: [Self.SubSequence] = []
var cursor: String.Index = startIndex
while let needleIndex = self[cursor...].firstIndex(of: needle) {
if needleIndex != cursor {
@umurgdk
umurgdk / NetworkClient.swift
Last active December 8, 2023 17:33
Poor man's network client
import Cocoa
enum HTTPMethod: String {
case get
case put
case post
case delete
}
protocol Request {
@umurgdk
umurgdk / FeedMediaGrid.swift
Created December 30, 2022 18:06
Mastodon AppKit cell view
import AppKit
import NukeUI
import PinLayout
final class FeedMediaGrid: BaseNSView {
let imageView1 = LazyImageView()
let imageView2 = LazyImageView()
let imageView3 = LazyImageView()
let imageView4 = LazyImageView()
| "OS Build Version" = "21G115"
+-o J293AP <class IOPlatformExpertDevice, id 0x100000227, registered, matched, active, busy 0 (12390 ms), retain 34>
| | | | "perf-states" = <0000000090010000007b9a175e02000000a4781f7d0200000054ea2a9f020000001f1337f702000000ea3b434503000080bb2c4c87030000>
| | | | "gfx-shared-region-base" = <0080f7ff09000000>
| | | | "gpu-num-perf-states" = <06000000>
| | | | "AGXParameterBufferMaxSizeEverMemless" = 293076992
| | | | "AGXParameterBufferMaxSizeNeverMemless" = 146538496
| | | | "gpu-core-count" = 8
| | | | "AGXParameterBufferMaxSize" = 439615488
| | | | "GPUConfigurationVariable" = {"gpu_gen"=13,"num_gps"=4,"num_cores"=8,"core_mask_list"=(255),"num_frags"=8,"num_mgpus"=1}
import Foundation
import Darwin
var logID: Int64 = 0
enum Errors: String, Error {
case couldntOpenFile
case failedToWrite
}
use std::{
fs,
io::{self, BufWriter, Write},
time::Instant,
};
fn main() -> io::Result<()> {
println!("Hello, world!");
let file = fs::File::create("output.txt")?;
import Foundation
import Darwin
var logID: Int64 = 0
enum Errors: String, Error {
case couldntOpenFile
case failedToWrite
}
@umurgdk
umurgdk / TextRendering.m
Created February 21, 2022 22:01
TextRendering CoreText
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
CGContextRef ctx = [NSGraphicsContext currentContext].CGContext;
[NSColor.blackColor setFill];
CGContextFillRect(ctx, CGRectMake(0, self.label.frame.size.height, self.label.frame.size.width, self.label.frame.size.height));
CFIndex stringLen = CFStringGetLength(text);
CTFontRef font = CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, 24.0f, nil);
class FlippedClipView: NSClipView {
var stickyView: NSView?
var trailingConstraint: NSLayoutConstraint?
override var isFlipped: Bool { true }
override func layout() {
super.layout()
if trailingConstraint == nil, let scrollView = enclosingScrollView, let stickyView = stickyView {
trailingConstraint = stickyView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor, constant: 0)
trailingConstraint?.isActive = true