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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
ASCII Comment‑Banner Generator (Gradio)
|
| 3 |
+
|
| 4 |
+
Features
|
| 5 |
+
--------
|
| 6 |
+
- Choose any comment style (full list from the supplied table)
|
| 7 |
+
- Set total width (characters) and total height (lines)
|
| 8 |
+
- Horizontal alignment : left / centre / right
|
| 9 |
+
- Vertical alignment : top / centre / bottom
|
| 10 |
+
- Border filler : '-', '=', '^' or any custom string
|
| 11 |
+
- Returns a ready‑to‑paste multi‑line comment banner.
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
import gradio as gr
|
| 15 |
+
from itertools import cycle
|
| 16 |
+
|
| 17 |
+
# ----------------------------------------------------------------------
|
| 18 |
+
# 1️⃣ Mapping from UI‑friendly names → (start_delim, end_delim)
|
| 19 |
+
# ----------------------------------------------------------------------
|
| 20 |
+
COMMENT_STYLES: dict[str, tuple[str, str]] = {
|
| 21 |
+
# ----- single‑line comment families ---------------------------------
|
| 22 |
+
"# (sh, Python, Ruby, etc.)": ("#", "#"),
|
| 23 |
+
"// (C‑family, JavaScript, Go, Rust…)": ("//", "//"),
|
| 24 |
+
"; (MASM/TASM, Lisp families)": (";", ";"),
|
| 25 |
+
"-- (SQL, Haskell, Ada, VHDL…)": ("--", "--"),
|
| 26 |
+
"% (MATLAB/Octave, LaTeX)": ("%", "%"),
|
| 27 |
+
"' (VB, VBA, VB.NET)": ("'", "'"),
|
| 28 |
+
"` (legacy shell)": ("`", "`"),
|
| 29 |
+
"REM (Batch files)": ("REM", "REM"),
|
| 30 |
+
":: (Batch hack, works in PowerShell)": ("::", "::"),
|
| 31 |
+
";; (Racket, Scheme, Clojure read‑time comment)": (";;", ";;"),
|
| 32 |
+
|
| 33 |
+
# ----- block‑only comment families ---------------------------------
|
| 34 |
+
"<!-- … --> (HTML / XML)": ("<!--", "-->"),
|
| 35 |
+
"/* … */ (C‑style block)": ("/*", "*/"),
|
| 36 |
+
"\"\"\" … \"\"\" (Python doc‑string, Elixir multi‑line string)": ('"""', '"""'),
|
| 37 |
+
"--[[ … ]]" + " (Lua block comment)": ("--[[", "]]"),
|
| 38 |
+
"#= … =# (Julia block comment)": ("#=", "=#"),
|
| 39 |
+
"#| … |# (Clojure / CLisp / Racket block)": ("#|", "|#"),
|
| 40 |
+
"<# … #> (PowerShell block comment)": ("<#", "#>"),
|
| 41 |
+
"=begin … =end (Ruby block comment)": ("=begin", "=end"),
|
| 42 |
+
|
| 43 |
+
# ----- languages that have both line‑ and block‑styles -------------
|
| 44 |
+
"# (Python line comment)": ("#", "#"),
|
| 45 |
+
"\"\"\" (Python triple‑quote doc‑string)": ('"""', '"""'),
|
| 46 |
+
"# (Ruby line comment)": ("#", "#"),
|
| 47 |
+
"=begin/=end (Ruby block comment)": ("=begin", "=end"),
|
| 48 |
+
"REM (PowerShell line comment)": ("#", "#"),
|
| 49 |
+
"<# … #> (PowerShell block comment)": ("<#", "#>"),
|
| 50 |
+
|
| 51 |
+
# ----- extra / special cases ---------------------------------------
|
| 52 |
+
"' (SQL single‑quote comment – rarely used)": ("'", "'"),
|
| 53 |
+
"-- (Ada line comment)": ("--", "--"),
|
| 54 |
+
"-- (COBOL comment – column 7)": ("--", "--"),
|
| 55 |
+
"` (Tcl comment – back‑tick rarely used)": ("`", "`"),
|
| 56 |
+
"/* … */ (CSS / Less / Sass SCSS)": ("/*", "*/"),
|
| 57 |
+
"/* … */ (GraphQL SDL – same as C‑style)": ("/*", "*/"),
|
| 58 |
+
"# (PowerShell line comment – alias for '--')": ("#", "#"),
|
| 59 |
+
|
| 60 |
+
# ----- more block‑style families (HTML‑like) -----------------------
|
| 61 |
+
"<!-- … --> (Markdown – HTML comment inside MD)": ("<!--", "-->"),
|
| 62 |
+
"<!-- … --> (Asciidoc comment)": ("<!--", "-->"),
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
# ----------------------------------------------------------------------
|
| 66 |
+
# 2️⃣ Helper – repeat a pattern so it exactly matches a given length
|
| 67 |
+
# ----------------------------------------------------------------------
|
| 68 |
+
def _repeat_pattern(pattern: str, length: int) -> str:
|
| 69 |
+
"""Return *pattern* repeated/cycled until *length* characters are filled."""
|
| 70 |
+
if not pattern:
|
| 71 |
+
pattern = "-" # safe fallback
|
| 72 |
+
return "".join(c for _, c in zip(range(length), cycle(pattern)))
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# ----------------------------------------------------------------------
|
| 76 |
+
# 3️⃣ Core – create the banner
|
| 77 |
+
# ----------------------------------------------------------------------
|
| 78 |
+
def make_banner(
|
| 79 |
+
text: str,
|
| 80 |
+
width: int = 80,
|
| 81 |
+
height: int = 5,
|
| 82 |
+
h_align: str = "both", # left / both / right
|
| 83 |
+
v_align: str = "both", # top / both / bottom
|
| 84 |
+
style: str = "# (sh, Python, Ruby, etc.)",
|
| 85 |
+
filler: str = "-", # what to repeat on the top/bottom border
|
| 86 |
+
) -> str:
|
| 87 |
+
"""
|
| 88 |
+
Build a comment banner.
|
| 89 |
+
|
| 90 |
+
Parameters
|
| 91 |
+
----------
|
| 92 |
+
text, width, height, h_align, v_align, style – see docstring above.
|
| 93 |
+
filler : str
|
| 94 |
+
String that will be repeated (cycled) on the top and bottom border.
|
| 95 |
+
Any length is accepted.
|
| 96 |
+
"""
|
| 97 |
+
# ------------------- 1️⃣ Resolve comment delimiters -------------------
|
| 98 |
+
try:
|
| 99 |
+
start, end = COMMENT_STYLES[style]
|
| 100 |
+
except KeyError as exc:
|
| 101 |
+
raise ValueError(f"Unsupported comment style: {style!r}") from exc
|
| 102 |
+
|
| 103 |
+
# ------------------- 2️⃣ Usable width ---------------------------------
|
| 104 |
+
# pattern: <start>␣<content>␣<end>
|
| 105 |
+
usable = width - (len(start) + len(end) + 4) # 4 = two spaces + two delimiters
|
| 106 |
+
if usable < 1: # safety – enlarge if needed
|
| 107 |
+
usable = 1
|
| 108 |
+
width = len(start) + len(end) + 5
|
| 109 |
+
|
| 110 |
+
# ------------------- 3️⃣ Build line types ----------------------------
|
| 111 |
+
border_line = f"{start} " + _repeat_pattern(filler, usable) + f" {end}"
|
| 112 |
+
|
| 113 |
+
# horizontal alignment of the inner text
|
| 114 |
+
if h_align == "left":
|
| 115 |
+
inner = text.ljust(usable)
|
| 116 |
+
elif h_align == "right":
|
| 117 |
+
inner = text.rjust(usable)
|
| 118 |
+
else: # centre (default)
|
| 119 |
+
inner = text.center(usable)
|
| 120 |
+
|
| 121 |
+
text_line = f"{start} " + inner + f" {end}"
|
| 122 |
+
empty_line = f"{start} " + (" " * usable) + f" {end}"
|
| 123 |
+
|
| 124 |
+
# ------------------- 4️⃣ Vertical padding ---------------------------
|
| 125 |
+
if height < 3:
|
| 126 |
+
height = 3
|
| 127 |
+
pad_total = height - 3 # lines that are not border / text
|
| 128 |
+
|
| 129 |
+
if v_align == "top":
|
| 130 |
+
pad_top, pad_bottom = 0, pad_total
|
| 131 |
+
elif v_align == "bottom":
|
| 132 |
+
pad_top, pad_bottom = pad_total, 0
|
| 133 |
+
else: # centre (default)
|
| 134 |
+
pad_top = pad_total // 2
|
| 135 |
+
pad_bottom = pad_total - pad_top
|
| 136 |
+
|
| 137 |
+
# ------------------- 5️⃣ Assemble -------------------------------
|
| 138 |
+
lines = [border_line] # top border
|
| 139 |
+
lines += [empty_line] * pad_top # upper padding
|
| 140 |
+
lines.append(text_line) # caption line
|
| 141 |
+
lines += [empty_line] * pad_bottom # lower padding
|
| 142 |
+
lines.append(border_line) # bottom border
|
| 143 |
+
|
| 144 |
+
return "\n".join(lines)
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
# ----------------------------------------------------------------------
|
| 148 |
+
# 4️⃣ Helper – decide which filler string to actually use
|
| 149 |
+
# ----------------------------------------------------------------------
|
| 150 |
+
def _resolve_filler(preset: str, custom: str) -> str:
|
| 151 |
+
"""Return the string that should be repeated on the border."""
|
| 152 |
+
if preset == "custom":
|
| 153 |
+
return custom or "-" # fall back to dash if custom empty
|
| 154 |
+
return preset # '-', '=', '^', …
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
# ----------------------------------------------------------------------
|
| 158 |
+
# 5️⃣ Gradio UI
|
| 159 |
+
# ----------------------------------------------------------------------
|
| 160 |
+
# First we build the component list – this is needed so we can refer to it
|
| 161 |
+
# later when we wrap the function that handles the custom filler.
|
| 162 |
+
input_components = [
|
| 163 |
+
gr.Textbox(label="Banner text",
|
| 164 |
+
placeholder="Enter your caption here…",
|
| 165 |
+
lines=1),
|
| 166 |
+
|
| 167 |
+
gr.Slider(minimum=20, maximum=200, step=2, value=80,
|
| 168 |
+
label="Total width (characters)"),
|
| 169 |
+
|
| 170 |
+
gr.Slider(minimum=3, maximum=30, step=1, value=5,
|
| 171 |
+
label="Total height (lines)"),
|
| 172 |
+
|
| 173 |
+
gr.Dropdown(choices=["left", "both", "right"],
|
| 174 |
+
value="both",
|
| 175 |
+
label="Horizontal alignment"),
|
| 176 |
+
|
| 177 |
+
gr.Dropdown(choices=["top", "both", "bottom"],
|
| 178 |
+
value="both",
|
| 179 |
+
label="Vertical alignment"),
|
| 180 |
+
|
| 181 |
+
gr.Dropdown(choices=sorted(COMMENT_STYLES.keys()),
|
| 182 |
+
value="# (sh, Python, Ruby, etc.)",
|
| 183 |
+
label="Comment style"),
|
| 184 |
+
|
| 185 |
+
gr.Dropdown(choices=["-", "=", "^", "custom"],
|
| 186 |
+
value="-",
|
| 187 |
+
label="Border filler (preset)"),
|
| 188 |
+
|
| 189 |
+
gr.Textbox(label="Custom filler (used only when preset = ‘custom’)",
|
| 190 |
+
placeholder="e.g. *~* – leave empty for default ‘-’",
|
| 191 |
+
lines=1),
|
| 192 |
+
]
|
| 193 |
+
|
| 194 |
+
output_component = gr.Textbox(label="Generated ASCII banner",
|
| 195 |
+
lines=14,
|
| 196 |
+
interactive=False)
|
| 197 |
+
|
| 198 |
+
def _gradio_wrapper(
|
| 199 |
+
text, width, height,
|
| 200 |
+
h_align, v_align, style,
|
| 201 |
+
filler_preset, filler_custom
|
| 202 |
+
):
|
| 203 |
+
"""Wrapper called by Gradio – resolves the custom filler first."""
|
| 204 |
+
filler = _resolve_filler(filler_preset, filler_custom)
|
| 205 |
+
return make_banner(
|
| 206 |
+
text=text,
|
| 207 |
+
width=width,
|
| 208 |
+
height=height,
|
| 209 |
+
h_align=h_align,
|
| 210 |
+
v_align=v_align,
|
| 211 |
+
style=style,
|
| 212 |
+
filler=filler,
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
demo = gr.Interface(
|
| 216 |
+
fn=_gradio_wrapper,
|
| 217 |
+
inputs=input_components,
|
| 218 |
+
outputs=output_component,
|
| 219 |
+
title="🖼️ ASCII Comment‑Banner Generator",
|
| 220 |
+
description=(
|
| 221 |
+
"Create ready‑to‑paste comment blocks for dozens of programming languages. "
|
| 222 |
+
"Pick width, height, horizontal & vertical alignment, any comment style, "
|
| 223 |
+
"and the character(s) that will form the top/bottom border (preset ‘-’, ‘=’, ‘^’, "
|
| 224 |
+
"or a custom string)."
|
| 225 |
+
),
|
| 226 |
+
examples=[
|
| 227 |
+
# text w h h‑align v‑align style filler preset custom
|
| 228 |
+
[
|
| 229 |
+
"DATA & TRAINING CONFIGURATION PROCESSING",
|
| 230 |
+
80, 5, "both", "both", "# (sh, Python, Ruby, etc.)", "-", "",
|
| 231 |
+
],
|
| 232 |
+
[
|
| 233 |
+
"WELCOME TO MY PROJECT",
|
| 234 |
+
60, 7, "left", "top", "// (C‑family, JavaScript, Go, Rust…)", "=", "",
|
| 235 |
+
],
|
| 236 |
+
[
|
| 237 |
+
"⚡️ QUICK START",
|
| 238 |
+
70, 4, "right", "bottom", "/* … */ (C‑style block)", "^", "",
|
| 239 |
+
],
|
| 240 |
+
[
|
| 241 |
+
"CUSTOM FILLER EXAMPLE",
|
| 242 |
+
70, 5, "both", "both", "# (sh, Python, Ruby, etc.)", "custom", "*~*",
|
| 243 |
+
],
|
| 244 |
+
[
|
| 245 |
+
"HTML HEADER",
|
| 246 |
+
70, 5, "both", "top", "<!-- … --> (HTML / XML)", "-", "",
|
| 247 |
+
],
|
| 248 |
+
[
|
| 249 |
+
"POWER‑SHELL MODULE",
|
| 250 |
+
70, 5, "both", "bottom", "<# … #> (PowerShell block comment)", "-", "",
|
| 251 |
+
],
|
| 252 |
+
],
|
| 253 |
+
allow_flagging="never",
|
| 254 |
+
)
|
| 255 |
+
|
| 256 |
+
if __name__ == "__main__":
|
| 257 |
+
# `share=True` will give you a public URL (optional)
|
| 258 |
+
demo.launch()
|