kikito / inspect.lua

Human-readable representation of Lua tables
https://github.com/kikito/inspect.lua
MIT License
1.38k stars 196 forks source link

stop/start inspection and direct print options #42

Closed Sorky closed 3 years ago

Sorky commented 5 years ago

Keep / discard / improve - Just sharing if you are interested...

  1. Allows the inspect to wait until needed
  2. Print (with user set text) rather than return

Test...

-- Variables local extensions = {"png", "bmp", {["jpg"] = 1, ["jpeg"] = 2}}

-- Code print(inspect(extensions)) -- "A" print(inspect(extensions, {wait = true})) -- "B" print("C: Inspecting 'extensions': " .. inspect(extensions)) -- "C" print(inspect(extensions, {start = true})) -- "D" print("E: Inspecting 'extensions': " .. inspect(extensions, {wait = true})) -- "E" inspect(extensions, {wait = true, doprint = "F: Inspecting 'extensions'..."}) -- "F" inspect(extensions, {wait = true, doprint = "" }) -- "G" inspect({},{stop = true}) -- "H" inspect( extensions, {wait = true, doprint = "I: This should not inspect"}) -- "I" for i=1,4 do -- "J" if i == 3 then inspect(,{start = 1}) else inspect(,{stop = 1}) end -- "K" inspect( extensions, {wait = true, doprint = "L: Inspecting 'extensions' (" .. i .. ")..." } ) -- "L" end

Output...

{ "png", "bmp", { jpeg = 2, jpg = 1 } } did not inspect - waiting for a start C: Inspecting 'extensions': { "png", "bmp", { jpeg = 2, jpg = 1 } } inspect enabled E: Inspecting 'extensions': { "png", "bmp", { jpeg = 2, jpg = 1 } } F: Inspecting 'extensions'... { "png", "bmp", { jpeg = 2, jpg = 1 } } { "png", "bmp", { jpeg = 2, jpg = 1 } } L: Inspecting 'extensions' (3)... { "png", "bmp", { jpeg = 2, jpg = 1 } }

Commentary...

A: Original way still works B: Will only work when called after a call with start=true [note: status message added when using the original way] C: Original way still works [only usage with wait enabled can get blocked] D: Enable all subsequent wait enabled calls [Does not inspect the element / bonus status message when using the original way] E: Basically the same as 'C' but it will now work because a start has been done [unlike 'B' which was blocke] F: Basically the same as 'E' but using the new doprint option G: Basically the same as 'E' but using the new doprint option with no preamble H: Stop further inspections [absolutely nothing prints as opposed to 'D'] I: Inspection blocked just like 'B' [but absolutely nothing prints unlike 'B'] J: One situation where the new options can really help follows... K: Start and stop selectively [using simplest option settings with absolutely nothing printing] L: Thanks to 'K' it only inspects the chosen instance [3] !!!

Notes...

  1. As shown in 'G' and 'K', all new options only need to exist and not be nil or false (ie: Can even be 1 or "")
  2. BONUS = Can start & stop inspection in one file from another!

PS...

Thanks for sharing the original!

coveralls commented 5 years ago

Coverage Status

Coverage decreased (-1.8%) to 97.448% when pulling 307eb2532bd4c83c0c9bddd112c7743e7610742e on Sorky:patch-1 into b611db6bfa9c12ce35dd4972032fbbd2ad5ba965 on kikito:master.

kikito commented 3 years ago

I think this feature is better implemented in your own custom inspect, like so:

local inspect = require "inspect"

local function my_inspect(t)
  if stopped then
    ...
  end
  return inspect(t)
end

And then use my_inspect instead of inspect all thorough your project.

Thanks for sending the PR though, and apologies for not reviewing it sooner.