Using jq with jrnl to make better output

in «tip» by Michael Beard
Tags: , , , , , , ,

Here is the json to text conversion, that was part of the view jrnl project, but I couldn't get working for some reason when I tried it.

Anyway, had some time today and decided that I should try it, as it would be very helpful to know how to do this. Not only for jrnl, but for other things as well.

Here is the script that I ended up modifying from the viewjrnl project (it does a text output):

def emph:
  if (env.SOLARIZED == "light")
  then "" + . + ""
  else "" + . + ""
  end;
def in_red:     "" + . + "";
def in_green:   "" + . + "";
def in_yellow:  "" + . + "";
def in_blue:    "" + . + "";
def in_magenta: "" + . + "";
def in_cyan:    "" + . + "";
#### Entries are stored in top level '.entries' key
.entries
#### Group by common date
| group_by(.date)[]
#### Remember the date for each group
#### (pluck index 0 because they're all the same within a group)
| {
    date: .[0].date,
    entries: .
  }
#### Pretty print
| (
    # Header for single day
    (
      (.date | strptime("%F") | strflocaltime("%a, %b %d") | in_blue)
      + "\n"
      + .date
      + " \n──────────────────────────────────────────────────────\n"
      | emph
    ),
    # Entries within a day
    (
      .entries[]
      | (.time | strptime("%R") | strflocaltime("%I:%M %p") | in_yellow )
        + ": "
        + (.title | in_cyan)
        + "\n"
        # Hacky solution to ensure there aren't too many newlines
        + if (.body | length) > 1
          then (.body | rtrimstr("\n") | gsub("\n(?<c>.)"; "\n\(.c)"))
          else ""
          end
    ),
    (
      "──────────────────────────────────────────────────────\n"
    )
  )

And, here is what I did with converting it to another Markdown view as well:

#### Entries are stored in top level '.entries' key
.entries
#### Group by common date
| group_by(.date)[]
#### Remember the date for each group
#### (pluck index 0 because they're all the same within a group)
| {
    date: .[0].date,
    entries: .
  }
#### Pretty print
| (
    # Header for single day
    (
      "# "
      + .date
      + " - "
      + (.date | strptime("%F") | strflocaltime("%a, %b %d"))
      + " \n_____\n"
    ),
    # Entries within a day
    (
      .entries[]
      | "## " 
        + (.time | strptime("%R") | strflocaltime("%I:%M %p"))
        + ": "
        + .title
        + "\n\n"
        # Hacky solution to ensure there aren't too many newlines
        + .body
        + "\n"
    ),
    (
      "_____ \n"
    )
  )

I could probably do another, if I wanted, for the rich framework format as well, but haven't done that yet.

Anyway, the scripts are both in the my_setup/bin/jq-scripts directory as the following names:

  • jq-2-md.jq
  • jq-2-text.jq

They should be called something like this:

jrnl -from 2020-08-01 --export json | jq -r -f jq-2-text.jq

jrnl -50 --export json | jq -r -f jq-2-text.jq 

jrnl -50 --export json | jq -r -f jq-2-text.jq | less -F -X

jrnl -on today --export json | jq -r -f jq-2-text.jq 

I'll probably get them incorporated into cmd-jrnl at some point.