1
0

Minor changes to imports

This commit is contained in:
Jon Michael Aanes 2017-07-24 11:49:45 +02:00
parent bb67701f8f
commit 3ea1aa2efe
2 changed files with 19 additions and 33 deletions

View File

@ -50,9 +50,9 @@ create `pretty`.
2. Lua-compatible output. 2. Lua-compatible output.
3. Customization. 3. Customization.
I'd rather have good defaults than provide a ton of customization options. And I'd rather have good defaults than provide a ton of customization options. If an
if some structure cannot be represented in Lua, I will rather extend the structure avoids easy representation in Lua, I'd rather extend the syntax, than
syntax, than lose the info. lose the info.
Another aspect where `pretty` shines is in exploratory programming, when Another aspect where `pretty` shines is in exploratory programming, when
attempting to avoid reliance on outside documentation. The amount of information attempting to avoid reliance on outside documentation. The amount of information
@ -98,7 +98,7 @@ must be a table.
`pretty` is sure to complain if you give it an unknown option, or if you give an `pretty` is sure to complain if you give it an unknown option, or if you give an
option a bad value. option a bad value.
- `indent: string`: The string to indent with. Four spaces (` `) by default. - `indent: string`: The string to indent with. Four spaces by default.
## TODO ## TODO
@ -144,5 +144,6 @@ you have ideas for improvements, or find an issue.
## License ## License
This project is licensed under the BeerWare license - Please see the The license is the BeerWare license - Please see the
[LICENSE.txt](https://gitfub.space/Jmaa/pretty/blob/master/LICENSE.txt) file for details. [LICENSE.txt](https://gitfub.space/Jmaa/pretty/blob/master/LICENSE.txt) file for
details.

View File

@ -37,33 +37,12 @@ a table, we have a better idea, but then the output would be cluttered.
--]=] --]=]
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Import files
-- Ensure loading library, if it exists, no matter where pretty.lua was loaded from. local import
-- Load the library component
local format_number, format_function, format_string, analyze_structure, TABLE_TYPE
do do
local thispath = ... and select('1', ...):match('.+%.') or '' local thispath = ... and select('1', ...):match('.+%.') or ''
local function import (name, ignore_failure) import = function (name, ignore_failure) return require(thispath..name) end
local was_loaded, lib_or_error = pcall(require, thispath..name)
if not was_loaded then
if ignore_failure then return nil end
error('[pretty]: Could not load vital library: '..name..'.lua:\n\t'..lib_or_error)
end
return lib_or_error
end
-- Load number and function formatting
-- Will use a very simple number formatting, if number.lua is not available.
-- Will use a very simple function formatting, if function.lua is not available.
-- Will use a very simple string formatting, if string.lua is not available.
format_number = import('number', true) or function (value, _, l) l[#l+1] = tostring(value) end
format_function = import('function', true) or function (value, _, l) l[#l+1] = 'function (...) --[['..tostring(value):sub(11)..']] end' end
format_string = import('pstring', true) or function (value, _, l) l[#l+1] = '[['..value..']]' end
-- Load other stuff
analyze_structure = import 'analyze_structure'
TABLE_TYPE = import 'table_type'
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -393,6 +372,11 @@ local function fix_seperator_info (l, indent_char, max_depth)
end end
end end
--------------------------------------------------------------------------------
local analyze_structure = import 'analyze_structure'
local TABLE_TYPE = import 'table_type'
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Formatting stuff -- Formatting stuff
@ -524,12 +508,12 @@ end
local TYPE_TO_FORMAT_FUNC = { local TYPE_TO_FORMAT_FUNC = {
['nil'] = format_primitive, ['nil'] = format_primitive,
['boolean'] = format_primitive, ['boolean'] = format_primitive,
['number'] = format_number, ['number'] = import 'number',
['string'] = format_string, ['string'] = import 'pstring',
['thread'] = format_coroutine, ['thread'] = format_coroutine,
['table'] = format_table, ['table'] = format_table,
['function'] = format_function, ['function'] = import 'function',
['userdata'] = format_primitive, -- TODO ['userdata'] = format_primitive, -- TODO
['cdata'] = format_primitive, -- TODO & Luajit only ['cdata'] = format_primitive, -- TODO & Luajit only
} }
@ -537,6 +521,7 @@ local TYPE_TO_FORMAT_FUNC = {
function format_value (value, depth, l) function format_value (value, depth, l)
assert(type(depth) == 'number' and type(l) == 'table') assert(type(depth) == 'number' and type(l) == 'table')
local formatting = TYPE_TO_FORMAT_FUNC[type(value)] local formatting = TYPE_TO_FORMAT_FUNC[type(value)]
--print(value, formatting)
if formatting then if formatting then
formatting(value, depth, l, format_value) formatting(value, depth, l, format_value)
else else