Sha256: d2d71420cbaf7403a71e0f8f98ec1d1778d6f176d0e487512f27b0c8429a7b7e

Contents?: true

Size: 1.54 KB

Versions: 4

Compression:

Stored size: 1.54 KB

Contents

require 'json'
require 'html_to_ansi'

module Gamefic
  module Tty
    class User
      attr_reader :input
      attr_reader :output

      # @param input [IO] The stream that receives input
      # @param output [IO] The stream that sends output
      def initialize input: STDIN, output: STDOUT
        @input = input
        @output = output
      end

      # Update the user with a hash of data representing the current game state.
      #
      # @param data [Hash]
      # @return [void]
      def update state
        @output.write state_to_text(state)
      end

      def state_to_text state
        output = state[:messages].to_s
        unless state[:options].nil?
          list = '<ol class="multiple_choice">'
          state[:options].each { |o|
            list += "<li><a href=\"#\" rel=\"gamefic\" data-command=\"#{o}\">#{o}</a></li>"
          }
          list += "</ol>"
          output += list
        end
        HtmlToAnsi.convert output
      end

      # Get input from the user.
      #
      # @return [String, nil]
      def query prompt = '> '
        @output.print prompt
        @input.gets
      end

      # @todo Save and restore aren't ready yet

      # def save filename, snapshot
      #   File.open(filename, 'w') do |file|
      #     file << snapshot.to_json
      #   end
      # end

      # def restore filename
      #   json = File.read(filename)
      #   snapshot = JSON.parse(json, symbolize_names: true)
      #   engine.restore snapshot
      # end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gamefic-tty-2.0.3 lib/gamefic-tty/user.rb
gamefic-tty-2.0.2 lib/gamefic-tty/user.rb
gamefic-tty-2.0.1 lib/gamefic-tty/user.rb
gamefic-tty-2.0.0 lib/gamefic-tty/user.rb