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

//@version=5
indicator("Calculator", overlay = true)

//Inputs
Num1 = input.float(defval = 0, title = "Input 1", group = "Calculate:")
//Operators
operator = input.string(defval = "+", title = "Operators", options=["+","-","x","÷","x^y"], group = "Calculate:")
//Inputs
Num2 = input.float(defval = 0, title = "Input 2", group = "Calculate:")

float cal = na
string result = na
if operator == "+"
    cal := Num1 + Num2
    result := (Num1 >= 0 ? str.tostring(Num1) : ("(" + str.tostring(Num1) + ")")) + " + " + (Num2 >= 0 ? str.tostring(Num2) : ("(" + str.tostring(Num2) + ")")) + " = " + str.tostring(cal)
else if operator == "-"
    cal := Num1 - Num2
    result := (Num1 >= 0 ? str.tostring(Num1) : ("(" + str.tostring(Num1) + ")")) + " - " + (Num2 >= 0 ? str.tostring(Num2) : ("(" + str.tostring(Num2) + ")")) + " = " + str.tostring(cal)
else if operator == "x"
    cal := Num1 * Num2
    result := (Num1 >= 0 ? str.tostring(Num1) : ("(" + str.tostring(Num1) + ")")) + " x " + (Num2 >= 0 ? str.tostring(Num2) : ("(" + str.tostring(Num2) + ")")) + " = " + str.tostring(cal)
else if operator == "÷"
    cal := Num1 / Num2
    result := (Num1 >= 0 ? str.tostring(Num1) : ("(" + str.tostring(Num1) + ")")) + " ÷ " + (Num2 >= 0 ? str.tostring(Num2) : ("(" + str.tostring(Num2) + ")")) + " = " + str.tostring(cal)
else if operator == "x^y"
    cal := math.pow(Num1,Num2)
    result := (Num1 >= 0 ? str.tostring(Num1) : ("(" + str.tostring(Num1) + ")")) + "^(" + str.tostring(Num2) + ") = " + str.tostring(cal)
else
    result := "Error"

//Table
//Format
tblPos = input.string(title="Position on Chart", defval="Middle Bottom", options=["Top Left", "Top Right", "Bottom Left", "Bottom Right", "Middle Left", "Middle Right", "Middle Bottom" ], group = "Table Customization")
tblposition = tblPos == "Top Left" ? position.top_left : tblPos == "Top Right" ? position.top_right : tblPos == "Bottom Left" ? position.bottom_left : tblPos == "Bottom Right" ? position.bottom_right : tblPos == "Middle Left" ? position.middle_left : tblPos == "Middle Right" ? position.middle_right : position.bottom_center
text_halign = input.string(defval = "Center", title="Horizontal Alignment", options=["Left", "Center", "Right"], group = "Table Customization")
text_halign_pos = text_halign == "Left" ? text.align_left : text_halign == "Center" ? text.align_center : text.align_right
text_valign = input.string(defval = "Center", title="Vertical Alignment", options=["Top", "Center", "Bottom"], group = "Table Customization")
text_valign_pos = text_valign == "Top" ? text.align_top : text_valign == "Center" ? text.align_center : text.align_bottom
text_size = input.string(defval = "Auto", title="Text Size", options=["Auto", "Tiny", "Small", "Normal", "Large", "Huge"], group = "Table Customization")
text_size_op = text_size == "Auto" ? size.auto : text_size == "Tiny" ? size.tiny : text_size == "Small" ? size.small : text_size == "Normal" ? size.normal : text_size == "Large" ? size.large : size.huge


//Dimensions
border_width = input.int(defval=1, title="Border Width", group = "Dimensions")
frame_width = input.int(defval=4, title="Frame Width", group = "Dimensions")
table_width = input.int(defval=20, title="Cell Width", group = "Dimensions")
table_height = input.int(defval=5, title="Cell Height", group = "Dimensions")

//Colors
tblBorderColor = input.color(title="Border Color", defval=#636363, group = "Table Styles")
celllBgColor = input.color(title="Background Color", defval=#ffffff, group = "Table Styles")
cellTextColor = input.color(title="Text Color", defval=#000000, group = "Table Styles")

var resultsTable = table.new(position = tblposition, columns = 1, rows = 1, bgcolor = #ffffff, border_width = border_width,frame_color = tblBorderColor, frame_width = frame_width)
table.clear(resultsTable, 0, 0)
table.cell(resultsTable, column=0, row=0, width = table_width, height = table_height, text_size = text_size_op, text=result, text_color=cellTextColor, text_halign=text_halign_pos, text_valign=text_valign_pos, bgcolor=celllBgColor)
