Skip to content

Instantly share code, notes, and snippets.

@vaaski
Last active December 4, 2021 22:31
Show Gist options
  • Select an option

  • Save vaaski/d0253b83f67b30a2df979d5f83bc3a1b to your computer and use it in GitHub Desktop.

Select an option

Save vaaski/d0253b83f67b30a2df979d5f83bc3a1b to your computer and use it in GitHub Desktop.
list all .bash_aliases pretty
#!/usr/bin/env node
// @ts-check
import { readFileSync } from "fs"
const aliases = readFileSync("/root/.bash_aliases", { encoding: "utf8" })
const dim = (/** @type string */ str) => `\x1b[2m${str}\x1b[0m`
const bright = (/** @type string */ str) => `\x1b[34m${str}\x1b[0m`
const lines = aliases.trim().split("\n")
/**
* @type Array<[string[], string]>
*/
const mapped = lines.map((a) => {
const removedAlias = a.replace("alias ", "")
const removedQuotes = removedAlias.replace(/\"|\'/g, "")
const [shorthand, longhand] = removedQuotes.split("=")
return [[shorthand], longhand]
})
const deduplicated = ((shortcuts) => {
/**
* @type Array<[string[], string]>
*/
const ret = []
for (const [[shorthand], longhand] of shortcuts) {
// check if the longhand already exists in the return object
const existingIndex = ret.findIndex(([shorthands]) => shorthands.includes(longhand))
// if yes: add it to the already exitsing shortcut
// if no: add it to the return object
if (existingIndex >= 0) {
ret[existingIndex][0].push(shorthand)
} else ret.push([[shorthand], longhand])
}
return ret
})(mapped)
const joinedShorthands = deduplicated.map(([shorthands, longhand]) => [
[...shorthands].reverse().join(", "),
longhand,
])
const joinedShortcut = joinedShorthands.map(
([shorthand, longhand]) => `${bright(shorthand)}\n ${dim(longhand)}`,
)
const joinedLines = joinedShortcut.join("\n")
console.log(joinedLines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment