Closed ghost closed 2 years ago
You just forked my repo and changed the name in the readme. Why?
@trustable-code Hi! I hope I have clarified any doubts. I changed the name of the repository for 4 reasons:
I consider this as some weird kind of spam.
@trustable-code Hi! How are you?
"My idea is to have an integrated calculator+database system. Initially, I thought of using SQLite for this purpose. It is a lightweight and easy to maintain database."
# Babbage - main program
import nigui
import os
import utils
import tiny_sqlite, std / options
let db = openDatabase(":memory:")
db.execScript("""
CREATE TABLE Babbage(
input TEXT,
result REAL
);
""")
#[
let db = openDatabase(":memory:")
db.execScript("""
CREATE TABLE Babbage(
input TEXT,
result REAL
);
INSERT INTO
Babbage(input, result)
VALUES
("1+2", 3);
""")
for row in db.iterate("SELECT input, result FROM Babbage"):
let (input, result) = row.unpack((string, Option[int]))
echo input, " ", result
]#
const buttonWidth = 70
const labelWidth = 55
const editFontSize = 22
const editFontFamily = "Consolas"
const historyFontSize = 18
let appConfigDir = getConfigDir() & "Babbage"
let historyFilePath = appConfigDir / "history.txt"
# Output:
# 1+2 (3)
# 1+2 None[int]
var lastCalculation = ""
var timer: Timer
app.init()
var window = newWindow("Babbage")
window.width = 600
window.height = 450
var mainContainer = newLayoutContainer(Layout_Vertical)
mainContainer.padding = 6
window.add(mainContainer)
var inputContainer = newLayoutContainer(Layout_Horizontal)
mainContainer.add(inputContainer)
var inputLabel = newLabel("Input:")
inputContainer.add(inputLabel)
inputLabel.minWidth = labelWidth
inputLabel.heightMode = HeightMode_Fill
var inputTextBox = newTextBox()
inputContainer.add(inputTextBox)
inputTextBox.fontSize = editFontSize
inputTextBox.fontFamily = editFontFamily
var clearButton = newButton("Clear")
clearButton.minWidth = buttonWidth
clearButton.heightMode = HeightMode_Fill
inputContainer.add(clearButton)
var resultContainer = newLayoutContainer(Layout_Horizontal)
mainContainer.add(resultContainer)
var resultLabel = newLabel("Result:")
resultContainer.add(resultLabel)
resultLabel.minWidth = labelWidth
resultLabel.heightMode = HeightMode_Fill
var resultTextBox = newTextBox()
resultContainer.add(resultTextBox)
resultTextBox.fontSize = editFontSize
resultTextBox.fontFamily = editFontFamily
var historyContainer = newLayoutContainer(Layout_Vertical)
mainContainer.add(historyContainer)
historyContainer.frame = newFrame("History")
var historyTextArea = newTextArea()
historyContainer.add(historyTextArea)
historyTextArea.fontSize = historyFontSize
historyTextArea.fontFamily = editFontFamily
if fileExists(historyFilePath):
historyTextArea.text = readFile(historyFilePath)
if historyTextArea.text.len == 0:
historyContainer.hide()
proc updateResult(event: TimerEvent) =
timer.stop()
var term = inputTextBox.text
lastCalculation = ""
resultTextBox.text = ""
if term.len == 0:
return
try:
let resultStr = term.calculate.formatFloat
resultTextBox.textColor = app.defaultTextColor
resultTextBox.text = resultStr
lastCalculation = term & " = " & resultStr
except:
resultTextBox.textColor = rgb(255, 0, 0) # red
resultTextBox.text = "Error: " & getCurrentExceptionMsg()
inputTextBox.onTextChange = proc(event: TextChangeEvent) =
lastCalculation = ""
resultTextBox.text = ""
timer.stop()
timer = startTimer(500, updateResult)
inputTextBox.onKeyDown = proc(event: KeyboardEvent) =
if event.key == Key_Return:
if lastCalculation == "":
updateResult(nil)
if lastCalculation != "":
historyTextArea.addLine(lastCalculation)
historyTextArea.scrollToBottom()
historyContainer.show()
window.onKeyDown = proc(event: KeyboardEvent) =
if event.key == Key_Escape:
window.dispose()
window.onDispose = proc(event: WindowDisposeEvent) =
if historyTextArea.text.len > 0 or fileExists(historyFilePath):
createDir(appConfigDir)
writeFile(historyFilePath, historyTextArea.text)
db.exec("INSERT INTO Babbage VALUES(?, ?)", historyTextArea, nil)
clearButton.onClick = proc(event: ClickEvent) =
inputTextBox.text = ""
resultTextBox.text = ""
inputTextBox.focus()
window.show()
inputTextBox.focus()
app.run()
@trustable-code Hi, Simon Krauter! How are you?
Regards, nkot56297