Module: Clack::Core::Settings

Defined in:
lib/clack/core/settings.rb

Overview

Global configuration for key bindings, guide bar display, and input classification.

Constant Summary collapse

ACTIONS =

Navigation and control actions

%i[up down left right space enter cancel].freeze
KEY_BACKSPACE =

Key code constants

"\b"
KEY_DELETE =

ASCII 8: Backspace

"\u007F"
KEY_CTRL_C =

ASCII 127: Delete (often sent by backspace key)

"\u0003"
KEY_CTRL_D =

ASCII 3: Ctrl+C (interrupt)

"\u0004"
KEY_ESCAPE =

ASCII 4: Ctrl+D (EOF, used for multiline submit)

"\e"
KEY_ENTER =

ASCII 27: Escape

"\r"
KEY_NEWLINE =

ASCII 13: Carriage return

"\n"
KEY_SPACE =

ASCII 10: Line feed

" "
PRINTABLE_CHAR_MIN =

First printable ASCII character (space)

32
ALIASES =

Key to action mappings

{
  "k" => :up,
  "j" => :down,
  "h" => :left,
  "l" => :right,
  "\e[A" => :up,
  "\e[B" => :down,
  "\e[C" => :right,
  "\e[D" => :left,
  KEY_ENTER => :enter,
  KEY_NEWLINE => :enter,
  KEY_SPACE => :space,
  KEY_ESCAPE => :cancel,
  KEY_CTRL_C => :cancel
}.freeze

Class Method Summary collapse

Class Method Details

.action?(key) ⇒ Symbol?

Look up the action mapped to a key code.

Parameters:

Returns:

  • (Symbol, nil)

    the action (:up, :down, :enter, etc.) or nil



89
90
91
92
# File 'lib/clack/core/settings.rb', line 89

def action?(key)
  aliases = @config_mutex.synchronize { @config[:aliases] }
  aliases[key] if ACTIONS.include?(aliases[key])
end

.backspace?(key) ⇒ Boolean

Check if a key is a backspace/delete

Returns:

  • (Boolean)


100
101
102
# File 'lib/clack/core/settings.rb', line 100

def backspace?(key)
  [KEY_BACKSPACE, KEY_DELETE].include?(key)
end

.configHash

Get a copy of the current global config

Returns:

  • (Hash)

    Current configuration



51
52
53
# File 'lib/clack/core/settings.rb', line 51

def config
  @config_mutex.synchronize { @config.dup }
end

.printable?(key) ⇒ Boolean

Check if a key is a printable character (handles combining marks and multi-codepoint grapheme clusters)

Returns:

  • (Boolean)


95
96
97
# File 'lib/clack/core/settings.rb', line 95

def printable?(key)
  key && key.grapheme_clusters.length == 1 && key.ord >= PRINTABLE_CHAR_MIN
end

.reset!Object

Reset settings to defaults



70
71
72
73
74
75
76
77
78
# File 'lib/clack/core/settings.rb', line 70

def reset!
  @config_mutex.synchronize do
    @config = {
      aliases: ALIASES.dup,
      with_guide: true,
      ci_mode: false
    }
  end
end

.update(aliases: nil, with_guide: nil, ci_mode: nil) ⇒ Hash

Update global settings

Parameters:

  • aliases (Hash, nil) (defaults to: nil)

    Custom key to action mappings (merged with defaults)

  • with_guide (Boolean, nil) (defaults to: nil)

    Whether to show guide bars

  • ci_mode (Boolean, Symbol, nil) (defaults to: nil)

    CI mode: true (always), :auto (detect), false (never)

Returns:

  • (Hash)

    Updated configuration



60
61
62
63
64
65
66
67
# File 'lib/clack/core/settings.rb', line 60

def update(aliases: nil, with_guide: nil, ci_mode: nil)
  @config_mutex.synchronize do
    @config[:aliases] = ALIASES.merge(aliases) if aliases
    @config[:with_guide] = with_guide unless with_guide.nil?
    @config[:ci_mode] = ci_mode unless ci_mode.nil?
    @config.dup
  end
end

.with_guide?Boolean

Check if guide bars should be shown

Returns:

  • (Boolean)


82
83
84
# File 'lib/clack/core/settings.rb', line 82

def with_guide?
  @config_mutex.synchronize { @config[:with_guide] }
end