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.
-
#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
- #add_screens(screenarray) ⇒ Object
- #current_screen ⇒ Object
- #drop_last_screen ⇒ Object
- #import(name) ⇒ Object
-
#initialize(input = STDIN, output = STDOUT) ⇒ Session
constructor
A new instance of Session.
- #next ⇒ Object
- #prepare ⇒ Object
- #previous ⇒ Object
- #save ⇒ Object
- #show(index) ⇒ Object
Constructor Details
#initialize(input = STDIN, output = STDOUT) ⇒ Session
Returns a new instance of Session.
15 16 17 18 19 20 21 22 |
# File 'lib/shellplay/session.rb', line 15 def initialize(input = STDIN, output = STDOUT) @sequence = [] @title = false @prompt = false @timeformat = false @config = Shellplay::Config.new(nil, input, output) @pointer = 0 end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
13 14 15 |
# File 'lib/shellplay/session.rb', line 13 def config @config end |
#pointer ⇒ Object (readonly)
Returns the value of attribute pointer.
13 14 15 |
# File 'lib/shellplay/session.rb', line 13 def pointer @pointer end |
#prompt ⇒ Object (readonly)
Returns the value of attribute prompt.
13 14 15 |
# File 'lib/shellplay/session.rb', line 13 def prompt @prompt end |
#sequence ⇒ Object (readonly)
Returns the value of attribute sequence.
13 14 15 |
# File 'lib/shellplay/session.rb', line 13 def sequence @sequence end |
#timeformat ⇒ Object (readonly)
Returns the value of attribute timeformat.
13 14 15 |
# File 'lib/shellplay/session.rb', line 13 def timeformat @timeformat end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
13 14 15 |
# File 'lib/shellplay/session.rb', line 13 def title @title end |
Instance Method Details
#add_screen(screenhash) ⇒ Object
52 53 54 55 56 |
# File 'lib/shellplay/session.rb', line 52 def add_screen(screenhash) s = Shellplay::Screen.new s.import(screenhash) @sequence << s end |
#add_screens(screenarray) ⇒ Object
58 59 60 |
# File 'lib/shellplay/session.rb', line 58 def add_screens(screenarray) @sequence += screenarray end |
#current_screen ⇒ Object
80 81 82 |
# File 'lib/shellplay/session.rb', line 80 def current_screen @sequence[@pointer] end |
#drop_last_screen ⇒ Object
62 63 64 65 |
# File 'lib/shellplay/session.rb', line 62 def drop_last_screen @sequence.pop previous end |
#import(name) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/shellplay/session.rb', line 24 def import(name) unless name sessions = Dir.glob(File.join(@config.basedir, '*.json')) if sessions.count == 0 puts "There is no recorded session locally." puts "Do you want to play a remote recording?" name = ask "url: " else 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 } else infile = IO.read(File.join(@config.basedir, "#{name}.json")) end data = JSON.parse(infile) @title = data['title'] @prompt = data['prompt'] @timeformat = data['timeformat'] data['sequence'].each do |screenhash| add_screen(screenhash) end end |
#next ⇒ Object
67 68 69 |
# File 'lib/shellplay/session.rb', line 67 def next @pointer += 1 end |
#prepare ⇒ Object
95 96 97 98 |
# File 'lib/shellplay/session.rb', line 95 def prepare set_title set_name end |
#previous ⇒ Object
71 72 73 |
# File 'lib/shellplay/session.rb', line 71 def previous @pointer -= 1 end |
#save ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/shellplay/session.rb', line 84 def save prepare h = {} h[:title] = @title h[:sequence] = @sequence.map(&:export) outfile = File.join(@config.basedir, "#{@name}.json") File.open(outfile, 'w') do |f| f.write JSON.pretty_generate(h) end end |
#show(index) ⇒ Object
75 76 77 78 |
# File 'lib/shellplay/session.rb', line 75 def show(index) @pointer = index.to_i current_screen end |