🏡 index.nim

nimp5 documentation

  1. Get started: This program creates a canvas that is 400 pixels wide and 400 pixels high, and then starts drawing white circles at the position of the mouse. When a mouse button is pressed, the circle color changes to black.

  2. Flashing canvas: A canvas that flashes random colors (shows how to change frameRate)

  3. Easing: Move the mouse across the screen and the symbol will follow.

  4. Keyboard: Click on the image to give it focus and press the letter keys to create forms in time and space. Each key has a unique identifying number. These numbers can be used to position shapes in space.

  5. Polygons: What is your favorite? Pentagon? Hexagon? Heptagon? No? What about the icosagon? The polygon() function created for this example is capable of drawing any regular polygon. Also shows possible inputs for colors.

  6. Okazz 220919a: Art by Okazz, original at openprocessing.org/sketch/1653811.

  7. Okazz 221026a: Art by Okazz, original at openprocessing.org/sketch/1653811.

  8. Sine Wave: Render a simple sine wave. Original by Daniel Shiffman https://p5js.org/examples/math-sine-wave.html

  9. Instance mode: This program creates a canvas in a local p5 instance using instance mode.

  10. Doorbell: How to load and play a sound (using p5sound). Adapted from p5.SoundFile reference.

import std / [os, algorithm, sugar, strformat, strutils, tables]
import nimib except toJson  # we need to remove this from nimib exports! it breaks jsony!
import jsony

type
  Entry* = object
    filename: string
    title: string
    description: string
    numbering: int
  Index* = object
    data: seq[Entry]

proc sort*(idx: var Index) =
  idx.data.sort do (e, f: Entry) -> int:
    cmp(e.numbering, f.numbering)

proc dump*(idx: Index) =
  var idx = idx
  sort(idx)
  writeFile("index.json", jsony.toJson(idx))

proc loadIndex*: Index =
  if fileExists("index.json"):
    result = "index.json".readFile.fromJson(Index)
  else:
    result = Index()
  sort result

# requires nb: NbDoc
proc addEntry*(nb: var NbDoc, numb: int, titl, desc: string) =
  let e = Entry(numbering: numb, title: titl, description: desc, filename: nb.filename)
  var idx = loadIndex()
  var found = false
  for f in idx.data.mitems:
    if f.filename == e.filename:
      f = e
      found = true
  if not found:
    idx.data.add e
  dump idx
  let text = "## " &  $(e.numbering) & ". " & e.title & "\n" & e.description
  nbText: text

when isMainModule:
  nbInit
  nbText: "## nimp5 documentation"
  let idx = loadIndex()
  var
    listEntries = ""
  for entry in idx.data:
    listEntries.add fmt"{entry.numbering}. [{entry.title}]({entry.filename}): {entry.description}" & "\n"
  nbText: listEntries
  nbSave