File: io_context.rb

package info (click to toggle)
ruby-unicode-plot 0.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,492 kB
  • sloc: ruby: 2,871; makefile: 4
file content (32 lines) | stat: -rw-r--r-- 570 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
require "forwardable"

module UnicodePlot
  class IOContext
    extend Forwardable

    def initialize(io, color: :auto)
      @io = io
      @color = check_color(color)
    end

    def_delegators :@io, :print, :puts

    def color?
      case @color
      when :auto
        @io.respond_to?(:tty?) ? @io.tty? : false
      else
        @color
      end
    end

    private def check_color(color)
      case color
      when true, false, :auto
        color
      else
        raise ArgumentError, "color must be either true, false, :auto"
      end
    end
  end
end