Skip to content

Instantly share code, notes, and snippets.

@rhowardiv
Created October 9, 2012 19:23
Show Gist options
  • Save rhowardiv/3860861 to your computer and use it in GitHub Desktop.
Save rhowardiv/3860861 to your computer and use it in GitHub Desktop.
powerpoint in bash
#!/bin/bash
usage() {
echo "Interactively show slides from a file"
echo
echo "Usage:"
echo "$0 FILE LINES"
echo
echo "FILE is the file containing the slides."
echo "In the file, each slide is indicated with a simple 'slide' by itself"
echo "at the start of a line."
echo "LINES is the desired height; \$LINES is generally what you want to"
echo "provide here."
echo
echo "Control:"
echo "Pressing k or h will move backwards one slide."
echo "Pressing anything else will move ahead one slide."
}
if [ $# -lt 2 ]; then
usage
exit 1
fi
UNPARSED="$(cat $1)"
REMOVE_LINES=$(echo "$UNPARSED" | sed '/^slide$/q' | wc -l)
if [ "$REMOVE_LINES" -eq 0 ]; then
echo "All slides including the first slide must start with 'slide' on a line by itself."
fi
UNPARSED="$(echo "$UNPARSED" | sed -n $((1 + $REMOVE_LINES)),\$p)"
I=0
while [ -n "$UNPARSED" ]; do
I=$((I + 1))
SLIDE[$I]="$(echo "$UNPARSED" | sed '/^slide$/q' | sed \$d; echo x)"
# bash gotcha: command expansion removes trailing newlines
# we need them here for counting purposes
# so throw an "x" on the end and take it off after expansion's done
SLIDE[$I]="${SLIDE[$I]%x}"
REMOVE_LINES=$(echo "${SLIDE[$I]}" | wc -l)
UNPARSED="$(echo "$UNPARSED" | sed 1,$(($REMOVE_LINES))d)"
done
echo "Loaded ${#SLIDE[@]} slides"
SLIDE_IX=0
while read -s -N 1; do
case "$REPLY" in
[hk])
GO=-1
;;
*)
GO=1
;;
esac
SLIDE_IX="$(($SLIDE_IX + $GO))"
if [ "$SLIDE_IX" -gt "${#SLIDE[@]}" ]; then
SLIDE_IX="${#SLIDE[@]}"
elif [ "$SLIDE_IX" -lt 1 ]; then
SLIDE_IX=1
fi
echo
echo -n "${SLIDE[$SLIDE_IX]}"
SLIDE_HEIGHT="$(echo "${SLIDE[$SLIDE_IX]}" | wc -l)"
I="$SLIDE_HEIGHT"
while [ "$I" -lt $2 ]; do
echo
I="$(($I + 1))"
done
done
@flores
Copy link

flores commented Feb 12, 2013

super cool.

@ryanuber
Copy link

ryanuber commented Mar 6, 2013

This is a good idea. I did something similar recently, the implementation is different but the general idea is the same.

https://github.com/ryanuber/slide.sh

@rhowardiv
Copy link
Author

@ryanuber Wow your version is shorter, cleaner and far more capable. I'm definitely using yours in the future!

@azriel
Copy link

azriel commented Mar 6, 2020

Hi @rhowardiv, you're probably not interested in supporting a script you wrote 8 years ago, but it perfectly fits my needs for a project and I am trying to get it working on mac. The error I get is ./ppt.sh: line 47: read: -N: invalid option – Any ideas?

Thanks so much

@azriel
Copy link

azriel commented Mar 6, 2020

To clarify, having slides be lines of a single text file is very attractive to me and other solutions have not had that feature

@rhowardiv
Copy link
Author

@azriel I'd love to help! This is what i see in man bash on linux
for read -N:

-N nchars
        read returns after reading exactly nchars characters rather than waiting for a complete line of input, unless EOF is encountered or read times out.  Delimiter characters encountered in the input are not treated specially and do not cause read  to  return
        until nchars characters are read.

If your version of bash is missing -N, maybe it has -n which seems more common in the wild and looks like it should also work fine?

@azriel
Copy link

azriel commented Mar 6, 2020

@rhowardiv Thanks! Looks like that works great. I'm going to play around with it and I'll let you know if I have any more questions. Thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment