Skip to content

Instantly share code, notes, and snippets.

View gibson042's full-sized avatar

Richard Gibson gibson042

View GitHub Profile
@gibson042
gibson042 / highlight.awk
Last active March 10, 2022 15:28
highlight.awk - ANSI-format matching input (e.g. to supplement grep)
#!/usr/bin/awk -f
BEGIN {
USAGE="Usage: %s rule=<awk ERE pattern>/<ANSI SGR format>[.<extra format>]... [rule=...]...";
EXAMPLE="Example: </etc/resolv.conf %s rule='#.*'/dim rule='([0-9]+[.]){3}[0-9]+'/bold.red";
CMD=ENVIRON["_"];
if(!CMD) CMD="$0";
USAGE=sprintf(USAGE, CMD) "\n" sprintf(EXAMPLE, CMD);
}
function usage(msg, code) {
if(msg!="") print "[ERROR] " msg > "/dev/stderr";
@gibson042
gibson042 / route.sh
Created January 14, 2021 00:21
Use awk to implement `ip route`-like behavior
#!/bin/sh
awk '
BEGIN { OFS="\t"; HEX_LOWER="0123456789abcdef" }
FILENAME ~ /trie$/ { if($0 ~ "/32 host") ADDRS[ipv4_decimal_to_bits(prev)]=prev; prev=$2; next }
FNR==1 { print $1, $2, $3, $8, "Source"; next }
{
iface=$1; dest=hex_reverse_octets($2); gateway=hex_reverse_octets($3); mask=hex_reverse_octets($8);
source_guess=lookup_by_longest_prefix(ADDRS, apply_bitmask(hex_to_bits(dest), hex_to_bits(mask)));
print iface, ipv4_hex_to_decimal(dest), ipv4_hex_to_decimal(gateway), ipv4_hex_to_decimal(mask), source_guess;
}
@gibson042
gibson042 / od.sh
Created January 6, 2021 19:48
Use gawk to implement `od`-like behavior for system recovery
#!/bin/sh
# Explanation:
# * `-v CHUNK_SIZE=128 -v CHUNK_OFFSET=0 -v CHUNK_COUNT=32` configures the process:
# * 128 octets per chunk
# * don't skip any leading chunks
# * stop after outputting data for 32 chunks (i.e., 128×32=4 KiB of data)
# * `LC_ALL=C` makes sure that numbers above 127 don't get transformed into multi-octet sequences.
# * `--re-interval` and `RS=".{" CHUNK_SIZE "}"` expresses fixed-length records of CHUNK_SIZE octets.
# * `OCTETS` is a string of all possible octets 0x00 through 0xFF in numerical order.
# * `NR>CHUNK_OFFSET` only matches after the first CHUNK_OFFSET records.
#!/usr/bin/env python
description = """
Replaces JSON Schema/OpenAPI "allOf" arrays with an expansion of their contents.
An "allOf" array containing a reference to its parent schema is not replaced,
although the self-reference is removed.
"description" and "summary" are not copied from a $ref target unless it is
a duplicate of the previous $ref target.
@gibson042
gibson042 / editor-tweaks.user.js
Last active November 25, 2024 16:13
Editor Tweaks user script
// ==UserScript==
// @name Editor Tweaks
// @namespace https://github.com/gibson042
// @description <{Command,Ctrl}+[;BIK…]> commands to control Markdown inside textareas, {Command,Ctrl}+Alt+mousedown to toggle monospace.
// @source https://gist.github.com/gibson042/13f4bf8ee4c0905c0abcb0f4edbcf55f
// @updateURL https://gist.github.com/gibson042/13f4bf8ee4c0905c0abcb0f4edbcf55f/raw/editor-tweaks.user.js
// @downloadURL https://gist.github.com/gibson042/13f4bf8ee4c0905c0abcb0f4edbcf55f/raw/editor-tweaks.user.js
// @version 0.6.1
// @date 2024-11-25
// @author Richard Gibson <@gmail.com>
@gibson042
gibson042 / github-expand-previous.png
Last active May 1, 2019 22:27
GitHub Expand Previous user script
github-expand-previous.png
@gibson042
gibson042 / focus_search.user.js
Last active September 8, 2023 21:52
Focus Search user script
// ==UserScript==
// @name Focus Search
// @namespace https://github.com/gibson042
// @description Responds to <{Command,Ctrl}+/> by attempting to find and focus a page's search box.
// @source https://gist.github.com/gibson042/c9b3406bc54f55726ec4
// @updateURL https://gist.github.com/gibson042/c9b3406bc54f55726ec4/raw/focus_search.user.js
// @downloadURL https://gist.github.com/gibson042/c9b3406bc54f55726ec4/raw/focus_search.user.js
// @version 0.4.0
// @date 2023-09-08
// @author Richard Gibson <@gmail.com>
@gibson042
gibson042 / README.md
Last active August 29, 2015 14:21
Git and GitHub

Clone me with the URL at right! Make changes!

@gibson042
gibson042 / gist:958d433d6aa3f0fc642b
Last active August 29, 2015 14:14
jQuery optimization ideas

jQuery optimization ideas

Size

Simplify pnum

[eE] is overkill while all regular expressions incorporating pnum are theirselves case-insensitive.

jQuery.cssNumber values

@gibson042
gibson042 / 00-notes.md
Last active November 23, 2021 19:11
jQuery.xhr

AJAX Examples

jQuery.xhr will be an asynchronous HTTP request API returning standards-compliant thenables. We have identified two possible approaches:

  • Autosend: control by options parameter; advanced functionality via beforeSend (similar to jQuery.ajax)
  • Chainable: control by options and/or chainable setters; advanced functionality via send: false and a chainable send method

This gist compares the approaches by imagining their use for current jQuery.ajax examples. To better highlight differences, chaining methods will be used instead of options more heavily than is likely in actual practice (assuming that options is available in the "Chainable" solution).