Debug_Window_Library
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sp2432

//@version=5
// @description Provides a framework for logging debug information to a window on the chart.
library("Debug_Window_Library", true)

// @function Adds a line of text to the debug window.
// @param txt - this is the text to be appended to the window
// @param maxLines - Optional - size of the window in lines. Default=15
// @param textColor - Optional - color of the text. Default=color.green.
export Log(string txt, int maxLines = 15, color textColor = color.green) =>
    var txts = array.new_string(0,"")
    var tbl = table.new(position.top_right, 1, 1, bgcolor = color.black, frame_width = 1, frame_color = color.gray)

    // push it into the array
    array.push(txts, txt + "\n")
    
    // trim the array if needed.
    if array.size(txts) > maxLines
        array.shift(txts)
    
    table.cell(tbl, 0, 0, array.join(txts))
    table.cell_set_text_size(tbl, 0, 0, size.small)
    table.cell_set_text_color(tbl, 0, 0, textColor)
    table.cell_set_text_halign(tbl, 0, 0, text.align_left)
