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.



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

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.



27
28
29
# File 'lib/gemwarrior/repl.rb', line 27

def evaluator
  @evaluator
end

#gameObject

Returns the value of attribute game.



27
28
29
# File 'lib/gemwarrior/repl.rb', line 27

def game
  @game
end

#worldObject

Returns the value of attribute world.



27
28
29
# File 'lib/gemwarrior/repl.rb', line 27

def world
  @world
end

Instance Method Details

#get_screen_widthObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gemwarrior/repl.rb', line 37

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

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



62
63
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
90
91
92
93
94
95
# File 'lib/gemwarrior/repl.rb', line 62

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
        input = read_line
        result = evaluator.parse(input)
        if result.eql?('exit')
          exit
        elsif result.eql?('checkupdate')
          check_for_new_release
        else
          puts result
        end
      rescue Interrupt
        puts
        puts QUIT_MESSAGE
        exit
      end
    end
  end
end