Skip to content

Instantly share code, notes, and snippets.

@jkoop
jkoop / numberToMetric.php
Last active August 12, 2021 21:35 — forked from liunian/gist:9338301
php8.0 - number to metric with prefix for things like human readable file size
<?php
function numberToMetric(int|float $number, int $significantDigits = 3, bool $binary = false): string {
static $prefixes = ['y','z','a','f','p','n','μ','m','','k','M','G','T','P','E','Z','Y'];
$factor = floor((strlen(abs($number)) - 1) / 3) * (abs($number) <=> 1);
$metricBase = $binary ? 1024 : 1000;
$number = $number / pow($metricBase, $factor);
return sprintf("%.{$significantDigits}H", $number) . $prefixes[$factor+8];
@jkoop
jkoop / curl.php
Last active December 25, 2021 00:55
php7.4 - helper function for curl
<?php
// https://gist.github.com/jkoop/a3c0e6bb7572f58b70fd5f7511b70e8f
/**
* `return->body->json` => `json_decode(return->body->dump)`
*
* @param string $url URL will not be url-encoded
* @param array $fields
* @param string $method HTTP method to use; defaults to GET
@jkoop
jkoop / screen.php
Last active August 29, 2021 00:17
php8.0 - print text to arbitrary place on the terminal with colour
<?php
class Screen {
public function __construct() {
$this->clear();
$this->fgColour = 'white';
$this->bgColour = 'black';
}
public function clear(): void {
@jkoop
jkoop / ClosureTable.php
Last active September 20, 2021 20:04 — forked from mohanklein/ClosureTable.php
Closure Table Trait for Laravel Eloquent Models
<?php // app/Http/Traits/ClosureTable.php
namespace App\Models\Traits;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Facades\Db;
/**
#include <math.h> // gcc -lm enumerateFactorsOfN.c
int enumerateFactorsOfN(int n) {
int factors = 1;
double sqrtN = sqrt(n);
int n2 = (int) sqrtN;
for (int i = 1; i <= n2; i++) {
if (n % i == 0) {
factors++;
<?php
function relativeTime(int|float|string $time): string {
static $minute = 60;
static $hour = 60 * 60;
static $day = 24 * 60 * 60;
static $month = 30 * 24 * 60 * 60;
static $year = 365 * 24 * 60 * 60;
if (!is_numeric($time)) {
@-moz-document domain("iplayif.com") {
body {
background: black;
color: white;
}
:root {
--glkote-buffer-fg: #fff;
--glkote-buffer-bg: #222;
--glkote-buffer-reverse-fg: #222;
@jkoop
jkoop / redlinks.js
Last active March 6, 2022 01:28
Try each link on the page, and if it's dead, made it red
@jkoop
jkoop / split-media-by-chapters.sh
Last active September 30, 2022 01:37
use ffmpeg to split file by chapter metadata
#!/bin/bash
while [ $# -gt 0 ]; do
extension="${1##*.}"
ffmpeg -i "$1" 2> tmp.txt
# Chapter #0:0: start 0.000000, end 1290.013333
# first _ _ start _ end
while read -r first _ _ start _ end; do
@jkoop
jkoop / human-readable-date-time.php
Last active March 23, 2022 14:19
Human-readable date/time for use in stuff like directory lists
<?php
// This is intended for use in stuff like directory lists
// 1234567890 unix time (input)
// 23:31:30 if today
// 13d 23:31 if this month
// Feb 13 23h if this year
// '09 Feb 13 if farther away