llm-shell 0.9.2 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +61 -66
- data/lib/llm/shell/command.rb +40 -40
- data/lib/llm/shell/commands/clear_screen.rb +4 -18
- data/lib/llm/shell/commands/debug_mode.rb +12 -0
- data/lib/llm/shell/commands/dir_import.rb +4 -20
- data/lib/llm/shell/commands/disable_tool.rb +33 -0
- data/lib/llm/shell/commands/enable_tool.rb +33 -0
- data/lib/llm/shell/commands/file_import.rb +4 -20
- data/lib/llm/shell/commands/help.rb +23 -36
- data/lib/llm/shell/commands/show_chat.rb +4 -19
- data/lib/llm/shell/commands/show_version.rb +4 -20
- data/lib/llm/shell/commands/system_prompt.rb +4 -18
- data/lib/llm/shell/completion.rb +5 -5
- data/lib/llm/shell/config.rb +4 -5
- data/lib/llm/shell/formatter.rb +1 -2
- data/lib/llm/shell/internal/coderay/lib/coderay/duo.rb +81 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/_map.rb +17 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/comment_filter.rb +25 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/count.rb +39 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/debug.rb +49 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/debug_lint.rb +63 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/div.rb +23 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/encoder.rb +190 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/filter.rb +58 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/html/css.rb +65 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/html/numbering.rb +108 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/html/output.rb +164 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/html.rb +333 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/json.rb +83 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/lines_of_code.rb +45 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/lint.rb +59 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/null.rb +18 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/page.rb +24 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/span.rb +23 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/statistic.rb +95 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/terminal.rb +195 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/text.rb +46 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/token_kind_filter.rb +111 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/xml.rb +72 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders/yaml.rb +50 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/encoders.rb +18 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/for_redcloth.rb +95 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/helpers/file_type.rb +151 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/helpers/plugin.rb +55 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/helpers/plugin_host.rb +221 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/helpers/word_list.rb +72 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/_map.rb +24 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/c.rb +189 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/clojure.rb +217 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/cpp.rb +217 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/css.rb +196 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/debug.rb +75 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/delphi.rb +144 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/diff.rb +221 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/erb.rb +81 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/go.rb +208 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/groovy.rb +268 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/haml.rb +168 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/html.rb +275 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/java/builtin_types.rb +421 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/java.rb +174 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/java_script.rb +236 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/json.rb +98 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/lua.rb +280 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/php.rb +527 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/python.rb +287 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/raydebug.rb +75 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/ruby/patterns.rb +178 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/ruby/string_state.rb +79 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/ruby.rb +477 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/sass.rb +232 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/scanner.rb +337 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/sql.rb +169 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/taskpaper.rb +36 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/text.rb +26 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/xml.rb +17 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners/yaml.rb +140 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/scanners.rb +27 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/styles/_map.rb +7 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/styles/alpha.rb +153 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/styles/style.rb +18 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/styles.rb +15 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/token_kinds.rb +85 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/tokens.rb +164 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/tokens_proxy.rb +55 -0
- data/lib/llm/shell/internal/coderay/lib/coderay/version.rb +3 -0
- data/lib/llm/shell/internal/coderay/lib/coderay.rb +284 -0
- data/lib/llm/shell/internal/io-line/lib/io/line/multiple.rb +19 -0
- data/lib/{io → llm/shell/internal/io-line/lib/io}/line.rb +2 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/bot/builder.rb +31 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/bot/conversable.rb +37 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/bot/prompt/completion.rb +49 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/bot/prompt/respond.rb +49 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/bot.rb +150 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/buffer.rb +162 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/client.rb +36 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/error.rb +49 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/eventhandler.rb +44 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/eventstream/event.rb +69 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/eventstream/parser.rb +88 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/eventstream.rb +8 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/file.rb +91 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/function.rb +177 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/message.rb +178 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/mime.rb +140 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/multipart.rb +101 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/object/builder.rb +38 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/object/kernel.rb +53 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/object.rb +89 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/provider.rb +352 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/error_handler.rb +36 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/files.rb +155 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/format/completion_format.rb +88 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/format.rb +29 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/models.rb +54 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/response/completion.rb +39 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/response/enumerable.rb +11 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/response/file.rb +23 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/response/web_search.rb +21 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic/stream_parser.rb +66 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/anthropic.rb +138 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/deepseek/format/completion_format.rb +68 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/deepseek/format.rb +27 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/deepseek.rb +75 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/audio.rb +73 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/error_handler.rb +47 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/files.rb +146 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/format/completion_format.rb +69 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/format.rb +39 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/images.rb +133 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/models.rb +60 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/completion.rb +35 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/embedding.rb +8 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/file.rb +11 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/files.rb +15 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/image.rb +31 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/models.rb +15 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/response/web_search.rb +22 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini/stream_parser.rb +86 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/gemini.rb +173 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/llamacpp.rb +74 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/error_handler.rb +36 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/format/completion_format.rb +77 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/format.rb +29 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/models.rb +56 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/response/completion.rb +28 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/response/embedding.rb +9 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama/stream_parser.rb +44 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/ollama.rb +116 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/audio.rb +91 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/error_handler.rb +46 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/files.rb +134 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/format/completion_format.rb +90 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/format/moderation_format.rb +35 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/format/respond_format.rb +72 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/format.rb +54 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/images.rb +109 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/models.rb +55 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/moderations.rb +65 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/audio.rb +7 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/completion.rb +40 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/embedding.rb +9 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/enumerable.rb +23 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/file.rb +7 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/image.rb +16 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/moderations.rb +34 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/responds.rb +48 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/response/web_search.rb +21 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/responses/stream_parser.rb +76 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/responses.rb +99 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/stream_parser.rb +86 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai/vector_stores.rb +228 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/openai.rb +206 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/xai/images.rb +58 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/xai.rb +72 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/providers/zai.rb +74 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/response.rb +67 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/array.rb +26 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/boolean.rb +13 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/integer.rb +43 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/leaf.rb +78 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/null.rb +13 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/number.rb +43 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/object.rb +41 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/string.rb +34 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema/version.rb +8 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/schema.rb +81 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/server_tool.rb +32 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/tool/param.rb +75 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/tool.rb +78 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/utils.rb +19 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm/version.rb +5 -0
- data/lib/llm/shell/internal/llm.rb/lib/llm.rb +121 -0
- data/lib/llm/shell/internal/optparse/lib/optionparser.rb +2 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/ac.rb +70 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/date.rb +18 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/kwargs.rb +27 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/shellwords.rb +7 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/time.rb +11 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/uri.rb +7 -0
- data/lib/llm/shell/internal/optparse/lib/optparse/version.rb +80 -0
- data/lib/llm/shell/internal/optparse/lib/optparse.rb +2469 -0
- data/lib/llm/shell/internal/paint/lib/paint/constants.rb +104 -0
- data/lib/llm/shell/internal/paint/lib/paint/pa.rb +13 -0
- data/lib/llm/shell/internal/paint/lib/paint/rgb_colors.rb +14 -0
- data/lib/llm/shell/internal/paint/lib/paint/shortcuts.rb +100 -0
- data/lib/llm/shell/internal/paint/lib/paint/shortcuts_version.rb +5 -0
- data/lib/llm/shell/internal/paint/lib/paint/util.rb +16 -0
- data/lib/llm/shell/internal/paint/lib/paint/version.rb +5 -0
- data/lib/llm/shell/internal/paint/lib/paint.rb +261 -0
- data/lib/llm/shell/internal/reline/lib/reline/config.rb +378 -0
- data/lib/llm/shell/internal/reline/lib/reline/face.rb +199 -0
- data/lib/llm/shell/internal/reline/lib/reline/history.rb +76 -0
- data/lib/llm/shell/internal/reline/lib/reline/io/ansi.rb +322 -0
- data/lib/llm/shell/internal/reline/lib/reline/io/dumb.rb +120 -0
- data/lib/llm/shell/internal/reline/lib/reline/io/windows.rb +530 -0
- data/lib/llm/shell/internal/reline/lib/reline/io.rb +55 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor/base.rb +37 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor/composite.rb +17 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor/emacs.rb +517 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor/vi_command.rb +518 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor/vi_insert.rb +517 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_actor.rb +8 -0
- data/lib/llm/shell/internal/reline/lib/reline/key_stroke.rb +119 -0
- data/lib/llm/shell/internal/reline/lib/reline/kill_ring.rb +125 -0
- data/lib/llm/shell/internal/reline/lib/reline/line_editor.rb +2356 -0
- data/lib/llm/shell/internal/reline/lib/reline/unicode/east_asian_width.rb +1292 -0
- data/lib/llm/shell/internal/reline/lib/reline/unicode.rb +421 -0
- data/lib/llm/shell/internal/reline/lib/reline/version.rb +3 -0
- data/lib/llm/shell/internal/reline/lib/reline.rb +527 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/generated_parser.rb +712 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/handler.rb +268 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/local_date.rb +35 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/local_date_time.rb +42 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/local_time.rb +40 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/parser.rb +21 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/scanner.rb +92 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/string_utils.rb +40 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb/version.rb +5 -0
- data/lib/llm/shell/internal/tomlrb/lib/tomlrb.rb +49 -0
- data/lib/llm/shell/options.rb +1 -1
- data/lib/llm/shell/renderer.rb +2 -3
- data/lib/llm/shell/repl.rb +21 -16
- data/lib/llm/shell/tool.rb +42 -0
- data/lib/llm/shell/tools/read_file.rb +15 -0
- data/lib/llm/shell/tools/system.rb +17 -0
- data/lib/llm/shell/tools/write_file.rb +16 -0
- data/lib/llm/shell/version.rb +1 -1
- data/lib/llm/shell.rb +83 -39
- data/libexec/llm-shell/shell +4 -6
- data/llm-shell.gemspec +0 -4
- metadata +233 -63
- data/lib/llm/function.rb +0 -17
- data/lib/llm/shell/command/extension.rb +0 -42
- data/lib/llm/shell/commands/utils.rb +0 -21
- data/lib/llm/shell/functions/read_file.rb +0 -22
- data/lib/llm/shell/functions/write_file.rb +0 -22
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Paint
|
|
4
|
+
# Clears all colors
|
|
5
|
+
NOTHING = "\033[0m"
|
|
6
|
+
|
|
7
|
+
# Number of possible colors in TRUE COLOR mode
|
|
8
|
+
TRUE_COLOR = 0xFFFFFF
|
|
9
|
+
|
|
10
|
+
# Basic colors (often, the color differs when using the bright effect)
|
|
11
|
+
# Final color will be 30 + value for foreground and 40 + value for background
|
|
12
|
+
ANSI_COLORS = {
|
|
13
|
+
:black => 0,
|
|
14
|
+
:red => 1,
|
|
15
|
+
:green => 2,
|
|
16
|
+
:yellow => 3,
|
|
17
|
+
:blue => 4,
|
|
18
|
+
:magenta => 5,
|
|
19
|
+
:cyan => 6,
|
|
20
|
+
:white => 7, :gray => 7,
|
|
21
|
+
:default => 9,
|
|
22
|
+
}.freeze
|
|
23
|
+
|
|
24
|
+
ANSI_COLORS_FOREGROUND = {
|
|
25
|
+
:black => 30,
|
|
26
|
+
:red => 31,
|
|
27
|
+
:green => 32,
|
|
28
|
+
:yellow => 33,
|
|
29
|
+
:blue => 34,
|
|
30
|
+
:magenta => 35,
|
|
31
|
+
:cyan => 36,
|
|
32
|
+
:white => 37, :gray => 37,
|
|
33
|
+
:default => 39,
|
|
34
|
+
}.freeze
|
|
35
|
+
|
|
36
|
+
ANSI_COLORS_BACKGROUND = {
|
|
37
|
+
:black => 40,
|
|
38
|
+
:red => 41,
|
|
39
|
+
:green => 42,
|
|
40
|
+
:yellow => 43,
|
|
41
|
+
:blue => 44,
|
|
42
|
+
:magenta => 45,
|
|
43
|
+
:cyan => 46,
|
|
44
|
+
:white => 47, :gray => 47,
|
|
45
|
+
:default => 49,
|
|
46
|
+
}.freeze
|
|
47
|
+
|
|
48
|
+
# Terminal effects - most of them are not supported ;)
|
|
49
|
+
# See https://en.wikipedia.org/wiki/ANSI_escape_code
|
|
50
|
+
ANSI_EFFECTS = {
|
|
51
|
+
:reset => 0, :nothing => 0, # usually supported
|
|
52
|
+
:bright => 1, :bold => 1, # usually supported
|
|
53
|
+
:faint => 2,
|
|
54
|
+
:italic => 3,
|
|
55
|
+
:underline => 4, # usually supported
|
|
56
|
+
:blink => 5, :slow_blink => 5,
|
|
57
|
+
:rapid_blink => 6,
|
|
58
|
+
:inverse => 7, :swap => 7, # usually supported
|
|
59
|
+
:conceal => 8, :hide => 9,
|
|
60
|
+
:default_font => 10,
|
|
61
|
+
:font0 => 10, :font1 => 11, :font2 => 12, :font3 => 13, :font4 => 14,
|
|
62
|
+
:font5 => 15, :font6 => 16, :font7 => 17, :font8 => 18, :font9 => 19,
|
|
63
|
+
:fraktur => 20,
|
|
64
|
+
:bright_off => 21, :bold_off => 21, :double_underline => 21,
|
|
65
|
+
:clean => 22,
|
|
66
|
+
:italic_off => 23, :fraktur_off => 23,
|
|
67
|
+
:underline_off => 24,
|
|
68
|
+
:blink_off => 25,
|
|
69
|
+
:inverse_off => 26, :positive => 26,
|
|
70
|
+
:conceal_off => 27, :show => 27, :reveal => 27,
|
|
71
|
+
:crossed_off => 29, :crossed_out_off => 29,
|
|
72
|
+
:frame => 51,
|
|
73
|
+
:encircle => 52,
|
|
74
|
+
:overline => 53,
|
|
75
|
+
:frame_off => 54, :encircle_off => 54,
|
|
76
|
+
:overline_off => 55,
|
|
77
|
+
}.freeze
|
|
78
|
+
|
|
79
|
+
# A list of color names for standard ansi colors, needed for 16/8 color fallback mode
|
|
80
|
+
# See https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
|
|
81
|
+
RGB_COLORS_ANSI = {
|
|
82
|
+
:black => [ 0, 0, 0],
|
|
83
|
+
:red => [205, 0, 0],
|
|
84
|
+
:green => [ 0, 205, 0],
|
|
85
|
+
:yellow => [205, 205, 0],
|
|
86
|
+
:blue => [ 0, 0, 238],
|
|
87
|
+
:magenta => [205, 0, 205],
|
|
88
|
+
:cyan => [ 0, 205, 205],
|
|
89
|
+
:white => [229, 229, 229], :gray => [229, 229, 229],
|
|
90
|
+
}.each { |k, v| v.freeze }.freeze
|
|
91
|
+
|
|
92
|
+
# A list of color names for standard bright ansi colors, needed for 16 color fallback mode
|
|
93
|
+
# See https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
|
|
94
|
+
RGB_COLORS_ANSI_BRIGHT = {
|
|
95
|
+
:black => [127, 127, 127],
|
|
96
|
+
:red => [255, 0, 0],
|
|
97
|
+
:green => [ 0, 255, 0],
|
|
98
|
+
:yellow => [255, 255, 0],
|
|
99
|
+
:blue => [ 92, 92, 255],
|
|
100
|
+
:magenta => [255, 0, 255],
|
|
101
|
+
:cyan => [ 0, 255, 255],
|
|
102
|
+
:white => [255, 255, 255], :gray => [255, 255, 255],
|
|
103
|
+
}.each { |k, v| v.freeze }.freeze
|
|
104
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "zlib"
|
|
4
|
+
|
|
5
|
+
module Paint
|
|
6
|
+
RGB_COLORS_INDEX_FILENAME = File.dirname(__FILE__) + "/../../data/rgb_colors.marshal.gz"
|
|
7
|
+
# A list of color names, based on X11's rgb.txt
|
|
8
|
+
|
|
9
|
+
File.open(RGB_COLORS_INDEX_FILENAME, "rb") do |file|
|
|
10
|
+
serialized_data = Zlib::GzipReader.new(file).read
|
|
11
|
+
serialized_data.force_encoding Encoding::BINARY
|
|
12
|
+
RGB_COLORS = Marshal.load(serialized_data)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'shortcuts_version'
|
|
4
|
+
|
|
5
|
+
require 'paint'
|
|
6
|
+
|
|
7
|
+
module Paint
|
|
8
|
+
# Hash for defining color/effect shortcuts
|
|
9
|
+
SHORTCUTS = {
|
|
10
|
+
# :example => { # would create a Paint::Example constant...
|
|
11
|
+
# :light_red => "\e[31;1m", # with a method .light_red
|
|
12
|
+
# }
|
|
13
|
+
}
|
|
14
|
+
SHORTCUTS.default = {}
|
|
15
|
+
|
|
16
|
+
class << self
|
|
17
|
+
# Paint::SomeModule --> Paint::SHORTCUTS[:some_module]
|
|
18
|
+
def const_missing(mod_name)
|
|
19
|
+
# get shortcuts
|
|
20
|
+
shortcuts = SHORTCUTS[mod_name.to_s.gsub(/[A-Z]/,'_\0').downcase[1..-1].to_sym] || []
|
|
21
|
+
|
|
22
|
+
# create module
|
|
23
|
+
class_eval "module #{mod_name}; end"
|
|
24
|
+
mod = const_get(mod_name)
|
|
25
|
+
|
|
26
|
+
# define direct behaviour, class methods
|
|
27
|
+
mod.define_singleton_method :method_missing do |color_name, *args|
|
|
28
|
+
if color_code = shortcuts[color_name]
|
|
29
|
+
string = Array(args).join
|
|
30
|
+
return string if Paint.mode.zero?
|
|
31
|
+
|
|
32
|
+
if args.empty?
|
|
33
|
+
color_code
|
|
34
|
+
else
|
|
35
|
+
color_code + string + NOTHING
|
|
36
|
+
end
|
|
37
|
+
else
|
|
38
|
+
nil
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
mod.define_singleton_method :respond_to_missing? do |color_name, *args|
|
|
43
|
+
shortcuts.include?(color_name) || super(color_name, *args)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# define include behaviour, instance methods
|
|
47
|
+
mod.define_singleton_method :included do |_|
|
|
48
|
+
shortcuts.each{ |color_name, color_code|
|
|
49
|
+
define_method color_name do |*args|
|
|
50
|
+
string = Array(args).join
|
|
51
|
+
return string if Paint.mode.zero?
|
|
52
|
+
|
|
53
|
+
if args.empty?
|
|
54
|
+
color_code
|
|
55
|
+
else
|
|
56
|
+
color_code + string + NOTHING
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
}
|
|
60
|
+
private(*shortcuts.keys) unless shortcuts.empty?
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# include variations, defined in child modules
|
|
64
|
+
mod.class_eval "module String; end"
|
|
65
|
+
string = mod.const_get(:String)
|
|
66
|
+
string.define_singleton_method :included do |_|
|
|
67
|
+
shortcuts.each{ |color_name, color_code|
|
|
68
|
+
define_method color_name do
|
|
69
|
+
if Paint.mode.zero?
|
|
70
|
+
to_s
|
|
71
|
+
else
|
|
72
|
+
color_code + to_s + NOTHING
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
}
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# OK, let's take it one level further ;)
|
|
79
|
+
mod.class_eval "module Prefix; end"
|
|
80
|
+
prefix_prefix = mod.const_get(:Prefix)
|
|
81
|
+
prefix_prefix.define_singleton_method :const_missing do |prefix_name|
|
|
82
|
+
class_eval "module #{prefix_name}; end"
|
|
83
|
+
prefix = const_get(prefix_name)
|
|
84
|
+
|
|
85
|
+
prefix.define_singleton_method :included do |_|
|
|
86
|
+
define_method prefix_name.to_s.gsub(/[A-Z]/,'_\0').downcase[1..-1].to_sym do |color_name|
|
|
87
|
+
if color_code = shortcuts[color_name]
|
|
88
|
+
return to_s if Paint.mode.zero?
|
|
89
|
+
color_code + to_s + NOTHING
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
prefix
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
mod
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Paint
|
|
4
|
+
# Standalone helpers which add functionality you sometimes need when working with terminal colors
|
|
5
|
+
class << self
|
|
6
|
+
# Removes any color and effect strings
|
|
7
|
+
def unpaint(string)
|
|
8
|
+
string.gsub(/\e\[(?:[0-9];?)+m/, '')
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Creates a random ANSI color
|
|
12
|
+
def random(background = false)
|
|
13
|
+
(background ? 40 : 30) + rand(8)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'paint/version'
|
|
4
|
+
require_relative 'paint/constants'
|
|
5
|
+
require_relative 'paint/util'
|
|
6
|
+
|
|
7
|
+
module Paint
|
|
8
|
+
autoload :RGB_COLORS, 'paint/rgb_colors'
|
|
9
|
+
|
|
10
|
+
class << self
|
|
11
|
+
# Takes a string and color options and colorizes the string, fast version without nesting
|
|
12
|
+
def [](string, *options)
|
|
13
|
+
return string.to_s if @mode.zero? || options.empty?
|
|
14
|
+
options = options[0] if options.size == 1 && !options[0].respond_to?(:to_ary)
|
|
15
|
+
@cache[options] + string.to_s + NOTHING
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Takes an array with string and color options and colorizes the string,
|
|
19
|
+
# extended version with nesting and substitution support
|
|
20
|
+
def %(paint_arguments, clear_color = NOTHING)
|
|
21
|
+
string, *options = paint_arguments
|
|
22
|
+
return string.to_s if options.empty?
|
|
23
|
+
substitutions = options.pop if options[-1].is_a?(Hash)
|
|
24
|
+
options = options[0] if options.size == 1 && !options[0].respond_to?(:to_ary)
|
|
25
|
+
current_color = @cache[options]
|
|
26
|
+
|
|
27
|
+
# Substitutions & Nesting
|
|
28
|
+
if substitutions
|
|
29
|
+
substitutions.each{ |key, value|
|
|
30
|
+
string = string.gsub(
|
|
31
|
+
"%{#{key}}",
|
|
32
|
+
value.is_a?(Array) ?
|
|
33
|
+
self.%(value, clear_color + current_color) :
|
|
34
|
+
value.to_s
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Wrap string (if Paint.mode > 0)
|
|
40
|
+
if @mode.zero?
|
|
41
|
+
string.to_s
|
|
42
|
+
else
|
|
43
|
+
current_color + string.to_s + clear_color
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Transforms options into the desired color. Used by @cache
|
|
48
|
+
def color(*options)
|
|
49
|
+
return '' if @mode.zero? || options.empty?
|
|
50
|
+
mix = []
|
|
51
|
+
color_seen = false
|
|
52
|
+
colors = ANSI_COLORS_FOREGROUND
|
|
53
|
+
|
|
54
|
+
options.each{ |option|
|
|
55
|
+
case option
|
|
56
|
+
when Symbol
|
|
57
|
+
if color = colors[option]
|
|
58
|
+
mix << color
|
|
59
|
+
color_seen = :set
|
|
60
|
+
elsif ANSI_EFFECTS.key?(option)
|
|
61
|
+
mix << effect(option)
|
|
62
|
+
else
|
|
63
|
+
raise ArgumentError, "Unknown color or effect: #{ option }"
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
when Array
|
|
67
|
+
if option.size == 3 && option.all?{ |n| n.is_a? Numeric }
|
|
68
|
+
mix << rgb(*[*option, color_seen])
|
|
69
|
+
color_seen = :set
|
|
70
|
+
else
|
|
71
|
+
raise ArgumentError, "Array argument must contain 3 numerals"
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
when ::String
|
|
75
|
+
if option =~ /\A#?(?<hex_color>[[:xdigit:]]{3}{1,2})\z/ # 3 or 6 hex chars
|
|
76
|
+
mix << rgb_hex($~[:hex_color], color_seen)
|
|
77
|
+
color_seen = :set
|
|
78
|
+
else
|
|
79
|
+
mix << rgb_name(option, color_seen)
|
|
80
|
+
color_seen = :set
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
when Numeric
|
|
84
|
+
integer = option.to_i
|
|
85
|
+
color_seen = :set if (30..49).include?(integer)
|
|
86
|
+
mix << integer
|
|
87
|
+
|
|
88
|
+
when nil
|
|
89
|
+
color_seen = :set
|
|
90
|
+
|
|
91
|
+
else
|
|
92
|
+
raise ArgumentError, "Invalid argument: #{ option.inspect }"
|
|
93
|
+
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
if color_seen == :set
|
|
97
|
+
colors = ANSI_COLORS_BACKGROUND
|
|
98
|
+
color_seen = true
|
|
99
|
+
end
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
wrap(*mix)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# This variable influences the color code generation
|
|
106
|
+
# Currently supported values:
|
|
107
|
+
# * 0xFFFFFF - 24-bit (~16 million) colors, aka truecolor
|
|
108
|
+
# * 256 - 256 colors
|
|
109
|
+
# * 16 - only ansi colors and bright effect
|
|
110
|
+
# * 8 - only ansi colors
|
|
111
|
+
# * 0 - no colorization!
|
|
112
|
+
attr_reader :mode
|
|
113
|
+
def mode=(val)
|
|
114
|
+
@cache.clear
|
|
115
|
+
|
|
116
|
+
case val
|
|
117
|
+
when 0, 8, 16, 256, TRUE_COLOR
|
|
118
|
+
@mode = val
|
|
119
|
+
when TrueClass
|
|
120
|
+
@mode = TRUE_COLOR
|
|
121
|
+
when nil
|
|
122
|
+
@mode = 0
|
|
123
|
+
else
|
|
124
|
+
raise ArgumentError, "Cannot set paint mode to value <#{val}>, possible values are: 0xFFFFFF (true), 256, 16, 8, 0 (nil)"
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Adds ANSI sequence
|
|
129
|
+
def wrap(*ansi_codes)
|
|
130
|
+
"\033[" + ansi_codes*";" + "m"
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Creates simple ansi color by looking it up on Paint::ANSI_COLORS
|
|
134
|
+
def simple(color_name, background = false)
|
|
135
|
+
(background ? 40 : 30) + ANSI_COLORS[color_name]
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# If not true_color, creates a 256-compatible color from rgb values,
|
|
139
|
+
# otherwise, an exact 24-bit color
|
|
140
|
+
def rgb(red, green, blue, background = false)
|
|
141
|
+
case @mode
|
|
142
|
+
when 8
|
|
143
|
+
"#{background ? 4 : 3}#{rgb_to_ansi(red, green, blue, false)}"
|
|
144
|
+
when 16
|
|
145
|
+
"#{background ? 4 : 3}#{rgb_to_ansi(red, green, blue, true)}"
|
|
146
|
+
when 256
|
|
147
|
+
"#{background ? 48 : 38}#{rgb_to_256(red, green, blue)}"
|
|
148
|
+
when TRUE_COLOR
|
|
149
|
+
"#{background ? 48 : 38}#{rgb_true(red, green, blue)}"
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Creates RGB color from a HTML-like color definition string
|
|
154
|
+
def rgb_hex(string, background = false)
|
|
155
|
+
case string.size
|
|
156
|
+
when 6
|
|
157
|
+
color_code = string.each_char.each_slice(2).map{ |hex_color| hex_color.join.to_i(16) }
|
|
158
|
+
when 3
|
|
159
|
+
color_code = string.each_char.map{ |hex_color_half| (hex_color_half*2).to_i(16) }
|
|
160
|
+
end
|
|
161
|
+
rgb(*[*color_code, background])
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# Creates a RGB from a name found in Paint::RGB_COLORS (based on rgb.txt)
|
|
165
|
+
def rgb_name(color_name, background = false)
|
|
166
|
+
if color_code = RGB_COLORS[color_name]
|
|
167
|
+
rgb(*[*color_code, background])
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# Creates the specified effect by looking it up in Paint::ANSI_EFFECTS
|
|
172
|
+
def effect(effect_name)
|
|
173
|
+
ANSI_EFFECTS[effect_name]
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# Determine supported colors
|
|
177
|
+
# Note: there's no reliable test for 24-bit color support
|
|
178
|
+
def detect_mode
|
|
179
|
+
if ENV['NO_COLOR'] # see https://no-color.org/
|
|
180
|
+
0
|
|
181
|
+
elsif RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ # windows
|
|
182
|
+
if ENV['ANSICON']
|
|
183
|
+
16
|
|
184
|
+
elsif ENV['ConEmuANSI'] == 'ON'
|
|
185
|
+
TRUE_COLOR
|
|
186
|
+
else
|
|
187
|
+
0
|
|
188
|
+
end
|
|
189
|
+
elsif ENV['TERM_PROGRAM'] == 'Apple_Terminal'
|
|
190
|
+
256
|
|
191
|
+
else
|
|
192
|
+
case ENV['TERM']
|
|
193
|
+
when /^rxvt-(?:.*)-256color$/
|
|
194
|
+
256
|
|
195
|
+
when /-color$/, /^rxvt/
|
|
196
|
+
16
|
|
197
|
+
else # optimistic default
|
|
198
|
+
TRUE_COLOR
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
private
|
|
204
|
+
|
|
205
|
+
# Returns 24-bit color value (see https://gist.github.com/XVilka/8346728)
|
|
206
|
+
# in ANSI escape sequnce format, without fore-/background information
|
|
207
|
+
def rgb_true(red, green, blue)
|
|
208
|
+
";2;#{red};#{green};#{blue}"
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# Returns closest supported 256-color an RGB value, without fore-/background information
|
|
212
|
+
# Inspired by the rainbow gem
|
|
213
|
+
def rgb_to_256(red, green, blue, approx = true)
|
|
214
|
+
return ";2;#{red};#{green};#{blue}" unless approx
|
|
215
|
+
|
|
216
|
+
gray_possible = true
|
|
217
|
+
sep = 42.5
|
|
218
|
+
|
|
219
|
+
while gray_possible
|
|
220
|
+
if red < sep || green < sep || blue < sep
|
|
221
|
+
gray = red < sep && green < sep && blue < sep
|
|
222
|
+
gray_possible = false
|
|
223
|
+
end
|
|
224
|
+
sep += 42.5
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
if gray
|
|
228
|
+
";5;#{ 232 + ((red.to_f + green.to_f + blue.to_f)/33).round }"
|
|
229
|
+
else # rgb
|
|
230
|
+
";5;#{ [16, *[red, green, blue].zip([36, 6, 1]).map{ |color, mod|
|
|
231
|
+
(6 * (color.to_f / 256)).to_i * mod
|
|
232
|
+
}].inject(:+) }"
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# Returns best ANSI color matching an RGB value, without fore-/background information
|
|
237
|
+
# See https://mail.python.org/pipermail/python-list/2008-December/1150496.html
|
|
238
|
+
def rgb_to_ansi(red, green, blue, use_bright = false)
|
|
239
|
+
color_pool = RGB_COLORS_ANSI.values
|
|
240
|
+
color_pool += RGB_COLORS_ANSI_BRIGHT.values if use_bright
|
|
241
|
+
|
|
242
|
+
ansi_color_rgb = color_pool.min_by{ |col| rgb_color_distance([red, green, blue],col) }
|
|
243
|
+
if ansi_color = RGB_COLORS_ANSI.key(ansi_color_rgb)
|
|
244
|
+
ANSI_COLORS[ansi_color]
|
|
245
|
+
else
|
|
246
|
+
ansi_color = RGB_COLORS_ANSI_BRIGHT.key(ansi_color_rgb)
|
|
247
|
+
"#{ANSI_COLORS[ansi_color]};1"
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def rgb_color_distance(rgb1, rgb2)
|
|
252
|
+
rgb1.zip(rgb2).inject(0){ |acc, (cur1, cur2)|
|
|
253
|
+
acc + (cur1 - cur2)**2
|
|
254
|
+
}
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# init instance vars
|
|
259
|
+
@mode = detect_mode
|
|
260
|
+
@cache = Hash.new{ |h, k| h[k] = color(*k) }
|
|
261
|
+
end
|