module PaintCommand
PaintCommand — parse !paint subcommands and paint arguments. Public: parse, usage_message Depends: IrcFormat, PaintSamples Tests: test/lib/paint_command_test.rb
Constants
- SNIPPET_PATTERN
Public Instance Methods
Source
# File lib/paint_command.rb, line 20 def parse(args) text = args.to_s.strip return { action: :help } if text.empty? || text.casecmp("help").zero? parts = split_args(text) return { action: :help } if parts.empty? case parts[0].downcase when "list" parse_list(parts) when "preview" parse_preview(parts) when "reset" parse_reset(parts) when "analyse" parse_analyse(parts) else parse_paint(text) end end
Source
# File lib/paint_command.rb, line 65 def parse_analyse(parts) theme = parts[1]&.downcase theme = nil if theme.nil? || theme.empty? { action: :analyse, theme: theme } end
Source
# File lib/paint_command.rb, line 116 def parse_colour(value) IrcFormat.parse_colour_spec(value) end
Source
# File lib/paint_command.rb, line 102 def parse_line_spec(value) if value.include?("-") start_line, end_line = value.split("-", 2).map { |part| Integer(part, exception: false) } return nil unless start_line&.positive? && end_line&.positive? && end_line >= start_line (start_line..end_line).to_a else line = Integer(value, exception: false) return nil unless line&.positive? [line] end end
Source
# File lib/paint_command.rb, line 41 def parse_list(parts) plugin = parts[1]&.downcase return { action: :list, plugin: plugin } if plugin.nil? || plugin.empty? { action: :list, plugin: plugin } end
Source
# File lib/paint_command.rb, line 71 def parse_paint(text) snippet = nil if (match = text.match(SNIPPET_PATTERN)) snippet = match[:snippet][1..-2] text = text[0...match.begin(:snippet)].strip end parts = split_args(text) return { error: usage_message } if parts.length < 4 plugin = parts[0].downcase lines = parse_line_spec(parts[1]) return { error: usage_message } unless lines pos = Integer(parts[2], exception: false) return { error: usage_message } unless pos&.positive? colour = parse_colour(parts[3]) return colour if colour[:error] { action: :paint, plugin: plugin, lines: lines, pos: pos, fg: colour[:fg], bg: colour[:bg], snippet: snippet } end
Source
# File lib/paint_command.rb, line 48 def parse_preview(parts) plugin = parts[1]&.downcase return { error: usage_message } if plugin.nil? || plugin.empty? { action: :preview, plugin: plugin } end
Source
# File lib/paint_command.rb, line 55 def parse_reset(parts) plugin = parts[1]&.downcase return { error: usage_message } if plugin.nil? || plugin.empty? line = parts[2] ? Integer(parts[2], exception: false) : nil result = { action: :reset, plugin: plugin } result[:line] = line if line&.positive? result end
Source
# File lib/paint_command.rb, line 120 def split_args(text) text.scan(/"[^"]*"|'[^']*'|\S+/).map do |token| token.gsub(/\A["']|["']\z/, "") end end
Source
# File lib/paint_command.rb, line 16 def usage_message "Usage: !paint list|preview|reset|analyse|help — or !paint <plugin> <line|range> <pos> <fg[,bg]> [\"snippet\"]" end