Class: Gemwarrior::Repl

Inherits:
Object
  • Object
show all
Defined in:
lib/gemwarrior/repl.rb

Constant Summary collapse

SCREEN_WIDTH_MIN =

CONSTANTS

80
SCREEN_WIDTH_MAX =
120
QUIT_MESSAGE =
'Temporal flux detected. Shutting down...'.colorize(:red)
'Giving up so soon? Jool will be waiting...'.colorize(:red)
SPLASH_MESSAGE =
'Welcome to *Jool*, where randomized fortune is just as likely as mayhem.'
GITHUB_NAME =
'michaelchadwick'
GITHUB_PROJECT =
'gemwarrior'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game, world, evaluator) ⇒ Repl

Returns a new instance of Repl.



31
32
33
34
35
36
37
# File 'lib/gemwarrior/repl.rb', line 31

def initialize(game, world, evaluator)
  self.game         = game
  self.world        = world
  self.evaluator    = evaluator

  GameOptions.data['wrap_width'] = get_screen_width
end

Instance Attribute Details

#evaluatorObject

Returns the value of attribute evaluator.



29
30
31
# File 'lib/gemwarrior/repl.rb', line 29

def evaluator
  @evaluator
end

#gameObject

Returns the value of attribute game.



29
30
31
# File 'lib/gemwarrior/repl.rb', line 29

def game
  @game
end

#worldObject

Returns the value of attribute world.



29
30
31
# File 'lib/gemwarrior/repl.rb', line 29

def world
  @world
end

Instance Method Details

#get_screen_widthObject



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/gemwarrior/repl.rb', line 39

def get_screen_width
  screen_width = SCREEN_WIDTH_MIN

  begin
    require 'io/console'
    screen_width = IO.console.winsize[1]
  rescue
    if command_exists?('tput')
      screen_width = `tput cols`.to_i
    elsif command_exists?('stty')
      screen_width = `stty size`.split.last.to_i
    elsif command_exists?('mode')
      mode_output = `mode`.split
      screen_width = mode_output[mode_output.index('Columns:')+1].to_i
    end
  end

  case
  when screen_width.nil?, screen_width <= 0
    return SCREEN_WIDTH_MIN
  else
    return [screen_width, SCREEN_WIDTH_MAX].min
  end
end

#main_loop(ext_input = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/gemwarrior/repl.rb', line 91

def main_loop(ext_input = nil)
  input = ext_input.nil? ? read_line : ext_input
  result = evaluator.parse(input)
  if result.eql?('exit')
    exit
  elsif result.eql?('checkupdate')
    check_for_new_release
  else
    puts result
  end
end

#start(initial_command, extra_command, new_skip, resume_skip) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gemwarrior/repl.rb', line 64

def start(initial_command, extra_command, new_skip, resume_skip)
  setup_screen(initial_command, extra_command, new_skip, resume_skip)

  clocker = Clocker.new

  at_exit do
    update_duration(clocker.stop)
    game.update_options_file
    log_stats(world.duration, world.player)
    save_game(world)
  end

  clocker.clock do
    # main loop
    loop do
      prompt
      begin
        main_loop
      rescue Interrupt
        puts
        puts QUIT_MESSAGE
        exit
      end
    end
  end
end