From 3ea1aa2efe4e11cd6477671f233e3aba17770bbd Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Mon, 24 Jul 2017 11:49:45 +0200 Subject: [PATCH] Minor changes to imports --- README.md | 13 +++++++------ pretty.lua | 39 ++++++++++++--------------------------- 2 files changed, 19 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 8628f59..f543840 100644 --- a/README.md +++ b/README.md @@ -50,9 +50,9 @@ create `pretty`. 2. Lua-compatible output. 3. Customization. -I'd rather have good defaults than provide a ton of customization options. And -if some structure cannot be represented in Lua, I will rather extend the -syntax, than lose the info. +I'd rather have good defaults than provide a ton of customization options. If an +structure avoids easy representation in Lua, I'd rather extend the syntax, than +lose the info. Another aspect where `pretty` shines is in exploratory programming, when 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 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 @@ -144,5 +144,6 @@ you have ideas for improvements, or find an issue. ## License -This project is licensed under the BeerWare license - Please see the -[LICENSE.txt](https://gitfub.space/Jmaa/pretty/blob/master/LICENSE.txt) file for details. +The license is the BeerWare license - Please see the +[LICENSE.txt](https://gitfub.space/Jmaa/pretty/blob/master/LICENSE.txt) file for +details. diff --git a/pretty.lua b/pretty.lua index d9d6db0..a4b8809 100644 --- a/pretty.lua +++ b/pretty.lua @@ -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. - --- Load the library component -local format_number, format_function, format_string, analyze_structure, TABLE_TYPE +local import do local thispath = ... and select('1', ...):match('.+%.') or '' - local function import (name, ignore_failure) - 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' + import = function (name, ignore_failure) return require(thispath..name) end end -------------------------------------------------------------------------------- @@ -393,6 +372,11 @@ local function fix_seperator_info (l, indent_char, max_depth) end end +-------------------------------------------------------------------------------- + +local analyze_structure = import 'analyze_structure' +local TABLE_TYPE = import 'table_type' + -------------------------------------------------------------------------------- -- Formatting stuff @@ -524,12 +508,12 @@ end local TYPE_TO_FORMAT_FUNC = { ['nil'] = format_primitive, ['boolean'] = format_primitive, - ['number'] = format_number, - ['string'] = format_string, + ['number'] = import 'number', + ['string'] = import 'pstring', ['thread'] = format_coroutine, ['table'] = format_table, - ['function'] = format_function, + ['function'] = import 'function', ['userdata'] = format_primitive, -- TODO ['cdata'] = format_primitive, -- TODO & Luajit only } @@ -537,6 +521,7 @@ local TYPE_TO_FORMAT_FUNC = { function format_value (value, depth, l) assert(type(depth) == 'number' and type(l) == 'table') local formatting = TYPE_TO_FORMAT_FUNC[type(value)] + --print(value, formatting) if formatting then formatting(value, depth, l, format_value) else