This commit is contained in:
dingfeng.wong
2025-07-22 14:55:54 +08:00
parent c7cc85d1e8
commit 75a936ef29
+61 -9
View File
@@ -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!"
'''