Class: SpiritHands::Prompt::Render

Inherits:
Object
  • Object
show all
Defined in:
lib/spirit_hands/prompt/base/render.rb

Overview

<.…> <.…/> </.…>

Constant Summary collapse

MATCHED_TAG_CODES =

<tag> … </tag>, tag -> inner part of escape codes

{
  'b'             => 1,
  'bold'          => 1,
  'bright'        => 1,
  'strong'        => 1,

  'dark'          => 2,
  'faint'         => 2,

  'i'             => 3,
  'italic'        => 3,
  'em'            => 3,

  'u'             => 4,
  'underline'     => 4,

  'blink'         => 5,  # evil
  'flash'         => 5,

  'rapid-blink'   => 6, # sinister
  'rapid-flash'   => 6,

  'reverse'       => 7,
  'negative'      => 7,
  'inverse'       => 7,

  'concealed'     => 8,

  'strike'        => 9,
  'strikethrough' => 9,

  'black'         => 30,
  'red'           => 31,
  'green'         => 32,
  'yellow'        => 33,
  'blue'          => 34,
  'magenta'       => 35,
  'cyan'          => 36,
  'white'         => 37,

  'bgblack'       => 40,
  'bgred'         => 41,
  'bggreen'       => 42,
  'bgyellow'      => 43,
  'bgblue'        => 44,
  'bgmagenta'     => 45,
  'bgcyan'        => 46,
  'bgwhite'       => 47,
}
SINGLE_TAGS =

<…/>

{
  # <cmd/>: command number
  'cmd' => ->(state) { state.pry.input_array.size },

  # <tgt/>: target string
  'tgt' => ->(state) {
    unless (str = Pry.view_clip(state.object)) == 'main'
      state.level = 0 if state.level < 0
      "(#{'../' * state.level}#{str})"
    else
      ''
    end
  },

  # <app/>: state.app (Object) converted to String
  'app' => ->(state) {
    app = state.app
    if app.class.respond_to?(:parent_name) && \
       app.class.parent_name.respond_to?(:underscore)
      app.class.parent_name.underscore
    elsif app
      app.to_s
    else
      ::SpiritHands.app
    end
  },

  # <sep/>: SpiritHands.prompt_separator
  'sep' => ->(_state) {
    ::SpiritHands.prompt_separator
  }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state, prompt, color) ⇒ Render

:state SpiritHands::Prompt::State :prompt String :color true or false/nil



103
104
105
106
107
# File 'lib/spirit_hands/prompt/base/render.rb', line 103

def initialize(state, prompt, color)
  @state = state
  @prompt = prompt
  @color = color
end

Instance Attribute Details

#errorsObject (readonly)

Array<String>



94
95
96
# File 'lib/spirit_hands/prompt/base/render.rb', line 94

def errors
  @errors
end

Instance Method Details

#errors?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/spirit_hands/prompt/base/render.rb', line 96

def errors?
  errors.any?
end

#to_sObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/spirit_hands/prompt/base/render.rb', line 109

def to_s
  @errors = []
  @tag_stack = []
  @result = ''
  @color_applied = false
  @in_tag = false

  @prompt.each_char do |c|
    if @in_tag
      tag_char(c)
    else
      nontag_char(c)
    end
  end

  # @tag_stack.any? --> error/s
  @tag_stack.each { |t| errors << "<#{t}>: Missing </#{t}>" }

  (errors?) ? '' : @result
end