Last active
March 29, 2019 19:10
-
-
Save iAmGroute/c743839bd757459512100d90551b0510 to your computer and use it in GitHub Desktop.
A simple python class to (adaptively) align text lines based on tab characters ('\t') in real time.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Column 1 Header Col2 Col3 | |
Shorter 1 Col2 Col3 | |
Longer message on column 1 Col2 Col3 | |
Again shorter 1 Col2 Col3 | |
Longer message on column 1 Col2 Col3 | |
Something in between Col2 Col3 | |
Something in between v2 I didn't move ! But now I did | |
Something in between v10 shorter Now I didn't move either | |
Everything in between from now on will be aligned | |
Until you call t.reset() | |
like this, see ? | |
test 1 test 2 | |
test 10 test 20 | |
Use it with unknown length variables like this: | |
Sending request to: example.com and waiting for reply on 123 | |
Sending request to: linux.org and waiting for reply on 123 | |
Sending request to: superlongurl.long.tld.too and waiting for reply on 123 | |
Sending request to: wikipedia.com and waiting for reply on 123 | |
Sending request to: notsolongurl.tld and waiting for reply on 123 | |
Sending request to: github.com and waiting for reply on 123 | |
Sending request to: git-scm.com and waiting for reply on 123 | |
You can also avoid modifying the existing layout by calling t.over() instead of t(). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SmartTabs: | |
def __init__(self): | |
self.columns = [0] | |
def __call__(self, line): | |
cells = line.split('\t') | |
cols = self.columns | |
# Pad as needed or update existing columns | |
for a in range(0, min(len(cells), len(cols))): | |
padding = cols[a] - len(cells[a]) | |
if padding > 0: | |
cells[a] += ' ' * padding | |
else: | |
cols[a] += -padding | |
# Add new columns if needed | |
for b in range(a + 1, len(cells)): | |
cols.append(len(cells[b])) | |
return ''.join(cells) | |
def over(self, line): | |
cells = line.split('\t') | |
cols = self.columns | |
# Pad as needed, but don't update existing columns | |
for a in range(0, min(len(cells), len(cols))): | |
padding = cols[a] - len(cells[a]) | |
if padding > 0: | |
cells[a] += ' ' * padding | |
return ''.join(cells) | |
def reset(self): | |
self.columns = [0] | |
t = SmartTabs() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: | |
from SmartTabs import t | |
print(t('Column 1 Header\t Col2\t Col3')) | |
print(t('Shorter 1\t Col2\t Col3')) | |
print(t('Longer message on column 1\t Col2\t Col3')) | |
print(t('Again shorter 1\t Col2\t Col3')) | |
print(t('Longer message on column 1\t Col2\t Col3')) | |
print(t('Something in between\t Col2\t Col3')) | |
print(t('Something in between v2\t I didn\'t move !\t But now I did')) | |
print(t('Something in between v10\t shorter\t Now I didn\'t move either')) | |
print(t('Everything in between\t from now on\t will be aligned')) | |
print(t('Until you call\t t.reset()')) | |
t.reset() | |
print(t('like this,\t see ?')) | |
print(t('test 1\t test 2')) | |
print(t('test 10\t test 20')) | |
print('Use it with unknown length variables like this:') | |
for url in ['example.com', 'linux.org', 'superlongurl.long.tld.too', 'wikipedia.com', 'notsolongurl.tld', 'github.com', 'git-scm.com']: | |
print(t('Sending request to:\t {0}\t and waiting for reply on {1}'.format(url, 123))) | |
print(t.over('You can also\t avoid modifying the existing layout by calling t.over() instead of t().')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment