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(input = STDIN, output = STDOUT) ⇒ Session

Returns a new instance of Session.



14
15
16
17
18
19
# File 'lib/shellplay/session.rb', line 14

def initialize(input = STDIN, output = STDOUT)
  @sequence = []
  @title = false
  @config = Shellplay::Config.new(nil, input, output)
  @pointer = 0
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#pointerObject (readonly)

Returns the value of attribute pointer.



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

def pointer
  @pointer
end

#sequenceObject (readonly)

Returns the value of attribute sequence.



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

def sequence
  @sequence
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#add_screen(screenhash) ⇒ Object



33
34
35
36
37
# File 'lib/shellplay/session.rb', line 33

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

#current_screenObject



57
58
59
# File 'lib/shellplay/session.rb', line 57

def current_screen
  @sequence[@pointer]
end

#drop_last_screenObject



39
40
41
42
# File 'lib/shellplay/session.rb', line 39

def drop_last_screen
  @sequence.pop
  previous
end

#import(name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/shellplay/session.rb', line 21

def import(name)
  name ||= ask "What session do you want to load?",
    aslist: true,
    choices: Dir.glob(File.join(@config.basedir, '*.json')).map { |f| File.basename(f, '.json') }
  infile = File.join(@config.basedir, "#{name}.json")
  data = JSON.parse(IO.read(infile))
  @title = data['title']
  data['sequence'].each do |screenhash|
    add_screen(screenhash)
  end
end

#nextObject



44
45
46
# File 'lib/shellplay/session.rb', line 44

def next
  @pointer += 1
end

#prepareObject



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

def prepare
  set_title
  set_name
end

#previousObject



48
49
50
# File 'lib/shellplay/session.rb', line 48

def previous
  @pointer -= 1
end

#saveObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/shellplay/session.rb', line 61

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



52
53
54
55
# File 'lib/shellplay/session.rb', line 52

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