| 1 |
-------------------------------------------------------------------------------- |
|---|
| 2 |
-- Title: WikiAbout.lua |
|---|
| 3 |
-- Description: Like a square peg in a round hole |
|---|
| 4 |
-- Author: Raphaël Szwarc http://alt.textdrive.com/lua/ |
|---|
| 5 |
-- Creation Date: January 30, 2007 |
|---|
| 6 |
-- Legal: Copyright (C) 2007 Raphaël Szwarc |
|---|
| 7 |
-- Under the terms of the MIT License |
|---|
| 8 |
-- http://www.opensource.org/licenses/mit-license.html |
|---|
| 9 |
-------------------------------------------------------------------------------- |
|---|
| 10 |
|
|---|
| 11 |
-- import dependencies |
|---|
| 12 |
local os = require( 'os' ) |
|---|
| 13 |
local package = require( 'package' ) |
|---|
| 14 |
local table = require( 'table' ) |
|---|
| 15 |
|
|---|
| 16 |
local collectgarbage = collectgarbage |
|---|
| 17 |
local getmetatable = getmetatable |
|---|
| 18 |
local pairs = pairs |
|---|
| 19 |
local require = require |
|---|
| 20 |
local setmetatable = setmetatable |
|---|
| 21 |
local tostring = tostring |
|---|
| 22 |
local type = type |
|---|
| 23 |
|
|---|
| 24 |
local runtime = _VERSION |
|---|
| 25 |
|
|---|
| 26 |
-------------------------------------------------------------------------------- |
|---|
| 27 |
-- WikiAbout |
|---|
| 28 |
-------------------------------------------------------------------------------- |
|---|
| 29 |
|
|---|
| 30 |
module( 'WikiAbout' ) |
|---|
| 31 |
_VERSION = '1.0' |
|---|
| 32 |
|
|---|
| 33 |
local self = setmetatable( _M, {} ) |
|---|
| 34 |
local meta = getmetatable( self ) |
|---|
| 35 |
|
|---|
| 36 |
-------------------------------------------------------------------------------- |
|---|
| 37 |
-- Utilities |
|---|
| 38 |
-------------------------------------------------------------------------------- |
|---|
| 39 |
|
|---|
| 40 |
local function Memory() |
|---|
| 41 |
local aMemory = collectgarbage( 'count' ) |
|---|
| 42 |
local anUnit = 'KB' |
|---|
| 43 |
local aPrecision = 0 |
|---|
| 44 |
|
|---|
| 45 |
if aMemory > 999 then |
|---|
| 46 |
aMemory = aMemory / 1024 |
|---|
| 47 |
anUnit = 'MB' |
|---|
| 48 |
aPrecision = 1 |
|---|
| 49 |
end |
|---|
| 50 |
|
|---|
| 51 |
return aMemory, anUnit, aPrecision |
|---|
| 52 |
end |
|---|
| 53 |
|
|---|
| 54 |
local function Module() |
|---|
| 55 |
local aList = {} |
|---|
| 56 |
|
|---|
| 57 |
for aName, aPackage in pairs( package[ 'loaded' ] ) do |
|---|
| 58 |
if aName == aName:lower() and type( aPackage ) == 'table' then |
|---|
| 59 |
aList[ #aList + 1 ] = aName |
|---|
| 60 |
end |
|---|
| 61 |
end |
|---|
| 62 |
|
|---|
| 63 |
table.sort( aList ) |
|---|
| 64 |
|
|---|
| 65 |
return aList |
|---|
| 66 |
end |
|---|
| 67 |
|
|---|
| 68 |
-------------------------------------------------------------------------------- |
|---|
| 69 |
-- Metamethods |
|---|
| 70 |
-------------------------------------------------------------------------------- |
|---|
| 71 |
|
|---|
| 72 |
function meta:__call() |
|---|
| 73 |
return function() |
|---|
| 74 |
return tostring( self ) |
|---|
| 75 |
end |
|---|
| 76 |
end |
|---|
| 77 |
|
|---|
| 78 |
function meta:__concat( aValue ) |
|---|
| 79 |
return tostring( self ) .. tostring( aValue ) |
|---|
| 80 |
end |
|---|
| 81 |
|
|---|
| 82 |
function meta:__tostring() |
|---|
| 83 |
local Nanoki = require( 'Nanoki' ) |
|---|
| 84 |
local Template = require( 'Template' ) |
|---|
| 85 |
local aTemplate = Template[ 'WikiAbout.txt' ] |
|---|
| 86 |
local aMemory, anUnit, aPrecision = Memory() |
|---|
| 87 |
|
|---|
| 88 |
aTemplate[ 'version' ] = Nanoki._VERSION |
|---|
| 89 |
aTemplate[ 'year' ] = os.date( '!*t' ).year |
|---|
| 90 |
aTemplate[ 'runtime' ] = runtime |
|---|
| 91 |
aTemplate[ 'memory' ] = ( '%.' .. aPrecision .. 'f %s' ):format( aMemory, anUnit ) |
|---|
| 92 |
aTemplate[ 'module' ] = table.concat( Module(), ', ' ) |
|---|
| 93 |
|
|---|
| 94 |
return tostring( aTemplate ) |
|---|
| 95 |
end |
|---|