class Shellplay::Session

Attributes

config[R]
name[R]
pointer[R]
prompt[R]
sequence[R]
timeformat[R]
title[R]

Public Class Methods

new(basedir = nil, basefile = nil, input = STDIN, output = STDOUT) click to toggle source
# File lib/shellplay/session.rb, line 16
def initialize(basedir = nil, basefile = nil, input = STDIN, output = STDOUT)
  @sequence = []
  @name = false
  @title = false
  @prompt = false
  @timeformat = false
  @pointer = 0
  @basedir = basedir || File.join(ENV['HOME'], '.shellplay')
  FileUtils.mkdir_p(@basedir) unless Dir.exist? @basedir
  @basefile = basefile || 'config.yml'
  @input = input
  @output = output
end

Public Instance Methods

add_screen(screenhash) click to toggle source

appends a screen to the main sequence

# File lib/shellplay/session.rb, line 65
def add_screen(screenhash)
  s = Shellplay::Screen.new
  s.import(screenhash)
  @sequence << s
end
add_screens(screenarray) click to toggle source

appends an array of screens to the sequence

# File lib/shellplay/session.rb, line 72
def add_screens(screenarray)
  @sequence += screenarray
end
current_screen() click to toggle source

returns the screen object at the current point in the sequence

# File lib/shellplay/session.rb, line 99
def current_screen
  @sequence[@pointer]
end
drop_last_screen() click to toggle source

used for cancelling a screen while recording

# File lib/shellplay/session.rb, line 77
def drop_last_screen
  @sequence.pop
  previous
end
import(name) click to toggle source

import a json file from local drive or http location

# File lib/shellplay/session.rb, line 31
def import(name)
  unless name
    sessions = Dir.glob(File.join(@basedir, '*.json'))
    if sessions.count == 0
      @output.puts "There is no recorded session locally."
      @output.puts "Do you want to play a remote recording?"
      name = ask "url: "
    else
      @output.puts "What session do you want to load?"
      name = ask "(input a number or an url if you want to play a remote recording)",
        aslist: true,
        choices: sessions.map { |f| File.basename(f, '.json') }
    end
  end
  if /^https?:\/\//.match name
    infile = open(name) { |f| f.read }
    @name = File.basename(name, '.json')
  else
    infile = IO.read(File.join(@basedir, "#{name}.json"))
    @name = name
  end
  data = JSON.parse(infile)
  @title = data['title']
  @config = Shellplay::Config.new({
    basedir: @basedir,
    basefile: File.join(@basedir, @basefile),
    prompt: data['prompt'],
    timeformat: data['timeformat'] }, input, output)
  data['sequence'].each do |screenhash|
    add_screen(screenhash)
  end
end
next() click to toggle source

jump to next screen

# File lib/shellplay/session.rb, line 83
def next
  @pointer += 1
end
prepare() click to toggle source

initialize the sequence meta-data

# File lib/shellplay/session.rb, line 116
def prepare
  set_title
  set_name
end
previous() click to toggle source

jump to previous screen

# File lib/shellplay/session.rb, line 88
def previous
  @pointer -= 1
end
save() click to toggle source

saves the json file for the sequence

# File lib/shellplay/session.rb, line 104
def save
  prepare
  h = {}
  h[:title] = @title
  h[:sequence] = @sequence.map(&:export)
  outfile = File.join(@basedir, "#{@name}.json")
  File.open(outfile, 'w') do |f|
    f.write JSON.pretty_generate(h)
  end
end
show(index) click to toggle source

jump to an arbitrary screen

# File lib/shellplay/session.rb, line 93
def show(index)
  @pointer = index.to_i
  current_screen
end

Private Instance Methods

set_name() click to toggle source

uses cliprompt to ask for a name if not set already name is the name of the file on the disk

# File lib/shellplay/session.rb, line 130
def set_name
  @name ||= ask("What is the name of the session file?", "untitled")
end
set_title() click to toggle source

uses cliprompt to ask for a title if not set already

# File lib/shellplay/session.rb, line 124
def set_title
  @title ||= ask("What is the title of this session?", "No Title")
end