Sha256: 8d972c60a0962bd52abef5226602b96f859f53135a3190ebb15841415c86f254

Contents?: true

Size: 831 Bytes

Versions: 7

Compression:

Stored size: 831 Bytes

Contents

require "json"
require "tty-prompt"
require "tty-markdown"

module Matheus
  # Usage:
  #    $ qs
  #    Lists the questions asked and their answers.
  class Qs < Command
    def call(_)
      return puts "No questions found in history." if history.empty?

      answer = prompt.select("Question:", choices, per_page: 10)
      print_markdown(answer)
    rescue => e
      Failure(e.message)
    end

    private

    def choices
      history.map do |entry|
        {entry["question"] => entry["answer"]}
      end
    end

    def history
      @history ||= File.exist?(QUESTION_HISTORY_FILE) ? JSON.parse(File.read(QUESTION_HISTORY_FILE)) : []
    end

    def prompt
      @prompt ||= TTY::Prompt.new(interrupt: :exit)
    end

    def print_markdown(answer)
      puts
      puts TTY::Markdown.parse(answer)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
matheus-0.7.1 lib/matheus/qs.rb
matheus-0.7.0 lib/matheus/qs.rb
matheus-0.6.4 lib/matheus/qs.rb
matheus-0.6.3 lib/matheus/qs.rb
matheus-0.6.2 lib/matheus/qs.rb
matheus-0.6.1 lib/matheus/qs.rb
matheus-0.6.0 lib/matheus/qs.rb