## [library] inspect.lua - print tables and debug beautifully

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: inspect.lua

I think I will not use the lua ids after all. They are not deterministic, so generating tests for them is more difficult than I'd like. Plus, if a table has a __tostring metamethod, its id isn't reachable anymore (I think).

Right now I'm thinking about rolling down my own table identifiers - as in 1,2,3,4 ... they should be easier to read and compare than the lua ids anyway.
When I write def I mean function.

BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

### Re: inspect.lua

You could momentarily set __tostring to nil, and then set it back again. But hey, I think your own identifiers would work just fine.

ishkabible
Party member
Posts: 241
Joined: Sat Oct 23, 2010 7:34 pm
Location: Kansas USA

### Re: inspect.lua

i didn't think about that __tostring thing... rolling your own IDs sounds better becuase the numbers will be so much smaller and as such easier to read. nice idea!!

ishkabible
Party member
Posts: 241
Joined: Sat Oct 23, 2010 7:34 pm
Location: Kansas USA

### Re: inspect.lua

hey kikito, have you updated inspect? i saw you updated cron and memorize so i thought i would ask...

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: inspect.lua

Not yet, sorry! It's on my TODO list.

I'll get on it as soon as I finish with the new tweening lib.
When I write def I mean function.

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: inspect.lua

v1.1 of inspect.lua has been released!

On this version I've added counters.

Counters are used to identify functions, so now they will be called <function 1>, <function 2>, etc instead of simply <function>. This way you can easily tell them appart:

Code: Select all

inspect({print, require, print}) == "<1>{ <function 1>, <function 2>, <function 1> }"
This (should) also happen with threads (<thread 1>, <thread 2>, etc) and userdata (<userdata 1>, <userdata 2>, etc). Although I haven't tested those cases.

You may have noticed the <1> before the table above. Now tables are preceded by an <identifier>:

Code: Select all

inspect({1,2,{3,4}}) == "<1>{ 1, 2, <2>{ 3, 4 } }"
These identifiers are used to avoid "re-printing" the same table again and again. When inspect encounters the same table more than once, instead of reprinting its contents again, it prints <table x>, where x is the table identifier:

Code: Select all

a = {1,2}
b = {3,4,a}
a[3] = b
inspect(a) = "<1>{ 1, 2, <2>{ 3, 4, <table 1> } }"
Notice how the second appearance of a was replaced by <table 1>.

I hope you are satisfied with the changes. Regards, and good night!
When I write def I mean function.

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: inspect.lua

New version (1.2.0) of inspect.lua is out:

The biggest change is that in the new version, the <id> of a table will be printed only for the tables that are repeated, while the rest will not. This tones visual noise, and makes the printouts more useful. Now if you see an <id> before a table, you now know that that table will appear more than once on the generated string.

Another change is that now the default depth is "infinite". The historical reason for having such parameter was table recursion, but handling them via <ids> is a better approach IMHO. The depth param is still there, and if you set it up to 4 inspect will behave like before.

There has also been some small code cleanup and refactoring, and the tests have been cleaned up a little bit.
When I write def I mean function.

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: [library] inspect.lua - print tables and debug beautiful

It seems that I forgot to mention inspect 2.0 on this thread.

Well, nevertheless, here you go: inspect.lua v3.0.0 is out. You can get it from github or luarocks:

https://github.com/kikito/inspect.lua

The basic function are the same: you give it a Lua value, and it gives you back a string representing the object, but in a way humans understand easily. It is especially useful for seeing the contents of tables.

Code: Select all

local inspect = require 'inspect'

print(inspect({1,2,3})) -- prints { 1, 2, 3 }
Inspect accepts a second optional parameter called options, which can be used to tweak the generated string in several ways. That is the one which has changed on this version. The new options are explained on the readme.

I have also made the code a bit smaller and faster, and fixed one bug related with floats used as keys on array-like tables.

Regards!
When I write def I mean function.

SuperZazu
Citizen
Posts: 56
Joined: Sun Jun 10, 2012 2:06 pm
Location: France
Contact:

### Re: [library] inspect.lua - print tables and debug beautiful

As always, a simple & useful tool. Thank you, mate

Marty
Citizen
Posts: 89
Joined: Mon Dec 04, 2017 1:47 am
Location: Germany

### Re: [library] inspect.lua - print tables and debug beautifully

Hey kikito, great work (not only on this lib).

Is it possible to make inspect work on metatables, too?

───▄▀▀▀▄▄▄▄▄▄▄▀▀▀▄───
───█▒▒░░░░░░░░░▒▒█───
────█░░░░░░░░░█────
▄▄──█░░░▀█▀░░░█──▄▄
█░░█▀▄░░░░░░░▄▀█░░█

### Who is online

Users browsing this forum: No registered users and 13 guests