module BannerCharCommand
BannerCharCommand — parse and handle !bannerchar (variable-width glyph editor). Public: parse, handle, split_joined_ascii, usage_message Depends: BannerFontStore, BannerFontReport, BannerFont Tests: test/lib/banner_char_command_test.rb
Constants
- MAX_WIDTH
- MIN_WIDTH
Public Instance Methods
Source
# File lib/banner_char_command.rb, line 129 def channel_allowed?(channel) !channel.nil? && !channel.to_s.strip.empty? end
Source
# File lib/banner_char_command.rb, line 134 def channel_error { error: true, reply: BannerFontReport.pm_error, multiline: false } end
Source
# File lib/banner_char_command.rb, line 139 def error_result(message) { error: true, reply: BannerFontReport.error(message), multiline: false } end
Source
# File lib/banner_char_command.rb, line 77 def handle(args, network:, channel:, store: RabbotDb) return channel_error unless channel_allowed?(channel) parsed = parse(args) return error_result(parsed[:error]) if parsed[:error] case parsed[:action] when :show handle_show(parsed[:letter], network: network, channel: channel, store: store) when :set handle_set(parsed[:letter], parsed[:rows], parsed[:width], network: network, channel: channel, store: store) when :reset handle_reset(parsed[:letter], network: network, channel: channel, store: store) else error_result(usage_message) end rescue ArgumentError => e error_result(e.message) end
Source
# File lib/banner_char_command.rb, line 113 def handle_reset(letter, network:, channel:, store:) char = BannerFontStore.normalize_letter(letter) BannerFontStore.reset_glyph!(char, network: network, channel: channel, store: store) { error: false, reply: BannerFontReport.char_reset(char), multiline: false } end
Source
# File lib/banner_char_command.rb, line 107 def handle_set(letter, rows, width, network:, channel:, store:) char = BannerFontStore.normalize_letter(letter) BannerFontStore.set_glyph!(char, rows, width: width, network: network, channel: channel, store: store) { error: false, reply: BannerFontReport.char_set(char, width: width), multiline: false } end
Source
# File lib/banner_char_command.rb, line 97 def handle_show(letter, network:, channel:, store:) char = BannerFontStore.normalize_letter(letter) glyphs = BannerFontStore.effective_glyphs(network: network, channel: channel, store: store) rows = glyphs[char] || BannerFont::GLYPHS[char] || BannerFont::GLYPHS[" "] width = BannerFont.glyph_width(rows) { error: false, reply: BannerFontReport.char_show(char, rows, width: width), multiline: true } rescue ArgumentError => e error_result(e.message) end
Source
# File lib/banner_char_command.rb, line 23 def parse(args) text = args.to_s.strip return { error: usage_message } if text.empty? parts = text.split(/\s+/) letter = parts[0].to_s.upcase return { error: usage_message } if letter.empty? if parts.length == 1 return { action: :show, letter: letter } end if parts.length == 2 && parts[1].casecmp("reset").zero? return { action: :reset, letter: letter } end if parts.length >= 3 && parts[0].match?(/\A\d+\z/) width = parts[0].to_i set_letter = parts[1].to_s.upcase joined = parts[2..].join(" ") return { error: width_error(width) } unless valid_width?(width) begin rows = split_joined_ascii(joined, width) rescue ArgumentError => e return { error: e.message } end return { action: :set, letter: set_letter, width: width, rows: rows } end { error: usage_message } end
Source
# File lib/banner_char_command.rb, line 62 def split_joined_ascii(text, width) joined = text.to_s.gsub(/\s+/, "") length = joined.length expected_three = 3 * width expected_four = 4 * width unless length == expected_three || length == expected_four raise ArgumentError, "Joined ASCII must be exactly #{expected_three} or #{expected_four} characters (got #{length})." end pattern = /.{#{width}}/ chunks = joined.scan(pattern) end
Source
# File lib/banner_char_command.rb, line 19 def usage_message "Usage: !bannerchar <letter> | !bannerchar <letter> reset | !bannerchar <width> <letter> <joined-ascii>" end
Source
# File lib/banner_char_command.rb, line 119 def valid_width?(width) width >= MIN_WIDTH && width <= MAX_WIDTH end
Source
# File lib/banner_char_command.rb, line 124 def width_error(width) "Width must be #{MIN_WIDTH}–#{MAX_WIDTH} (got #{width})." end