Sha256: 8fb79fdb9b4fcb780cc3bca268ccf73c8294bd2b873b45a9a29a86cea37edba2

Contents?: true

Size: 878 Bytes

Versions: 2

Compression:

Stored size: 878 Bytes

Contents

# frozen_string_literal: true

require "tty-prompt"

module Vercon
  class Stdout
    def initialize
      @stdout = $stdout
      @prompt = TTY::Prompt.new
      @lines = 0
    end

    def write(message)
      @stdout.puts(message)
      @lines += message.count("\n") + 1
    end

    def erase(lines: nil)
      (lines || @lines).times do
        @stdout.print("\e[A\e[K")
        @lines -= 1
      end
    end

    %i[ask yes? no? say ok warn error mask select].each do |method|
      define_method(method) do |*args, **params, &block|
        @lines += args.first.count("\n") + 1
        @prompt.send(method, *args, **params, &block)
      end
    end

    %i[puts print].each do |method|
      define_method(method) do |*args, **params, &block|
        @lines += args.first.count("\n") + 1
        @stdout.send(method, *args, **params, &block)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vercon-0.0.3 lib/vercon/stdout.rb
vercon-0.0.2 lib/vercon/stdout.rb