-
Notifications
You must be signed in to change notification settings - Fork 53
/
simpletable.py
88 lines (73 loc) · 2.82 KB
/
simpletable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8 -*-
import re
try:
from .helpers import BaseBlockCommand
except ValueError:
from helpers import BaseBlockCommand
class SimpletableCommand(BaseBlockCommand):
_SEPARATOR = ' '
def get_result(self, indent, table):
result = '\n'.join(self._draw_table(indent, table))
result += '\n'
return result
def run(self, edit):
region, lines, indent = self.get_block_bounds()
table = self._parse_table(lines)
result = self.get_result(indent, table)
self.view.replace(edit, region, result)
def _split_table_cells(self, row_string):
return re.split(r'\s\s+', row_string.strip())
def _parse_table(self, raw_lines):
parsed_lines = []
for row_string in raw_lines:
if not self._row_is_separator(row_string):
parsed_lines.append(self._split_table_cells(row_string))
return parsed_lines
def _row_is_separator(self, row):
return re.match('^[\t =]+$', row)
def _table_header_line(self, widths):
linechar = '='
parts = []
for width in widths:
parts.append(linechar * width)
return SimpletableCommand._SEPARATOR.join(parts)
def _get_column_max_widths(self, table):
widths = []
for row in table:
num_fields = len(row)
# dynamically grow
if num_fields >= len(widths):
widths.extend([0] * (num_fields - len(widths)))
for i in range(num_fields):
field_width = len(row[i])
widths[i] = max(widths[i], field_width)
return widths
def _pad_fields(self, row, width_formats):
""" Pad all fields using width formats """
new_row = []
for i in range(len(row)):
col = row[i]
col = width_formats[i] % col
new_row.append(col)
return new_row
def _draw_table(self, indent, table):
if not table:
return []
col_widths = self._get_column_max_widths(table)
# Reserve room for separater
len_sep = len(SimpletableCommand._SEPARATOR)
sep_col_widths = [(col + len_sep) for col in col_widths]
width_formats = [('%-' + str(w) + 's' + SimpletableCommand._SEPARATOR) for w in col_widths]
header_line = self._table_header_line(sep_col_widths)
output = [indent + header_line]
first = True
for row in table:
# draw the lines (num_lines) for this row
row = self._pad_fields(row, width_formats)
output.append(indent + SimpletableCommand._SEPARATOR.join(row))
# draw the under separator for header
if first:
output.append(indent + header_line)
first = False
output.append(indent + header_line)
return output