|
| 1 | +from pyfiglet import Figlet, FontNotFound |
| 2 | +from termcolor import colored |
| 3 | + |
| 4 | +try: |
| 5 | + from importlib.metadata import version |
| 6 | +except ImportError: |
| 7 | + from importlib_metadata import version |
| 8 | + |
| 9 | + |
| 10 | +def generate_banner( |
| 11 | + title="Agentic Security", |
| 12 | + font="slant", |
| 13 | + version="v2.1.0", |
| 14 | + tagline="Proactive Threat Detection & Automated Security Protocols", |
| 15 | + author="Developed by: [Security Team]", |
| 16 | + website="Website: https://github.com/msoedov/agentic_security", |
| 17 | + warning="", |
| 18 | +): |
| 19 | + """Generate a visually enhanced banner with dynamic width and borders.""" |
| 20 | + # Define the text elements |
| 21 | + |
| 22 | + # Initialize Figlet with the specified font, fallback to default if not found |
| 23 | + try: |
| 24 | + f = Figlet(font=font) |
| 25 | + except FontNotFound: |
| 26 | + f = Figlet() # Fallback to default font |
| 27 | + |
| 28 | + # Render the title text and calculate the maximum width of Figlet lines |
| 29 | + banner_text = f.renderText(title) |
| 30 | + banner_lines = banner_text.splitlines() |
| 31 | + figlet_max_width = max(len(line) for line in banner_lines) if banner_lines else 0 |
| 32 | + |
| 33 | + # Create the details line and calculate its width |
| 34 | + details_line = f"Version: {version} | {website}" |
| 35 | + details_width = len(details_line) |
| 36 | + |
| 37 | + # Calculate widths of other text elements |
| 38 | + warning_width = len(warning) |
| 39 | + tagline_width = len(tagline) |
| 40 | + |
| 41 | + # Determine the overall maximum width for centering |
| 42 | + overall_max_width = max( |
| 43 | + figlet_max_width, warning_width, tagline_width, details_width |
| 44 | + ) |
| 45 | + |
| 46 | + # Pad the Figlet lines to the overall maximum width |
| 47 | + padded_banner_lines = [line.center(overall_max_width) for line in banner_lines] |
| 48 | + |
| 49 | + # Define decorative characters and colors |
| 50 | + decor_chars = ["▄", "■", "►"] |
| 51 | + decor_colors = ["blue", "red", "yellow"] |
| 52 | + |
| 53 | + # Create and color the content lines |
| 54 | + content_lines = [] |
| 55 | + for line in padded_banner_lines: |
| 56 | + content_lines.append(colored(line, "blue")) |
| 57 | + content_lines.append(colored(decor_chars[0] * overall_max_width, decor_colors[0])) |
| 58 | + content_lines.append( |
| 59 | + colored(warning.center(overall_max_width), "red", attrs=["blink", "bold"]) |
| 60 | + ) |
| 61 | + content_lines.append(colored(decor_chars[1] * overall_max_width, decor_colors[1])) |
| 62 | + content_lines.append(colored(tagline.center(overall_max_width), "red")) |
| 63 | + content_lines.append(colored(decor_chars[2] * overall_max_width, decor_colors[2])) |
| 64 | + content_lines.append(colored(details_line.center(overall_max_width), "magenta")) |
| 65 | + |
| 66 | + # Define border color and create top and bottom borders |
| 67 | + border_color = "blue" |
| 68 | + top_border = colored("╔" + "═" * (overall_max_width + 2) + "╗", border_color) |
| 69 | + bottom_border = colored("╚" + "═" * (overall_max_width + 2) + "╝", border_color) |
| 70 | + |
| 71 | + # Add side borders to each content line with padding |
| 72 | + bordered_content = [ |
| 73 | + colored("║ ", border_color) + line + colored(" ║", border_color) |
| 74 | + for line in content_lines |
| 75 | + ] |
| 76 | + |
| 77 | + # Assemble the full banner |
| 78 | + banner = top_border + "\n" + "\n".join(bordered_content) + "\n" + bottom_border |
| 79 | + return banner |
| 80 | + |
| 81 | + |
| 82 | +def init_banner(): |
| 83 | + ver = version("agentic_security") |
| 84 | + print(generate_banner(version=ver)) |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + init_banner() |
0 commit comments