Skip to content

Commit

Permalink
added charcount parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenZcience committed Feb 21, 2024
1 parent b61fc78 commit d4206e2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
41 changes: 40 additions & 1 deletion cat_win/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from cat_win.const.argconstants import ARGS_EVAL, ARGS_SORT, ARGS_GREP_ONLY, ARGS_PLAIN_ONLY
from cat_win.const.argconstants import ARGS_FFILE_PREFIX, ARGS_DOTFILES, ARGS_OCT, ARGS_URI
from cat_win.const.argconstants import ARGS_DIRECTORIES, ARGS_DDIRECTORIES, ARGS_SPECIFIC_FORMATS
from cat_win.const.argconstants import ARGS_CHARCOUNT, ARGS_CCHARCOUNT
from cat_win.const.colorconstants import CKW
from cat_win.const.defaultconstants import DKW
from cat_win.persistence.cconfig import CConfig
Expand Down Expand Up @@ -236,7 +237,7 @@ def _show_wordcount() -> None:
print('The word count includes the following files:', end='')
print(color_dic[CKW.RESET_ALL], end='\n\t')
print('\n\t'.join(map(
lambda f: f"{color_dic[CKW.SUMMARY]}{f}{color_dic[CKW.RESET_ALL]}",used_files
lambda f: f"{color_dic[CKW.SUMMARY]}{f}{color_dic[CKW.RESET_ALL]}", used_files
)))
sorted_word_count = sorted(word_count.items(), key=lambda token: token[1], reverse=True)
format_delimeter = f"{color_dic[CKW.RESET_ALL]}:{color_dic[CKW.SUMMARY]} "
Expand All @@ -251,6 +252,38 @@ def _show_wordcount() -> None:
print(color_dic[CKW.RESET_ALL])


def _show_charcount() -> None:
char_count = {}
used_files = []

for hfile in holder.files:
try:
with open(hfile.path, 'r', encoding=arg_parser.file_encoding) as file:
for char in list(file.read()):
char_count[char] = char_count.get(char, 0)+1
used_files.append(hfile.displayname)
except (OSError, UnicodeError):
pass

print(color_dic[CKW.SUMMARY], end='')
print('The char count includes the following files:', end='')
print(color_dic[CKW.RESET_ALL], end='\n\t')
print('\n\t'.join(map(
lambda f: f"{color_dic[CKW.SUMMARY]}{f}{color_dic[CKW.RESET_ALL]}", used_files
)))
sorted_char_count = sorted(char_count.items(), key=lambda token: token[1], reverse=True)
format_delimeter = f"{color_dic[CKW.RESET_ALL]}:{color_dic[CKW.SUMMARY]} "
for _, group in groupby(sorted_char_count, lambda token: token[1]):
sorted_group = sorted(group, key=lambda token: token[0])
formatted_char_count = map(
lambda x: f"{color_dic[CKW.SUMMARY]}{repr(x[0]) if x[0].isspace() else x[0]}"
f"{format_delimeter}{x[1]}{color_dic[CKW.RESET_ALL]}",
sorted_group
)
print('\n' + '\n'.join(formatted_char_count), end='')
print(color_dic[CKW.RESET_ALL])


def _show_sum() -> None:
"""
display the line sum of each file individually if
Expand Down Expand Up @@ -973,6 +1006,9 @@ def edit_files() -> None:
if holder.args_id[ARGS_WORDCOUNT]:
print('')
_show_wordcount()
if holder.args_id[ARGS_CHARCOUNT]:
print('')
_show_charcount()
if holder.args_id[ARGS_SUM]:
print('')
_show_sum()
Expand Down Expand Up @@ -1191,6 +1227,9 @@ def main():
if holder.args_id[ARGS_WWORDCOUNT]:
_show_wordcount()
return
if holder.args_id[ARGS_CCHARCOUNT]:
_show_charcount()
return

edit_files() # print the cat-output

Expand Down
7 changes: 6 additions & 1 deletion cat_win/const/argconstants.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def __init__(self, short_form: str, long_form: str, arg_help: str, arg_id: int,
ARGS_EVAL, ARGS_SORT, ARGS_GREP_ONLY, ARGS_PLAIN_ONLY, ARGS_FILE_PREFIX = range(40, 45)
ARGS_FFILE_PREFIX, ARGS_DOTFILES, ARGS_OCT, ARGS_URI, ARGS_WORDCOUNT = range(45, 50)
ARGS_WWORDCOUNT, ARGS_DIRECTORIES, ARGS_DDIRECTORIES = range(50, 53)
ARGS_SPECIFIC_FORMATS, ARGS_CONFIG = range(53, 55)
ARGS_SPECIFIC_FORMATS, ARGS_CONFIG, ARGS_CHARCOUNT = range(53, 56)
ARGS_CCHARCOUNT, = range(56, 57)

DIFFERENTIABLE_ARGS = [ARGS_CUT, ARGS_REPLACE]

Expand Down Expand Up @@ -99,6 +100,10 @@ def __init__(self, short_form: str, long_form: str, arg_help: str, arg_id: int,
ARGS_WORDCOUNT, show_arg_on_shell=False, section=5),
ArgConstant('-W', '--WORDCOUNT', 'ONLY display the wordcount',
ARGS_WWORDCOUNT, show_arg=False, section=5),
ArgConstant('--cc', '--charcount', 'display the charcount',
ARGS_CHARCOUNT, show_arg_on_shell=False, section=5),
ArgConstant('--CC', '--CHARCOUNT', 'ONLY display the charcount',
ARGS_CCHARCOUNT, show_arg=False, section=5),

# search and match
ArgConstant('-g', '--grep', 'only show lines containing queried keywords or patterns',
Expand Down

0 comments on commit d4206e2

Please sign in to comment.