From 72e238cc8938f4114b3909b7c0e7bb310b017d87 Mon Sep 17 00:00:00 2001
From: Silas Kraume <silas.kraume1552@gmail.com>
Date: Thu, 22 Feb 2024 18:03:58 +0100
Subject: [PATCH] added sort by line length functionality

---
 cat_win/cat.py                | 3 ++-
 cat_win/const/argconstants.py | 2 +-
 cat_win/util/argparser.py     | 2 +-
 3 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/cat_win/cat.py b/cat_win/cat.py
index cb32f732..f297cab4 100644
--- a/cat_win/cat.py
+++ b/cat_win/cat.py
@@ -752,7 +752,8 @@ def edit_content(content: list, show_bytecode: bool, file_index: int = 0,
             elif arg == ARGS_REVERSE:
                 content.reverse()
             elif arg == ARGS_SORT:
-                content.sort(key = lambda l: l[1].casefold())
+                sort_method = len if param.isupper() else str.casefold
+                content.sort(key = lambda l: sort_method(l[1]))
             elif arg == ARGS_BLANK:
                 content = [c for c in content if c[1]]
             elif arg == ARGS_EVAL:
diff --git a/cat_win/const/argconstants.py b/cat_win/const/argconstants.py
index fb46be87..d470b82f 100644
--- a/cat_win/const/argconstants.py
+++ b/cat_win/const/argconstants.py
@@ -68,7 +68,7 @@ def __init__(self, short_form: str, long_form: str, arg_help: str, arg_id: int,
 				ARGS_REVERSE, show_arg_on_shell=False, section=3),
     ArgConstant('-u', '--unique', 'suppress repeated output lines',
 				ARGS_SQUEEZE, show_arg_on_shell=False, section=3),
-    ArgConstant('--sort', '--sort', 'sort all lines alphabetically',
+    ArgConstant('--sort', '--SORT', 'sort all lines alphabetically or by length',
 				ARGS_SORT, show_arg_on_shell=False, section=3),
     ArgConstant('--sf', '--specific-format', 'automatically format specific file types',
                 ARGS_SPECIFIC_FORMATS, show_arg_on_shell=False, section=3),
diff --git a/cat_win/util/argparser.py b/cat_win/util/argparser.py
index e5983dcc..3534d39a 100644
--- a/cat_win/util/argparser.py
+++ b/cat_win/util/argparser.py
@@ -144,7 +144,7 @@ def check_unknown_args(self, shell_arg: bool = False) -> list:
 # print(leven_short.__round__(3), leven_long.__round__(3),
 #       max(leven_short, leven_long).__round__(3), u_arg, arg.long_form, sep="\t") # DEBUG
                 if max(leven_short, leven_long) > self.SIMILARITY_LIMIT:
-                    if leven_short > leven_long:
+                    if leven_short >= leven_long:
                         possible_arg_replacement[1].append((arg.short_form, leven_short))
                     else:
                         possible_arg_replacement[1].append((arg.long_form, leven_long))