Class: Shellplay::Session

Inherits:
Object
  • Object
show all
Includes:
Cliprompt
Defined in:
lib/shellplay/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(basedir = nil, basefile = nil, input = STDIN, output = STDOUT) ⇒ Session

Returns a new instance of Session.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/shellplay/session.rb', line 15

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')
  @basefile = basefile || 'config.yml'
  @input = input
  @output = output
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/shellplay/session.rb', line 13

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/shellplay/session.rb', line 13

def name
  @name
end

#pointerObject (readonly)

Returns the value of attribute pointer.



13
14
15
# File 'lib/shellplay/session.rb', line 13

def pointer
  @pointer
end

#promptObject (readonly)

Returns the value of attribute prompt.



13
14
15
# File 'lib/shellplay/session.rb', line 13

def prompt
  @prompt
end

#sequenceObject (readonly)

Returns the value of attribute sequence.



13
14
15
# File 'lib/shellplay/session.rb', line 13

def sequence
  @sequence
end

#timeformatObject (readonly)

Returns the value of attribute timeformat.



13
14
15
# File 'lib/shellplay/session.rb', line 13

def timeformat
  @timeformat
end

#titleObject (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



61
62
63
64
65
# File 'lib/shellplay/session.rb', line 61

def add_screen(screenhash)
  s = Shellplay::Screen.new
  s.import(screenhash)
  @sequence << s
end

#add_screens(screenarray) ⇒ Object



67
68
69
# File 'lib/shellplay/session.rb', line 67

def add_screens(screenarray)
  @sequence += screenarray
end

#current_screenObject



89
90
91
# File 'lib/shellplay/session.rb', line 89

def current_screen
  @sequence[@pointer]
end

#drop_last_screenObject



71
72
73
74
# File 'lib/shellplay/session.rb', line 71

def drop_last_screen
  @sequence.pop
  previous
end

#import(name) ⇒ Object



28
29
30
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
# File 'lib/shellplay/session.rb', line 28

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

#nextObject



76
77
78
# File 'lib/shellplay/session.rb', line 76

def next
  @pointer += 1
end

#prepareObject



104
105
106
107
# File 'lib/shellplay/session.rb', line 104

def prepare
  set_title
  set_name
end

#previousObject



80
81
82
# File 'lib/shellplay/session.rb', line 80

def previous
  @pointer -= 1
end

#saveObject



93
94
95
96
97
98
99
100
101
102
# File 'lib/shellplay/session.rb', line 93

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



84
85
86
87
# File 'lib/shellplay/session.rb', line 84

def show(index)
  @pointer = index.to_i
  current_screen
end