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

//@version=5
// @description TODO: simple debug console. USAGE : import keio/console/1 as console
library("console", overlay=false)

//                                  ███████╗ █████╗ ███████╗██╗   ██╗         
//                                  ██╔════╝██╔══██╗██╔════╝╚██╗ ██╔╝         
//                                  █████╗  ███████║███████╗ ╚████╔╝          
//                                  ██╔══╝  ██╔══██║╚════██║  ╚██╔╝           
//                                  ███████╗██║  ██║███████║   ██║            
//                                  ╚══════╝╚═╝  ╚═╝╚══════╝   ╚═╝            
// 
//                               ██████╗ ██████╗ ██╗███╗   ██╗████████╗
//                               ██╔══██╗██╔══██╗██║████╗  ██║╚══██╔══╝
//                               ██████╔╝██████╔╝██║██╔██╗ ██║   ██║   
//                               ██╔═══╝ ██╔══██╗██║██║╚██╗██║   ██║   
//                               ██║     ██║  ██║██║██║ ╚████║   ██║   
//                               ╚═╝     ╚═╝  ╚═╝╚═╝╚═╝  ╚═══╝   ╚═╝   
//                                                                                    
//                     ██████╗ ██████╗ ███╗   ██╗███████╗ ██████╗ ██╗     ███████╗
//                    ██╔════╝██╔═══██╗████╗  ██║██╔════╝██╔═══██╗██║     ██╔════╝
//                    ██║     ██║   ██║██╔██╗ ██║███████╗██║   ██║██║     █████╗  
//                    ██║     ██║   ██║██║╚██╗██║╚════██║██║   ██║██║     ██╔══╝  
//                    ╚██████╗╚██████╔╝██║ ╚████║███████║╚██████╔╝███████╗███████╗
//                     ╚═════╝ ╚═════╝ ╚═╝  ╚═══╝╚══════╝ ╚═════╝ ╚══════╝╚══════╝


// █ █ █▀█ █ █ █   ▀█▀ █▀█   █ █ █▀ █▀▀
// █▀█ █▄█ ▀▄▀▄▀    █  █▄█   █▄█ ▄█ ██▄

// Make sure your strategy overlay is false

// █ █▄ █ █▀ ▀█▀ ▄▀█ █   █  
// █ █ ▀█ ▄█  █  █▀█ █▄▄ █▄▄

// import keio/console/2 as console
// var log = console.init()

// █▀█ █▀█ █ █▄ █ ▀█▀
// █▀▀ █▀▄ █ █ ▀█  █ 

// log := console.print(log, "message")

// █▀█ █▀█ ▀█▀ █ █▀█ █▄ █ ▄▀█ █  
// █▄█ █▀▀  █  █ █▄█ █ ▀█ █▀█ █▄▄

// You can also specify the number of lines displayed and the number of panes/columns
// Simply pass the 'lines' and 'panes' parameters when initializing, for example:

// var log = console(lines=10, panes=4)

// Lines are limited to 60, default is 5
// Panes are limited to 20, default is 8

// █▀▀ ▀▄▀ ▄▀█ █▀▄▀█ █▀█ █   █▀▀   █▀ ▀█▀ █▀█ ▄▀█ ▀█▀ █▀▀ █▀▀ █▄█
// ██▄ █ █ █▀█ █ ▀ █ █▀▀ █▄▄ ██▄   ▄█  █  █▀▄ █▀█  █  ██▄ █▄█  █ 

// //@version=5
// strategy("example", overlay=false)

// import keio/console/2 as console
// var log = console.init()

// var int long = 0

// longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))
// if (longCondition)
//     long += 1
//     log := console.print(log, "Long entry : " + str.tostring(long))
//     strategy.entry("long", strategy.long)
// strategy.exit(id = "long exit", from_entry = "long", limit=close, stop=close)






// █   █ █▄▄ █▀█ ▄▀█ █▀█ █▄█   █▀▀ █▀█ █▀▄ █▀▀
// █▄▄ █ █▄█ █▀▄ █▀█ █▀▄  █    █▄▄ █▄█ █▄▀ ██▄

var MAX_ENTRIES = 1200
var MAX_LINES = 60
var MAX_PANES = 20

// @function init() function. USAGE : var log = console.init()
// @param lines Optional. Number of lines to display
// @param panes Optional. Number of panes to display
// @returns initialised log
export init(int lines=5, int panes=8) =>
     
    // Set configuration parameters
    int n_lines = (na(lines) or lines <= 0 or lines > MAX_LINES) ? MAX_LINES : lines
    int n_panes = (na(panes) or panes <= 0) ? 1 : (panes > MAX_PANES ? MAX_PANES : panes)
     
    // Initialise log
    string[] log = array.new_string(MAX_ENTRIES)
     
    // Save parameters
    array.push(log, str.tostring(n_lines))
    array.push(log, str.tostring(n_panes))
     
    // Return log
    log

// @function Prints a message to the console. USAGE : log := console.print(log, "message")
// @param log The log returned from the init() function
// @param input Message to be displayed in the console
// @returns The log
export print(string[] log, string input) =>
     
    // Extract configuration parameters
    int panes = math.floor(str.tonumber(array.pop(log)))
    int lines = math.floor(str.tonumber(array.pop(log)))
     
    // Save new log input
    array.push(log, input)
     
    // Discard oldest log input
    array.shift(log)
     
    // Create console table
    var table console = table.new(
         position=position.bottom_left,
         columns=panes,
         rows=1,
         border_color=color.new(color.white, 50),
         border_width=1
         )
     
    // Concatenate and display log inputs
    for p = 0 to panes-1
        string content = na
        for i = 0 to lines-1
            content += array.get(log, MAX_ENTRIES-(p+1)*lines+i) + "\n"
        table.cell(
             table_id=console,
             column=panes-1-p,
             row=0, 
             text=content,
             width=100/panes,
             height=100,
             bgcolor=color.black,
             text_size=size.small,
             text_valign=text.align_bottom,
             text_halign=text.align_left,
             text_color=color.white
             )
     
    // Save configuration parameters
    array.push(log, str.tostring(lines))
    array.push(log, str.tostring(panes))
     
    // Return log
    log

//                              ███████╗██╗███╗   ███╗██████╗ ██╗     ███████╗
//                              ██╔════╝██║████╗ ████║██╔══██╗██║     ██╔════╝
//                              ███████╗██║██╔████╔██║██████╔╝██║     █████╗  
//                              ╚════██║██║██║╚██╔╝██║██╔═══╝ ██║     ██╔══╝  
//                              ███████║██║██║ ╚═╝ ██║██║     ███████╗███████╗
//                              ╚══════╝╚═╝╚═╝     ╚═╝╚═╝     ╚══════╝╚══════╝
//                                                                              
//                                  ██████╗  █████╗ ███████╗██╗ ██████╗       
//                                  ██╔══██╗██╔══██╗██╔════╝██║██╔════╝       
//                                  ██████╔╝███████║███████╗██║██║            
//                                  ██╔══██╗██╔══██║╚════██║██║██║            
//                                  ██████╔╝██║  ██║███████║██║╚██████╗       
//                                  ╚═════╝ ╚═╝  ╚═╝╚══════╝╚═╝ ╚═════╝       
//                                                                              
//                               https://www.youtube.com/watch?v=2bjk26RwjyU