Skip to content
Kward Search API index

Class: Kward::NativeShell::PromptFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/shell/native_prompt_format.rb

Overview

Expand only known prompt escapes. Metadata is text, never format source or terminal control data. Only styles generated here occupy zero cells.

Constant Summary collapse

ESCAPES =
/(%(?:[FK]\{[^{}%]*\}|1~|[~d\/nm?#%fkbB]))/n.freeze
COLORS =
%w[black red green yellow blue magenta cyan white].freeze
STYLES =
{ "%f" => [39], "%k" => [49], "%B" => [1], "%b" => [22] }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(template, directory:, home:, status:, color:, prefix: "") ⇒ PromptFormat

Returns a new instance of PromptFormat.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/kward/shell/native_prompt_format.rb', line 14

def initialize(template, directory:, home:, status:, color:, prefix: "")
  @directory = directory.b
  @home = home.to_s.b
  @status = status
  @parts = template.b.split(ESCAPES).filter_map do |part|
    style = STYLES[part] || color_style(part)
    style ? (style if color) : expand(part)
  end
  @parts.unshift(prefix.b) unless prefix.empty?
  @styled = @parts.any? { |part| part.is_a?(Array) }
end

Instance Method Details

#cells(encoding) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/kward/shell/native_prompt_format.rb', line 26

def cells(encoding)
  @parts.flat_map do |part|
    if part.is_a?(Array)
      [[TerminalSequences.sgr(*part), 0]]
    else
      TerminalText.locale_cells(part.b.force_encoding(encoding).scrub)
    end
  end
end

#render(cells) ⇒ Object



36
37
38
39
# File 'lib/kward/shell/native_prompt_format.rb', line 36

def render(cells)
  text = cells.map(&:first).join
  @styled ? text + TerminalSequences::SGR_RESET : text
end