#!/usr/bin/env python

# Small presentation tool
#
# Copyright (c) 2011, Pierre Carrier <perso@gcarrier.fr>
# Permission to use, copy, modify, and/or distribute this software for any purpose
# with or without fee is hereby granted, provided that the above copyright notice
# and this permission notice appear in all copies.
#
# Example: slider < slides
#
# slides contains slides separated by a "%" line, and finishes with a "%" line.
#
# Yes, this is a crappy script.
# Quickly hacked for my personal use, needs some serious cleanup.

from sys import exit, argv, stdin, stdout
from curses import setupterm, tigetstr

file = open(argv[1])
slides, current_slide = [], ""
for line in file.xreadlines():
    if line.strip() == "%":
        slides.append(current_slide)
        current_slide = ""
    else:
        current_slide += line

def display_slide(slide):
    print tigetstr("clear"),
    print(slide)

# backup screen
setupterm()
print tigetstr("smcup"),
stdout.flush()

index = 0
while True:
    display_slide(slides[index])
    print "        ["+str(index+1)+"/"+str(len(slides))+" (#pnq)]"
    command = stdin.readline().strip()
    if command == "n":     index += 1
    elif command == "p": index -= 1
    elif command == "q": exit(0)
    else: index = int(command)-1
    index %= len(slides)

# restore screen
print tigetstr("mmcup"),
stdout.flush()
