File: multiline.rb

package info (click to toggle)
ruby-tty-prompt 0.23.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,452 kB
  • sloc: ruby: 8,847; makefile: 4
file content (72 lines) | stat: -rw-r--r-- 1,684 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# frozen_string_literal: true

require_relative "question"
require_relative "symbols"

module TTY
  class Prompt
    # A prompt responsible for multi line user input
    #
    # @api private
    class Multiline < Question
      HELP = "(Press Ctrl+D or Ctrl+Z to finish)".freeze

      def initialize(prompt, **options)
        super
        @help         = options[:help] || self.class::HELP
        @first_render = true
        @lines_count  = 0
      end

      # Provide help information
      #
      # @return [String]
      #
      # @api public
      def help(value = (not_set = true))
        return @help if not_set

        @help = value
      end

      def read_input
        @prompt.read_multiline
      end

      def keyreturn(*)
        @lines_count += 1
      end
      alias keyenter keyreturn

      def render_question
        header = ["#{@prefix}#{message} "]
        if !echo?
          header
        elsif @done
          header << @prompt.decorate(@input.to_s, @active_color)
        elsif @first_render
          header << @prompt.decorate(help, @help_color)
          @first_render = false
        end
        header << "\n"
        header.join
      end

      def process_input(question)
        @prompt.print(question)
        @lines = read_input
        @input = "#{@lines.first.strip} ..." unless @lines.first.to_s.empty?
        if Utils.blank?(@input) && default?
          @input = default
          @lines = default
        end
        @evaluator.(@lines)
      end

      def refresh(lines, lines_to_clear)
        size = @lines_count + lines_to_clear + 1
        @prompt.clear_lines(size)
      end
    end # Multiline
  end # Prompt
end # TTY