printDefinition(func) || print function definition

Discussion in 'Mods & Lua Functions' started by Xeraphus, Jan 11, 2017.

  1. Xeraphus

    Xeraphus Support Team Support Team Premium User

    Joined:
    Feb 14, 2015
    Messages:
    2,087
    Likes Received:
    784
    Best Answers:
    65
    made this while learning debug library for fun
    always wondered how to print function definition

    usage: printDefinition(Game.convertIpToString)
    [​IMG]

    code:
    Code (Text):
    1.  
    2. function printDefinition(func)
    3.     local info = debug.getinfo(func)
    4.     if (info.what == "C") then
    5.         return print("Input may not be a C function.")
    6.     end
    7.     local count = 0
    8.     local startL, endL = info.linedefined, info.lastlinedefined
    9.     for line in io.lines(info.short_src) do
    10.         count = count + 1
    11.         if (count >= startL) and (count <= endL) then
    12.             -- Fix large indentations by replacing all tab characters with 4 spaces
    13.             local text = line:gsub('%\t', '    ')
    14.             print(text)
    15.          
    16.             -- Break loop once last line is printed
    17.             if (count == endl) then
    18.                 break
    19.             end
    20.         end
    21.     end
    22. end
     
    Last edited: Feb 3, 2017
  2. Colandus

    Colandus Support Team Support Team

    Joined:
    Jun 6, 2007
    Messages:
    2,414
    Likes Received:
    148
    Best Answers:
    17
    You should probably name it differently though as printf in other languages is similar to Lua string.format. Could cause confusions.
     
    Xeraphus likes this.
  3. Xeraphus

    Xeraphus Support Team Support Team Premium User

    Joined:
    Feb 14, 2015
    Messages:
    2,087
    Likes Received:
    784
    Best Answers:
    65
    renamed to printDefinition
     

Share This Page

Loading...