icat: Add a command line argument for horizontal alignment

This commit is contained in:
Kovid Goyal
2017-12-15 13:08:27 +05:30
parent ddb2e41352
commit cb04deef63
2 changed files with 18 additions and 9 deletions

View File

@@ -227,7 +227,7 @@ def display_bitmap(rgb_data, width, height):
with NamedTemporaryFile(suffix='.rgba', delete=False) as f: with NamedTemporaryFile(suffix='.rgba', delete=False) as f:
f.write(rgb_data) f.write(rgb_data)
assert len(rgb_data) == 4 * width * height assert len(rgb_data) == 4 * width * height
show(f.name, width, height, 32) show(f.name, width, height, 32, align='left')
def test_render_string(text='Hello, world!', family='monospace', size=64.0, dpi=96.0): def test_render_string(text='Hello, world!', family='monospace', size=64.0, dpi=96.0):

View File

@@ -41,6 +41,11 @@ class OpenFailed(ValueError):
OPTIONS = '''\ OPTIONS = '''\
--align
type=choices
choices=center,left,right
default=center
Horizontal alignment for the displayed image. Defaults to centered.
''' '''
@@ -84,7 +89,7 @@ def fit_image(width, height, pwidth, pheight):
return int(width), int(height) return int(width), int(height)
def set_cursor(cmd, width, height): def set_cursor(cmd, width, height, align):
ss = screen_size() ss = screen_size()
cw = int(ss.width / ss.cols) cw = int(ss.width / ss.cols)
num_of_cells_needed = int(ceil(width / cw)) num_of_cells_needed = int(ceil(width / cw))
@@ -98,7 +103,11 @@ def set_cursor(cmd, width, height):
else: else:
x_off = width % cw x_off = width % cw
cmd['X'] = x_off cmd['X'] = x_off
extra_cells = (ss.cols - num_of_cells_needed) // 2 extra_cells = 0
if align == 'center':
extra_cells = (ss.cols - num_of_cells_needed) // 2
elif align == 'right':
extra_cells = (ss.cols - num_of_cells_needed)
if extra_cells: if extra_cells:
sys.stdout.buffer.write(b' ' * extra_cells) sys.stdout.buffer.write(b' ' * extra_cells)
@@ -116,9 +125,9 @@ def write_chunked(cmd, data):
cmd.clear() cmd.clear()
def show(outfile, width, height, fmt, transmit_mode='t'): def show(outfile, width, height, fmt, transmit_mode='t', align='center'):
cmd = {'a': 'T', 'f': fmt, 's': width, 'v': height} cmd = {'a': 'T', 'f': fmt, 's': width, 'v': height}
set_cursor(cmd, width, height) set_cursor(cmd, width, height, align)
if detect_support.has_files: if detect_support.has_files:
cmd['t'] = transmit_mode cmd['t'] = transmit_mode
write_gr_cmd(cmd, standard_b64encode(os.path.abspath(outfile).encode(fsenc))) write_gr_cmd(cmd, standard_b64encode(os.path.abspath(outfile).encode(fsenc)))
@@ -161,7 +170,7 @@ def convert(path, m, ss):
return outfile.name, width, height return outfile.name, width, height
def process(path): def process(path, args):
m = identify(path) m = identify(path)
ss = screen_size() ss = screen_size()
needs_scaling = m.width > ss.width needs_scaling = m.width > ss.width
@@ -174,7 +183,7 @@ def process(path):
fmt = 24 if m.mode == 'rgb' else 32 fmt = 24 if m.mode == 'rgb' else 32
transmit_mode = 't' transmit_mode = 't'
outfile, width, height = convert(path, m, ss) outfile, width, height = convert(path, m, ss)
show(outfile, width, height, fmt, transmit_mode) show(outfile, width, height, fmt, transmit_mode, align=args.align)
print() # ensure cursor is on a new line print() # ensure cursor is on a new line
@@ -257,9 +266,9 @@ def main(args=sys.argv):
try: try:
if os.path.isdir(item): if os.path.isdir(item):
for x in scan(item): for x in scan(item):
process(item) process(item, args)
else: else:
process(item) process(item, args)
except OpenFailed as e: except OpenFailed as e:
errors.append(e) errors.append(e)
if not errors: if not errors: