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 1, 2019
1 parent ff94ed2 commit 0925ead
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 32 deletions.
38 changes: 24 additions & 14 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='82'):
'''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,11 @@ def __init__(self, highligther):
self.highligther = highligther
self.output = ''

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

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

def final(self):
return self.output

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

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

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

def final(self):
return self.output

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

def append(self, data, color):
def append(self, data, color, width):
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
#it skippanble for the next append
hexs = ' '
hexs = ' '
self.skipspace = True
else:
self._add_hex_space()
Expand All @@ -101,7 +109,9 @@ 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 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='82'):
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
32 changes: 23 additions & 9 deletions multidiff/command_line_interface.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
#!/usr/bin/python3
import argparse
from multidiff import MultidiffModel, StreamView, SocketController, FileController, StdinController
import os

def main():

rows, columns = os.popen('stty size', 'r').read().split()
columns = int(columns)

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


if args.width == 'max':
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 All @@ -32,7 +41,7 @@ def make_parser():
│ sensor module │
└───────────────┘
""")

parser.add_argument('file',
type=str,
nargs='*',
Expand All @@ -42,7 +51,7 @@ def make_parser():
dest='port',
type=int,
help='start a local socket server on the given port')

parser.add_argument('-s','--stdin',
dest='stdin',
action='store_true',
Expand All @@ -52,21 +61,21 @@ def make_parser():
dest='mode',
default='sequence',
help='mode of operation, either "baseline" or "sequence"')

parser.add_argument('-i','--informat',
dest='informat',
help='input data format:\n' +
' utf8 (stdin default)\n' +
' raw (file and server default)\n' +
' hex\n'+
' json')

parser.add_argument('-o','--outformat',
dest='outformat',
default='hexdump',
help='output data format:\n' +
' utf8\n' +
' hex\n' +
help='output data format:\n' +
' utf8\n' +
' hex\n' +
' hexdump (default)')

parser.add_argument('--html',
Expand All @@ -75,6 +84,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: 7 additions & 7 deletions test/hexdump_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ def mono(string, color):

def test_single_line_mono():
hd = HexdumpEncoder(mono)
hd.append(bytes("foobar", "utf8"), "mono")
hd.append(bytes("foobar", "utf8"), "mono", '82')
result = hd.final()
dump = '000000: 66 6f 6f 62 61 72 |foobar |'
assert(result == dump)

def test_multiple_line_mono():
hd = HexdumpEncoder(mono)
hd.append(bytes("0123456789abcdef012345678", "utf8"), "mono")
hd.append(bytes("0123456789abcdef012345678", "utf8"), "mono", '82')
result = hd.final()
dump = '000000: 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66 |0123456789abcdef|\n'
dump+= '000010: 30 31 32 33 34 35 36 37 38 |012345678 |'
assert(result == dump)

def test_single_line_insert():
hd = HexdumpEncoder(ansi_colored)
hd.append(bytes("foobar", "utf8"), "insert")
hd.append(bytes("foobar", "utf8"), "insert", '82')
result = hd.final()
print(result)
dump = '000000: '
Expand All @@ -33,8 +33,8 @@ def test_single_line_insert():

def test_multiple_line_insert():
hd = HexdumpEncoder(ansi_colored)
hd.append(bytes("012345678a", "utf8"), "equal")
hd.append(bytes("012345678a", "utf8"), "replace")
hd.append(bytes("012345678a", "utf8"), "equal", '82')
hd.append(bytes("012345678a", "utf8"), "replace", '82')
result = hd.final()
#jesus!
dump = '000000: 30 31 32 33 34 35 36 37 38 61 '
Expand All @@ -50,8 +50,8 @@ def test_multiple_line_insert():

def test_empty_data_insert():
hd = HexdumpEncoder(ansi_colored)
hd.append(bytes("A", "utf8"), "replace")
hd.append(bytes("", "utf8"), "delete")
hd.append(bytes("A", "utf8"), "replace", '82')
hd.append(bytes("", "utf8"), "delete", '82')
result = hd.final()
print(result)
dump = '000000: '
Expand Down

0 comments on commit 0925ead

Please sign in to comment.