diff --git a/README.md b/README.md index d79101d..cd1ec88 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,24 @@ assert(inspect(t5, {depth = 2}) == [[{ `options.depth` defaults to infinite (`math.huge`). +### options.newline & options.indent + +These are the strings used by `inspect` to respectively add a newline and indent each level of a table. + +By default, `options.newline` is `"\n"` and `options.indent` is `" "` (two spaces). + +``` lua +local t = {a={b=1}} + +assert(inspect(t) == [[{ + a = { + b = 1 + } +}]]) + +assert(inspect(t, {newline='@', indent="++"}), "{@++a = {@++++b = 1@++}@}" +``` + ### options.process `options.process` is a function which allow altering the passed object before transforming it into a string. diff --git a/inspect.lua b/inspect.lua index d548be1..8d2aa9f 100644 --- a/inspect.lua +++ b/inspect.lua @@ -199,7 +199,7 @@ function Inspector:down(f) end function Inspector:tabify() - self:puts("\n", string.rep(" ", self.level)) + self:puts(self.newline, string.rep(self.indent, self.level)) end function Inspector:commaControl(needsComma) @@ -304,6 +304,8 @@ function inspect.inspect(root, options) options = options or {} local depth = options.depth or math.huge local process = options.process + local newline = options.newline or '\n' + local indent = options.indent or ' ' if process then root = processRecursive(process, root, {}) end @@ -314,6 +316,8 @@ function inspect.inspect(root, options) level = 0, ids = setmetatable({}, idsMetaTable), maxIds = setmetatable({}, maxIdsMetaTable), + newline = newline, + indent = indent, tableAppearances = countTableAppearances(root) }, Inspector_mt) diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index 958aa81..f5ee147 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -197,6 +197,22 @@ describe( 'inspect', function() end) end) + describe('the newline option', function() + it('changes the substring used for newlines', function() + local t = {a={b=1}} + + assert.equal(inspect(t, {newline='@'}), "{@ a = {@ b = 1@ }@}") + end) + end) + + describe('the indent option', function() + it('changes the substring used for indenting', function() + local t = {a={b=1}} + + assert.equal(inspect(t, {indent='>>>'}), "{\n>>>a = {\n>>>>>>b = 1\n>>>}\n}") + end) + end) + describe('the process option', function() it('removes one element', function()