Snippets are one of my favorite parts of programming. The shear endless depth of command line tricks and customizations is so exciting! Here is a collection I maintain for myself to comeback to.

Also navigable by list of snippet types here.


measuring node.js function performance -- 23.08.2021 %

How fast is that new function?

 import { xx } from 'xx';
+import { performance } from 'perf_hooks';
@@ -160,7 +161,10 @@ const userSessionMiddleware = async (req: XRequest, res: ExpressResponse, ne
+        var t0 = performance.now();
         req.isBot = headersIndicateBot(req.headers);
+        var t1 = performance.now();
+        console.log('Took', (t1 - t0).toFixed(4), 'milliseconds to calculate is bot');

source: https://www.sitepoint.com/measuring-javascript-functions-performance/

\- [ js, node-js ]

init yubikey -- 16.07.2021 %

Some servers at work require yubikey authentication and for some reason I have to fetch and verify every time I want to use it.

$ export SSH_AUTH_SOCK=~/.gnupg/S.gpg-agent.ssh
$ gpg --card-edit

fetch
verify
enter key
via pass yubikey
quit

$ ssh-add -L
$ ssh travis@serverX
\- [ yubikey, gpg ]

add types to a javascript when using typescript -- 12.07.2021 %

Today I had a .js file that I needed to import into the rest of a TypeScript app but could not convert it to a .ts file. To get around this limitation I added an accompanying .d.ts file.

countryConfig.js

const countries = [
    {
        code: 'de',
        domainExtension: '.de'
    },
    ...
];

exports.countries = countries;

countryConfig.d.ts

interface Country {
    code: string;
    domainExtension: string;
}

export const countries: Array<Country>;

app.ts

import countries from './countryConfig'

...

source: https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-d-ts.html

\- [ js, typescript ]

filtered git diff -- 25.06.2021 %

How to browse git diff of two hashes but exclude some files. In this case any file with test in the name won’t be in the diff.

gd xxxsha001xxx...xxxsha002xxx -- $(gd --name-only xxxsha001xxx...xxxsha002xxx | rg --invert-match test)

“gd” == “git diff”, via zsh git plugin

source

\- [ git ]

locally host istanbul js results -- 23.06.2021 %

At work we use istanbul js for code coverage but at times the cli output is not enough to debug. Luckily istanbul also outputs a HTML report that can be viewed with the following command:

$ npx http-server ./coverage/lcov-report
\- [ js, npx, npm ]

markdown to org file conversion -- 21.06.2021 %

I use the Bear Notes app on IOS which has a nice markdown export feature. From there it is easy to air drop the file to my mac then I paste it into my org notes after converting it.

$ pandoc -f markdown -t org -o note.org /tmp/md_note.md && pbcopy < /tmp/md_note.md

source: stackexchange

\- [ org, markdown ]

gpg usb workflow -- 13.06.2021 %

How to use a GPG key stored on a flash drive to encrypt files? I was perplexed for sometime. Eventually I figured out instead of exporting, importing, file system linking.. you just use a remote key ring that contains the keys you want!

  1. Create the new key on the flash drive with
$ gpg --full-generate-key --homedir /Volumes/usb_flash_stick/key_homedir
  1. Use that new public key to encrypt files
$ gpg --encrypt-files --homedir /Volumes/usb_flash_stick/key_homedir -r XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ./file_a

This also bring the possibility of only storing the public key locally and having the secret key safe on the USB. See how to move keys snippet.

\- [ gpg ]

list tar contents -- 13.06.2021 %

Before putting a tar file somewhere hard to access like S3 Glacier I note what is in it. I create a manifest file. Simply list the file names within:

$ tar tvf example.tar

With more automation worked in I came up with this:

  • list all the tar files using fd (rust find replacment)
  • list content of each one
  • pipe that into a file for safe keeping
$ for x in $(fd -e tar) ; do (tar tvf "$x" && echo "\n") ; done > /tmp/example_manifest
\- [ tar ]

split files -- 13.06.2021 %

To prevent upload errors to S3 Glacier I keep my files ~500mb. So larger ones must be split.

$ split -b 500mb example.txt example.txt.part

perl script example

\- [ split ]

org-roam capture templates -- 06.04.2021 %

Recently I’ve started using org-roam, so far so good. Utilizing capture buffers I create notes for work and my reefing aquarium hobby. Adding the roam tags manually became a pain so now I’ve figured out a way to prefill them with capture templates.

(setq org-roam-capture-templates
        '(("r" "reef" plain (function org-roam-capture--get-point)
           "%?"
           :file-name "%<%Y%m%d%H%M%S>-${slug}"
           :head "#+title: ${title}\n#+roam_tags: reef"
           :unnarrowed t)
          ("w" "work" plain (function org-roam-capture--get-point)
           "%?"
           :file-name "%<%Y%m%d%H%M%S>-${slug}"
           :head "#+title: ${title}\n#+roam_tags: work"
           :unnarrowed t)
          ("d" "default" plain (function org-roam-capture--get-point)
           "%?"
           :file-name "%<%Y%m%d%H%M%S>-${slug}"
           :head "#+title: ${title}\n"
           :unnarrowed t)))

source: https://www.reddit.com/r/orgmode/comments/lmlsdr/simple_question_re_orgroam_how_to_access_capture/

\- [ org, emacs ]