From e2c814f7147eeb5eff38392b0f386cfc2dbf2b1b Mon Sep 17 00:00:00 2001 From: Evan Paterakis Date: Fri, 6 Feb 2026 22:00:37 +0200 Subject: [PATCH] feat: copy as markdown table --- data/ui/application.ui | 5 ++++ po/dev.geopjr.Collision.pot | 6 ++++- src/collision/window.cr | 47 ++++++++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/data/ui/application.ui b/data/ui/application.ui index c21122e..396ae3b 100644 --- a/data/ui/application.ui +++ b/data/ui/application.ui @@ -10,6 +10,11 @@ _Compare Hash Functions app.hashinfo + + Copy as _Markdown Table + win.markdown + action-disabled + _Keyboard Shortcuts app.shortcuts diff --git a/po/dev.geopjr.Collision.pot b/po/dev.geopjr.Collision.pot index 5853d60..8c7c580 100644 --- a/po/dev.geopjr.Collision.pot +++ b/po/dev.geopjr.Collision.pot @@ -61,10 +61,14 @@ msgid "_Compare Hash Functions" msgstr "" #: data/ui/application.ui:14 +msgid "Copy as _Markdown Table" +msgstr "" + +#: data/ui/application.ui:19 msgid "_Keyboard Shortcuts" msgstr "" -#: data/ui/application.ui:18 +#: data/ui/application.ui:23 msgid "_About Collision" msgstr "" diff --git a/src/collision/window.cr b/src/collision/window.cr index dd740d3..d2b9999 100644 --- a/src/collision/window.cr +++ b/src/collision/window.cr @@ -52,6 +52,7 @@ module Collision @hash_results = Hash(Symbol, String).new @file_path_queued : Path? = nil + @markdown_action : Gio::SimpleAction property working : Bool = false @@ -82,12 +83,14 @@ module Collision end GLib.idle_add do + @hash_results.clear res.each do |hash_type, hash_value| @hash_results[hash_type] = hash_value @hash_rows[hash_type].subtitle = hash_value.size < 8 ? hash_value : Collision.split_by_4(hash_value) end @mainStack.visible_child_name = "results" + @markdown_action.enabled = true @header_bar.show_title = true @openFileBtn.visible = true @switcher_bar.visible = true @@ -155,6 +158,40 @@ module Collision end end + def on_markdown + table = String.build do |str| + max_k = @hash_results.keys.map { |h| Collision::HASH_FUNCTIONS[h] }.max_by?(&.size).try(&.size) || 3 + max_v = @hash_results.values.max_by?(&.size).try { |m| m.size + 2 } || 3 # codeblock + + str << '|' + "CRF".center(str, max_k, ' ') + str << '|' + "Value".center(str, max_v, ' ') + str << '|' + str << '\n' + + str << '|' + str << "-" * max_k + str << '|' + str << "-" * max_v + str << '|' + str << '\n' + + Collision::HASH_FUNCTIONS.each do |k, v| + next unless @hash_results.keys.includes?(k) + + str << '|' + v.ljust(str, max_k, ' ') + str << '|' + "`#{@hash_results[k]}`".ljust(str, max_v, ' ') + str << '|' + str << '\n' + end + end + + self.clipboard.set(table.strip) + end + def on_drop(file : Gio::File) loading self.file = file @@ -164,6 +201,7 @@ module Collision @progressbar.text = Gettext.gettext("Pending") @progressbar.fraction = 0.0 @mainStack.visible_child_name = "spinner" + @markdown_action.enabled = false @header_bar.show_title = false @openFileBtn.visible = false @switcher_bar.visible = false @@ -181,7 +219,7 @@ module Collision end def handle_input_change(text : String) - result = @hash_results.values.includes?(text.downcase.gsub(' ', "")) + result = @hash_results.values.includes?(text.downcase.delete(' ')) if text.size == 0 @verifyOverlayLabel.visible = true @verifyFeedback.visible = false @@ -286,6 +324,13 @@ module Collision end add_action(file_action) + @markdown_action = Gio::SimpleAction.new("markdown", nil) + @markdown_action.enabled = false + @markdown_action.activate_signal.connect do + on_markdown + end + add_action(@markdown_action) + @hash_row_container = Gtk::ListBox.cast(template_child("hash_row_container")) setup_hashrows