File: mode.rb

package info (click to toggle)
ruby-tty-reader 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 772 kB
  • sloc: ruby: 1,759; sh: 4; makefile: 4
file content (42 lines) | stat: -rw-r--r-- 764 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
33
34
35
36
37
38
39
40
41
42
# frozen_string_literal: true

require "io/console"

module TTY
  class Reader
    class Mode
      # Initialize a Terminal
      #
      # @api public
      def initialize(input = $stdin)
        @input = input
      end

      # Echo given block
      #
      # @param [Boolean] is_on
      #
      # @api public
      def echo(is_on = true, &block)
        if is_on || !@input.tty?
          yield
        else
          @input.noecho(&block)
        end
      end

      # Use raw mode in the given block
      #
      # @param [Boolean] is_on
      #
      # @api public
      def raw(is_on = true, &block)
        if is_on && @input.tty?
          @input.raw(&block)
        else
          yield
        end
      end
    end # Mode
  end # Reader
end # TTY