Pragmatic programmer console tips

June 26, 2020

Preview of pragmatic console tips

Install the following software: jq.
Download pragmatic-tips.json and place it somewhere your console can read it.

Place the following code in /etc/profile.d/pragmatic-tips.sh:

#!/bin/bash

function _pragmatic_tip() {
  local TIPS="/c/Dev/Tools/pragmatic-tips.json"

  if [ -f "$TIPS" ] && [ "$(command -v jq)" != "" ]; then
    local TIP_SIZE=100
    local INDEX=$(( RANDOM % TIP_SIZE ))
    local TIP=$(jq -c ".[$INDEX]" "$TIPS")

    local INDENT="  "
    local NUMBER=$(echo $TIP | jq -r '.number')
    local TITLE=$(echo $TIP | jq -r '.title')
    local TEXT=$(echo $TIP | jq -r '.text' | fold -w 50 --spaces | sed "s/^/$INDENT/g")

    printf "$INDENT\e[90m$NUMBER$INDENT\e[38;5;213m$TITLE\n\e[38;5;198m$TEXT\e[39m"
  fi
}

_pragmatic_tip

Scraping the tips

I used the following script on https://pragprog.com/tips/ website:

x = [...document.querySelector('.tpp_tips_container').children].map(child =>
    ({
        number: child.querySelector(".tip-number").innerText,
        title: child.querySelector(".tip-title").innerText,
        text: child.querySelector(".tip-body").innerText
    }));

JSON.stringify(x, null, 4);

Then saved that json to a file.