From fef5e3f633e937409b59e2bf9d7e410c615e2b40 Mon Sep 17 00:00:00 2001 From: Jorge Morante Date: Wed, 20 Sep 2023 09:37:17 +0200 Subject: [PATCH] simple style validation --- src/fingers/commands/load_config.cr | 3 +++ src/tmux_style_printer.cr | 16 +++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/fingers/commands/load_config.cr b/src/fingers/commands/load_config.cr index af6162e..0061594 100644 --- a/src/fingers/commands/load_config.cr +++ b/src/fingers/commands/load_config.cr @@ -80,6 +80,9 @@ class Fingers::Commands::LoadConfig < Fingers::Commands::Base config.save Fingers.reset_config + rescue e : TmuxStylePrinter::InvalidFormat + puts "[tmux-fingers] #{e.message}" + exit(1) end def clean_up_patterns(patterns) diff --git a/src/tmux_style_printer.cr b/src/tmux_style_printer.cr index 324f5a0..dffc3ca 100644 --- a/src/tmux_style_printer.cr +++ b/src/tmux_style_printer.cr @@ -1,4 +1,8 @@ class TmuxStylePrinter + + class InvalidFormat < Exception + end + abstract class Shell abstract def exec(cmd) end @@ -70,7 +74,7 @@ class TmuxStylePrinter private def parse_color(style) match = style.match(/(?bg|fg)=(?(colou?r(?[0-9]+)|.*))/) - return "" unless match + raise InvalidFormat.new("Invalid color definition: #{style}") unless match layer = match["layer"] color = match["color"] @@ -81,7 +85,9 @@ class TmuxStylePrinter return reset_to_applied_styles! end - color_to_apply = color_code || COLOR_MAP[color] + color_to_apply = color_code || COLOR_MAP[color]? + + raise InvalidFormat.new("Invalid color definition: #{style}") if color_to_apply.nil? result = shell.exec("tput #{LAYER_MAP[layer]} #{color_to_apply}") @@ -93,11 +99,15 @@ class TmuxStylePrinter private def parse_style(style) match = style.match(/(?no)?(?