Auto justify section headers with custom padding text

Let’s say we have a text file that we want to organize and have section headers. We want the headers to look well indented and visually separate sections. An example might look like this.

---========= DISPLAY CONTENTS OF DATABASES AND TABLES =========---

mysql> show tables;

---======================= MODIFY DATA ========================---

mysql> ALTER TABLE books MODIFY title VARCHAR(50) AFTER book_id;
mysql> UPDATE books SET genre=3 WHERE book_id=1;

Typing the extra = and centering the text might be a laborious process. A short python script could center the text and generate the header.

def f(a):
    print("---"+a.rjust(30+int(len(a)/2),"=").ljust(60,"=")+"---")

f(" SORT ROWS ")
---======================== SORT ROWS =========================---

The function accepts the header string. It first right justifies the string and pads(fills) the extra space in front with =. Then it left justifies and pads the extra space at the end with =.

a.rjust(30+int(len(a)/2),“=”)

Does the main work. If we have a string of length 11 and we want the header to be of length 60, then we need to split the text in half (size ~5), pad the front with 25 = symbols. Since the string has length 11, setting a [width=30+5] will help rjust() pad by (30+5-11=24) symbols (correct number). Left justify is simpler since we know we want the whole header to be of length = 60.

ljust(60,“=”)