Sha256: 93ed1f7c6dc0d719523a939b4e9469b821f39aaacfc7c2dbf93cbbdf0be623e8

Contents?: true

Size: 1.4 KB

Versions: 4

Compression:

Stored size: 1.4 KB

Contents

# Aruba
module Aruba
  # Api
  module Api
    # Text manipulation
    module Text
      # Unescape text
      #
      # '\n' => "\n"
      # '\e' => "\e"
      # '\033' => "\e"
      # '\"' => '"'
      #
      # @param [#to_s] text
      #   Input
      def unescape_text(text)
        text
          .gsub('\n', "\n")
          .gsub('\"', '"')
          .gsub('\e', "\e")
          .gsub('\033', "\e")
          .gsub('\016', "\016")
          .gsub('\017', "\017")
          .gsub('\t', "\t")
      end

      # Remove ansi characters from text
      #
      # @param [#to_s] text
      #   Input
      def extract_text(text)
        text
          .gsub(/(?:\e|\033)\[\d+(?>(;\d+)*)m/, '')
          .gsub(/\\\[|\\\]/, '')
          .gsub(/\007|\016|\017/, '')
      end

      # Unescape special characters and remove ANSI characters
      #
      # @param [#to_s] text
      #   The text to sanitize
      def sanitize_text(text)
        text = unescape_text(text)
        text = extract_text(text) if aruba.config.remove_ansi_escape_sequences

        text.chomp
      end

      # Replace variables in command string (experimental)
      #
      # @param [#to_s] text
      #   The text to parse
      def replace_variables(text)
        if text.include? '<pid-last-command-started>'
          text = text.gsub(/<pid-last-command-started>/, last_command_started.pid.to_s)
        end

        text
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
aruba-1.0.4 lib/aruba/api/text.rb
aruba-1.0.3 lib/aruba/api/text.rb
aruba-1.0.2 lib/aruba/api/text.rb
aruba-1.0.1 lib/aruba/api/text.rb