Skip to content

Commit

Permalink
pushes several changes:
Browse files Browse the repository at this point in the history
* New DSL: center, block, code, section, image (the generic slide is gone).

* code accepts an extra argument to configure the language.

* Support for images (only works in iTerm2).

* New #i and #b ANSI helpers for italics and bold.
  • Loading branch information
fxn committed Jul 13, 2013
1 parent 7c8e44c commit 0450e64
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 75 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,32 @@ Terminal Keynote is text-based, but with style! Syntax highlighting is done on t

There are four types of slides:

### :code
### code

A slide with source code. Syntax highlighted on the fly. If you want to put a title or file name or something use source code comments and imagination.

![Terminal Keynote Code](https://raw.github.com/fxn/tkn/master/screenshots/terminal-keynote-code.png)

### :center
### center

A slide whose text is centered line by line.

![Terminal Keynote Center](https://raw.github.com/fxn/tkn/master/screenshots/terminal-keynote-center.png)

### :block
### block

A slide with text content whose formatting is preserved, but that is centered as a whole in the screen. Do that with CSS, ha!

I find centering content in the screen as a block to be more aesthetically pleasant that flushing against the left margin. There is no way to flush against a margin.

![Terminal Keynote Block](https://raw.github.com/fxn/tkn/master/screenshots/terminal-keynote-block.png)

### image

A slide with an image. (Only supported in [iTerm2](http://www.iterm2.com/)).

![Terminal Keynote Image](https://raw.github.com/fxn/tkn/master/screenshots/terminal-keynote-image.png)

### Sections

Sections have a title and draw kind of a fleuron. This is also hard-coded because it is what I wanted.
Expand Down Expand Up @@ -96,7 +102,7 @@ If Terminal Keynote evolves it is going to do so in backwards incompatible ways

### Requirements

Terminal Keynote needs [Ruby 1.9](http://www.ruby-lang.org) and [Pygments](http://pygments.org).
Terminal Keynote needs [Ruby 1.9 or 2.0](http://www.ruby-lang.org) and [Pygments](http://pygments.org).

Once those are installed run `gem install bundler`, root privs may be needed. Then cd into the directory with your `tkn` clone and run `bundle`, this installs the Ruby dependencies.

Expand Down
89 changes: 75 additions & 14 deletions bin/tkn
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,35 @@ require 'pygments'
# --- DSL -------------------------------------------------------------
#

def slide(content, format=:block)
$slides << [content.strip_heredoc, format]
def center(content)
slide(content, :center)
end

def section(content)
$slides << [content, :section]
def block(content)
slide(content, :block)
end

def code(content, lexer=:ruby)
slide(content, :code, lexer)
end

def image(image_path)
slide(image_path, :image)
end

def section(title)
slide(title, :section)
yield
end

def slide(content, format, *args)
$slides << {
content: content.strip_heredoc,
format: format,
args: args
}
end


#
# --- ANSI Escape Sequences -------------------------------------------
Expand All @@ -39,6 +59,14 @@ def cursor_at(row, col)
"\e[#{row};#{col}H"
end

def i(str)
"\e[3m#{str}\e[0m"
end

def b(str)
"\e[1m#{str}\e[0m"
end


#
# --- Utilities -------------------------------------------------------
Expand All @@ -65,6 +93,21 @@ def winsize
$stdout.winsize
end

# Sets an image as terminal background, assumes the presentation run in iTerm2.
def set_background_image(image_path)
system %(osascript -e 'tell application "iTerm" to set background image path of current session of current terminal to "#{image_path}"')
end

# Clears the background image.
def clear_background_image
set_background_image(nil)
end

# Is this slide an image? (procedural style).
def image?(slide)
slide[:format] == :image
end


#
# --- Slide Rendering -------------------------------------------------
Expand All @@ -73,7 +116,7 @@ end
# Returns a string that the caller has to print as is to get the slide
# properly rendered. The caller is responsible for clearing the screen.
def render(slide)
send("render_#{slide[1]}", slide[0]) if slide[0] =~ /\S/
send("render_#{slide[:format]}", slide[:content], *slide[:args])
end

# Renders the content by centering each individual line.
Expand All @@ -91,20 +134,20 @@ def render_center(content)
end

# Renders a section banner.
def render_section(content)
def render_section(title)
nrows, ncols = winsize
width = width(content)
width = width(title)

rfil = [1, width - 5].max/2
lfil = [1, width - 5 - rfil].max
fleuron = '─' * lfil + ' ❧❦☙ ' + '─' * rfil

render_center("#{fleuron}\n\n#{content}\n\n#{fleuron}\n")
render_center("#{fleuron}\n\n#{title}\n\n#{fleuron}\n")
end

# Renders Ruby source code.
def render_code(code)
render_block(Pygments.highlight(code, formatter: 'terminal256', lexer: 'ruby', options: {style: 'bw'}))
# Renders source code.
def render_code(code, lexer)
render_block(Pygments.highlight(code, formatter: 'terminal256', lexer: lexer, options: {style: 'bw'}))
end

# Centers the whole content as a block. That is, the format within the content
Expand All @@ -124,6 +167,11 @@ def render_block(content)
end
end

# Renders an image in the terminal by setting it as background (only works with iTerm2).
def render_image(image_path)
set_background_image(File.expand_path(image_path))
end


#
# --- Main Loop -------------------------------------------------------
Expand All @@ -147,8 +195,14 @@ end
n = 0
deck = ARGV[0]
mtime = nil
image = false

loop do
if image
clear_background_image
image = false
end

print clear_screen

current_mtime = File.mtime(deck)
Expand All @@ -159,9 +213,16 @@ loop do
end

n = [[0, n].max, $slides.length - 1].min
render($slides[n]).each_char do |c|
print c
sleep 0.002 # old-school touch: running cursor
slide = $slides[n]

if image?(slide)
render(slide)
image = true
else
render(slide).each_char do |c|
print c
sleep 0.002 # old-school touch: running cursor
end
end

case read_command
Expand Down
Loading

0 comments on commit 0450e64

Please sign in to comment.