Class: Shellplay::Session
- Inherits:
-
Object
- Object
- Shellplay::Session
- Includes:
- Cliprompt
- Defined in:
- lib/shellplay/session.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#pointer ⇒ Object
readonly
Returns the value of attribute pointer.
-
#prompt ⇒ Object
readonly
Returns the value of attribute prompt.
-
#sequence ⇒ Object
readonly
Returns the value of attribute sequence.
-
#timeformat ⇒ Object
readonly
Returns the value of attribute timeformat.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
-
#add_screen(screenhash) ⇒ Object
appends a screen to the main sequence.
-
#add_screens(screenarray) ⇒ Object
appends an array of screens to the sequence.
-
#current_screen ⇒ Object
returns the screen object at the current point in the sequence.
-
#drop_last_screen ⇒ Object
used for cancelling a screen while recording.
-
#import(name) ⇒ Object
import a json file from local drive or http location.
-
#initialize(basedir = nil, basefile = nil, input = STDIN, output = STDOUT) ⇒ Session
constructor
A new instance of Session.
-
#next ⇒ Object
jump to next screen.
-
#prepare ⇒ Object
initialize the sequence meta-data.
-
#previous ⇒ Object
jump to previous screen.
-
#save ⇒ Object
saves the json file for the sequence.
-
#show(index) ⇒ Object
jump to an arbitrary screen.
Constructor Details
#initialize(basedir = nil, basefile = nil, input = STDIN, output = STDOUT) ⇒ Session
Returns a new instance of Session.
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# 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 |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
14 15 16 |
# File 'lib/shellplay/session.rb', line 14 def config @config end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/shellplay/session.rb', line 14 def name @name end |
#pointer ⇒ Object (readonly)
Returns the value of attribute pointer.
14 15 16 |
# File 'lib/shellplay/session.rb', line 14 def pointer @pointer end |
#prompt ⇒ Object (readonly)
Returns the value of attribute prompt.
14 15 16 |
# File 'lib/shellplay/session.rb', line 14 def prompt @prompt end |
#sequence ⇒ Object (readonly)
Returns the value of attribute sequence.
14 15 16 |
# File 'lib/shellplay/session.rb', line 14 def sequence @sequence end |
#timeformat ⇒ Object (readonly)
Returns the value of attribute timeformat.
14 15 16 |
# File 'lib/shellplay/session.rb', line 14 def timeformat @timeformat end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
14 15 16 |
# File 'lib/shellplay/session.rb', line 14 def title @title end |
Instance Method Details
#add_screen(screenhash) ⇒ Object
appends a screen to the main sequence
65 66 67 68 69 |
# File 'lib/shellplay/session.rb', line 65 def add_screen(screenhash) s = Shellplay::Screen.new s.import(screenhash) @sequence << s end |
#add_screens(screenarray) ⇒ Object
appends an array of screens to the sequence
72 73 74 |
# File 'lib/shellplay/session.rb', line 72 def add_screens(screenarray) @sequence += screenarray end |
#current_screen ⇒ Object
returns the screen object at the current point in the sequence
99 100 101 |
# File 'lib/shellplay/session.rb', line 99 def current_screen @sequence[@pointer] end |
#drop_last_screen ⇒ Object
used for cancelling a screen while recording
77 78 79 80 |
# File 'lib/shellplay/session.rb', line 77 def drop_last_screen @sequence.pop previous end |
#import(name) ⇒ Object
import a json file from local drive or http location
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# 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 ⇒ Object
jump to next screen
83 84 85 |
# File 'lib/shellplay/session.rb', line 83 def next @pointer += 1 end |
#prepare ⇒ Object
initialize the sequence meta-data
116 117 118 119 |
# File 'lib/shellplay/session.rb', line 116 def prepare set_title set_name end |
#previous ⇒ Object
jump to previous screen
88 89 90 |
# File 'lib/shellplay/session.rb', line 88 def previous @pointer -= 1 end |
#save ⇒ Object
saves the json file for the sequence
104 105 106 107 108 109 110 111 112 113 |
# 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) ⇒ Object
jump to an arbitrary screen
93 94 95 96 |
# File 'lib/shellplay/session.rb', line 93 def show(index) @pointer = index.to_i current_screen end |