Skip to content

Commit

Permalink
some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenZcience committed Nov 30, 2024
1 parent 1b586d0 commit 5a5d0f5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
29 changes: 13 additions & 16 deletions cat_win/src/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,21 +855,18 @@ def edit_files() -> None:
start = len(u_files)-1 if u_args[ARGS_REVERSE] else 0
end = -1 if u_args[ARGS_REVERSE] else len(u_files)

raw_view_mode = None
if u_args[ARGS_HEXVIEW] or u_args[ARGS_BINVIEW]:
for arg, param in u_args:
if arg == ARGS_HEXVIEW:
raw_view_mode = 'X' if param.isupper() else 'x'
break
if arg == ARGS_BINVIEW:
raw_view_mode = 'b'
break
raw_view_mode = u_args.find_first(ARGS_HEXVIEW, ARGS_BINVIEW)
if raw_view_mode is not None:
raw_view_mode = (
'b' if raw_view_mode[0] == ARGS_BINVIEW else
'X' if raw_view_mode[1].isupper() else 'x'
)

for i in range(start, end, -1 if u_args[ARGS_REVERSE] else 1):
if raw_view_mode:
print_raw_view(i, raw_view_mode)
else:
if raw_view_mode is None:
edit_file(i)
else:
print_raw_view(i, raw_view_mode)
if u_args[ARGS_FILES] or u_args[ARGS_DIRECTORIES]:
print()
if u_args[ARGS_FILES]:
Expand Down Expand Up @@ -1091,11 +1088,11 @@ def handle_args(tmp_file_helper: TmpFileHelper) -> None:
unknown_files = IoHelper.write_files(
unknown_files, piped_input, arg_parser.file_encoding
)
elif u_args[ARGS_EDITOR]:
elif u_args.find_first(ARGS_EDITOR, ARGS_HEX_EDITOR, True) is not None:
unknown_files = [file for file in unknown_files if Editor.open(
file, u_files.get_file_display_name(file), u_args[ARGS_PLAIN_ONLY]
)]
elif u_args[ARGS_HEX_EDITOR]:
elif u_args.find_first(ARGS_HEX_EDITOR, ARGS_EDITOR, True) is not None:
unknown_files = [file for file in unknown_files if HexEditor.open(
file, u_files.get_file_display_name(file)
)]
Expand All @@ -1104,11 +1101,11 @@ def handle_args(tmp_file_helper: TmpFileHelper) -> None:
unknown_files, arg_parser.file_encoding, u_args[ARGS_ONELINE]
)

if u_args[ARGS_EDITOR]:
if u_args.find_first(ARGS_EDITOR, ARGS_HEX_EDITOR, True) is not None:
with IoHelper.dup_stdin(u_args[ARGS_STDIN]):
for file in known_files:
Editor.open(file, u_files.get_file_display_name(file), u_args[ARGS_PLAIN_ONLY])
elif u_args[ARGS_HEX_EDITOR]:
elif u_args.find_first(ARGS_HEX_EDITOR, ARGS_EDITOR, True) is not None:
with IoHelper.dup_stdin(u_args[ARGS_STDIN]):
for file in known_files:
HexEditor.open(file, u_files.get_file_display_name(file))
Expand Down
27 changes: 26 additions & 1 deletion cat_win/src/domain/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Arguments:
about the user defined arguments
"""
def __init__(self) -> None:
self.args: list = [] # list of all used parameters: format [[id, param]]
self.args: list = [] # list of all used parameters: format [(id, param),...]
self.args_id: dict = {}

def set_args(self, args: list) -> None:
Expand Down Expand Up @@ -110,6 +110,31 @@ def delete_args(self, args: list) -> None:
self.args_id = {}
self.set_args(diff_list(self.args, args))

def find_first(self, arg_id1: int, arg_id2: int, is_first: bool = False) -> tuple:
"""
decide if an argument was passed in before another one.
Parameters:
arg_id1 (int):
the first arg to compare
arg_id2 (int):
the second arg to compare
Returns:
(tuple|None):
the argument that was passed in first or None
with is_first == True:
the first argument if it was passed in before the second one else None
"""
if not self[arg_id1] and (not self[arg_id2] or is_first):
return None
for a_id, param in self.args:
if a_id == arg_id1:
return (arg_id1, param)
if a_id == arg_id2:
return None if is_first else (arg_id2, param)
return None

def __getitem__(self, o: int) -> bool:
return self.args_id.get(o, False)

Expand Down

0 comments on commit 5a5d0f5

Please sign in to comment.