Here is a fairly simple ASCII depiction of an open book:
|\
| \
| \
| \
| \__________
| || |
| || Lorem i |
\ || psum do |
\ || lor sit |
\ || amet, |
\ || consect |
\||_________|
Notice that the text portion is only on the right page and is 7 characters wide by 5 tall. Also notice that the top edge of the book has 5 backslashes and 10 underscores. The 10 comes from the text width plus 3 and the 5 is half of 10.
Using the same scaling format we can resize the book to have a text area w characters wide and h high, where w is any odd positive integer and h is any positive integer.
Some w×h books: 1×1, 1×2, 3×2
|\
|\ | \
|\ | \____ | \______
| \____ | || | | || |
| || | | || L | \ || Lor |
\ || L | \ || o | \ || em |
\||___| \||___| \||_____|
The number of underscores at the top is always w+3 and the number of backslashes is always (w+3)/2.
Goal
Write a progam that takes a filename and w and h as command line arguments, and outputs a book with those text dimensions to stdout, displaying the contents of the file.
When the file has more text than will fit in one page the N
key should print the next page and B
should go back a page. Nothing should happen if B
is pressed from the first page or N
is pressed from the last page. The program should stop when the Q
key is hit.
Example
Suppose f.txt
contains Lorem ipsum dol?
and the user has pressed the key sequence N N B N N Q
. The program should run something like this:
>>> bookmaker f.txt 3 2
|\
| \
| \______
| || |
\ || Lor |
\ || em |
\||_____|
|\
| \
| \______
| || |
\ || ips |
\ || um |
\||_____|
|\
| \
| \______
| || |
\ || dol |
\ || ? |
\||_____|
|\
| \
| \______
| || |
\ || ips |
\ || um |
\||_____|
|\
| \
| \______
| || |
\ || dol |
\ || ? |
\||_____|
>>>
Notice that there is a newline after every book and no trailing spaces. This is required.
Notes
- You may assume the file only contains printable ASCII characters (hex 20 to 7E).
- Print one character in each available spot, regardless of word boundaries.
- w and h are optional arguments that default to 7 and 5 respectively. Your program will either be given neither or both. (You may assume input is always well formed.)
- Fill any empty text space on the last page with spaces.
Q
should still be required to quit if there is only one page.
Winning
The shortest program in bytes after bonuses are applied wins.
Bonuses
- Remove leading spaces so that every line starts with a word (or word segment). e.g.
| amet, |
in the first example would become| amet, c |
. (-30 bytes) - Clear the screen of previously drawn books after
N
orB
is pressed (andT
if you do the bonus after this) so the book looks like its changing pages in place. (-20 bytes) Make the
T
key instantly toggle between the text being drawn from left-to-right top-to-bottom (the default), to top-to-bottom left-to-right. If you did the first bonus it should work for columns in the top-to-bottom mode. (-100 bytes)So for example:
|\ | \ | \______ | || | \ || Lor | \ || em | \||_____|
becomes
|\ | \ | \______ | || | \ || Lrm | \ || oe | \||_____|
\033[2J\033[;H
\$\endgroup\$