This commit is contained in:
dingfeng.wong
2025-07-22 14:27:09 +08:00
parent 4a81b1617b
commit 75a8770bf8
2 changed files with 97 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
# cli decode
def cli_decode(base64_encoded_script: str) -> str:
return f"echo '{base64_encoded_script}' | base64 -d | gunzip "
# source now
def source_now(base64_encoded_script: str) -> str:
return f"source <({cli_decode(base64_encoded_script)})"
better_env = """
export TERM=xterm
export PS1="\[\e[0;32m\]\u@\h (\$(ip route get 8.8.8.8 | sed -n '/src/{s/.*src \([^ ]*\).*/\\1/p;q}')):\w\$ \[\e[0m\]"
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 -f "$LAST_MODIFIED_FILE" "$@";
else
echo "No files found in the current directory to tail.";
fi
}; _tl_func'
echo "Bash customizations sourced successfully!"
"""
+66
View File
@@ -0,0 +1,66 @@
"""
Utility functions for string compression and encoding.
This module provides functions for compressing strings using gzip
and encoding them with base64 for efficient storage and transmission.
"""
import base64
import gzip
from typing import Final
def compress_string(text: str, *, encoding: str = "utf-8") -> bytes:
"""
Compress a string using gzip with maximum compression.
Args:
text: The string to compress
encoding: The text encoding to use (default: utf-8)
Returns:
The compressed data as bytes
Raises:
UnicodeEncodeError: If the text cannot be encoded with the specified encoding
"""
text_bytes: bytes = text.encode(encoding)
return gzip.compress(text_bytes, compresslevel=9)
def encode_base64(data: bytes) -> str:
"""
Encode bytes to a base64 string.
Args:
data: The bytes to encode
Returns:
The base64-encoded string
"""
return base64.b64encode(data).decode("ascii")
def compress_and_encode(text: str, *, encoding: str = "utf-8") -> str:
"""
Compress a string with gzip and encode it as base64.
This is a convenience function that combines compress_string() and encode_base64().
Args:
text: The string to compress and encode
encoding: The text encoding to use (default: utf-8)
Returns:
The compressed and base64-encoded string
Raises:
UnicodeEncodeError: If the text cannot be encoded with the specified encoding
"""
compressed_data: bytes = compress_string(text, encoding=encoding)
return encode_base64(compressed_data)
# Constants for common use cases
MAX_COMPRESSION_LEVEL: Final[int] = 9
DEFAULT_ENCODING: Final[str] = "utf-8"