Skip to content

Commit

Permalink
Adds width attribute which specifies number of bytes printed per line
Browse files Browse the repository at this point in the history
  • Loading branch information
Utkarsh1308 committed Jun 3, 2019
1 parent 8617dde commit 4f9302d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 17 deletions.
39 changes: 26 additions & 13 deletions multidiff/Render.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from multidiff.Ansi import Ansi
import binascii
import html
import textwrap

class Render():
def __init__(self, encoder='hexdump', color='ansi'):
def __init__(self, encoder='hexdump', color='ansi', width=None):
'''Configure the output encoding and coloring method of this rendering object'''
if color == 'ansi':
self.highligther = ansi_colored
Expand All @@ -16,17 +17,19 @@ def __init__(self, encoder='hexdump', color='ansi'):
self.encoder = HexEncoder
elif encoder == 'utf8':
self.encoder = Utf8Encoder


self.width = width

def render(self, model, diff):
'''Render the diff in the given model into a UTF-8 String'''
result = self.encoder(self.highligther)
obj = model.objects[diff.target]
for op in diff.opcodes:
data = obj.data[op[3]:op[4]]
if type(data) == bytes:
result.append(data, op[0])
result.append(data, op[0], self.width)
elif type(data) == str:
result.append(bytes(data, "utf8"), op[0])
result.append(bytes(data, "utf8"), op[0], self.width)
return result.final()

def dumps(self, model):
Expand All @@ -42,9 +45,12 @@ def __init__(self, highligther):
self.highligther = highligther
self.output = ''

def append(self, data, color):
def append(self, data, color, width=None):
self.output += self.highligther(str(data, 'utf8'), color)

if width:
if len(self.output) > int(width):
self.output = textwrap.fill(self.output, int(width))

def final(self):
return self.output

Expand All @@ -53,10 +59,14 @@ class HexEncoder():
def __init__(self, highligther):
self.highligther = highligther
self.output = ''
def append(self, data, color):

def append(self, data, color, width=None):
data = str(binascii.hexlify(data),'utf8')
self.output += self.highligther(data, color)

if width:
if len(self.output) > int(width):
self.output = textwrap.fill(self.output, int(width))

def final(self):
return self.output

Expand All @@ -71,16 +81,16 @@ def __init__(self, highligther):
self.skipspace = False
self.asciirow = ''

def append(self, data, color):
def append(self, data, color, width=None):
if len(data) == 0:
self._append(data, color)
self._append(data, color, width)
while len(data) > 0:
if self.rowlen == 16:
self._newrow()
consumed = self._append(data[:16 - self.rowlen], color)
consumed = self._append(data[:16 - self.rowlen], color, width)
data = data[consumed:]

def _append(self, data, color):
def _append(self, data, color, width):
if len(data) == 0:
#in the case of highlightig a deletion in a target or an
#addition in the source, print a highlighted space and mark
Expand All @@ -101,7 +111,10 @@ def _append(self, data, color):
asciis += '.'
self.asciirow += self.highligther(asciis, color)

self.hexrow += self.highligther(hexs, color)
self.hexrow += self.highligther(hexs, color)
if width:
if len(self.hexrow) > int(width):
self.hexrow = textwrap.fill(self.hexrow, int(width))
self.rowlen += len(data)
return len(data)

Expand Down
4 changes: 2 additions & 2 deletions multidiff/StreamView.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
class StreamView():
'''A class for building UIs. Has some pretty serious side effects.
Use Render instead if you're not making a long-running UI'''
def __init__(self, model, encoding='hexdump', mode='sequence', color='ansi'):
def __init__(self, model, encoding='hexdump', mode='sequence', color='ansi', width=None):
self.color = color
self.render = Render(color=color, encoder=encoding)
self.render = Render(color=color, encoder=encoding, width=width)
self.mode = mode
self.model = model
model.add_listener(self)
Expand Down
16 changes: 14 additions & 2 deletions multidiff/command_line_interface.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
#!/usr/bin/python3
import argparse
from multidiff import MultidiffModel, StreamView, SocketController, FileController, StdinController
import os

def main():
args = make_parser().parse_args()
m = MultidiffModel()
v = StreamView(m, encoding=args.outformat, mode=args.mode, color=args.color)


if args.width == 'max':
rows, columns = os.popen('stty size', 'r').read().split()
columns = int(columns)
args.width = columns

v = StreamView(m, encoding=args.outformat, mode=args.mode, color=args.color, width=args.width)

if len(args.file) > 0:
informat = args.informat if args.informat else 'raw'
files = FileController(m, informat)
Expand Down Expand Up @@ -75,6 +82,11 @@ def make_parser():
const='html',
default='ansi',
help='use html for colors instead of ansi codes')

parser.add_argument('-w', '--width',
dest='width',
default='82',
help='number of bytes printed per line, either an integer or max(width of console)')
return parser

if __name__ == '__main__':
Expand Down
14 changes: 14 additions & 0 deletions test/hexdump_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,17 @@ def test_empty_data_insert():
dump += Ansi.replace + 'A' + Ansi.reset
dump += ' |'
assert(result == dump)

def test_width():
hd = HexdumpEncoder(ansi_colored)
hd.append(bytes("foobar", "utf8"), "insert", "25")
result = hd.final()
print(result)
dump = '000000: '
dump+= Ansi.insert + '66'
dump+= '\n6f 6f 62 61 72' + Ansi.reset
dump+= ' |'
dump+= Ansi.insert + 'foobar' + Ansi.reset
dump+= ' |'
print(dump)
assert(result == dump)

0 comments on commit 4f9302d

Please sign in to comment.