From 75a936ef2975bc4032cb1262b946b89725fed1bc Mon Sep 17 00:00:00 2001 From: "dingfeng.wong" Date: Tue, 22 Jul 2025 14:55:54 +0800 Subject: [PATCH] a --- src/elf/better_bash.py | 70 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 9 deletions(-) diff --git a/src/elf/better_bash.py b/src/elf/better_bash.py index d0367d2..1e544d3 100644 --- a/src/elf/better_bash.py +++ b/src/elf/better_bash.py @@ -56,20 +56,72 @@ def build_ps1_prompt() -> str: return f'{green}{user_host} ($({local_ip_cmd})):{work_dir}{prompt_symbol} {reset}' +# Alias building utilities +def create_bash_function(func_name: str, func_body: str) -> str: + """ + Creates a bash function with the given name and body. + The function body should be properly formatted bash code. + """ + return f'{func_name}() {{\n{func_body}\n}}' + +def create_alias_with_function(alias_name: str, func_name: str, func_body: str) -> str: + """ + Creates a bash alias that defines and immediately calls a function. + This pattern allows for complex logic within aliases. + """ + function_def = create_bash_function(func_name, func_body) + return f"alias {alias_name}='{function_def}; {func_name}'" + +# File finding utilities +def find_last_modified_file_command() -> str: + """ + Returns a command that finds the last modified file in current directory. + Uses find with printf to get modification time, sorts by time, and gets the newest. + """ + return 'find . -maxdepth 1 -type f -printf "%T@ %p\\0" | sort -znr | head -zn1 | cut -d" " -f2- --zero-terminated' + +def create_conditional_message(condition: str, success_msg: str, failure_msg: str) -> str: + """ + Creates a bash conditional that shows different messages based on a condition. + """ + return f'''if [ {condition} ]; then + {success_msg} + else + {failure_msg} + fi''' + +# Tail last modified file alias components +def get_tail_last_logic() -> str: + """ + Creates the logic for tailing the last modified file in current directory. + """ + find_cmd = find_last_modified_file_command() + success_action = '''echo "Tailing: \\"$LAST_MODIFIED_FILE\\""; + tail "$@" "$LAST_MODIFIED_FILE"''' + failure_action = 'echo "No files found in the current directory to tail."' + + conditional = create_conditional_message( + '-n "$LAST_MODIFIED_FILE"', + success_action, + failure_action + ) + + return f'''LAST_MODIFIED_FILE=$({find_cmd}); + {conditional}''' + +def build_tail_last_alias() -> str: + """ + Builds the 'tl' alias that tails the last modified file in current directory. + """ + func_body = get_tail_last_logic() + return create_alias_with_function('tl', '_tl_func', func_body) + better_env = f''' export TERM=xterm export PS1="{build_ps1_prompt()}" # Tail the last modified file in current directory -alias tl='_tl_func() {{ - LAST_MODIFIED_FILE=$(find . -maxdepth 1 -type f -printf "%T@ %p\0" | sort -znr | head -zn1 | cut -d" " -f2- --zero-terminated); - if [ -n "$LAST_MODIFIED_FILE" ]; then - echo "Tailing: \"$LAST_MODIFIED_FILE\""; - tail "$@" "$LAST_MODIFIED_FILE"; - else - echo "No files found in the current directory to tail."; - fi -}}; _tl_func' +{build_tail_last_alias()} echo "Bash customizations sourced successfully!" '''