thomastay / fortune

Nim implementation of the BSD fortune, with chunking to reduce load time

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Zig Fortune

A version of fortune built with Zig, and thus runs on Windows. Fortune is a BSD program which prints witty messages to the console.

~/fortune> fortune 100
Computers can figure out all kinds of problems, except the things in
the world that just don't add up.

Features

  • Statically linked executable with small footprint
  • Takes an input an integer to print a specific fortune. No in-built randomness. For a good source of randomness, you should use Powershell's in built Get-Random.
  • Fast, under 20ms to print

Requirements

You will need the Zig compiler, which you can download from their website.

Building

To create, run

zig build

This will create an executable called fortune.exe, or fortune depending on your OS. This executable has the fortunes location built in, so you can move it around as you wish. However, the autogenerated fortunes/ folder cannot be moved around.

Supplying your own fortune file

Simply replace fortune.txt with your own file. The format of a fortune looks like this:

fortune1 CRLF
% CRLF
fortune2

How it works

The fortune.txt file is preprocessed to create a file called "fortunes-chunked.txt". This consists of fortunes which have been aligned on page boundaries. Then, we use the _lseek call to quickly load the page we want, find the fortune, and print it to the screen.

About

Nim implementation of the BSD fortune, with chunking to reduce load time

License:BSD 2-Clause "Simplified" License


Languages

Language:Zig 64.8%Language:Nim 35.2%