simonkrauter / NiCalc

Simple calculator written in Nim using NiGui
49 stars 6 forks source link

Thank you letter #5

Closed ghost closed 2 years ago

ghost commented 2 years ago

@trustable-code Hi, Simon Krauter! How are you?


  1. I'm writing this message to thank you for making such a good, usable code that somehow helped to build my own tool. If you want to follow this project I made based on your code here is the reference link: babbage.
  2. I'm creating a small software called 'babbage' - it's a tribute to Charles Babbage. Some say he created the concept of "programmable computer". For now I only updated the README ;D the inspiration was only in the name of the project. I haven't read much about this scientist's life.
  3. 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.
  4. I hope to contribute something opensource, I think any developer who likes computers wants to do something different and innovative. Your code is wonderful, thanks to your code I am creating something innovative. Thanks a lot for this code. The license continues as FLOSS in my software(fork) ;D


Regards, nkot56297

simonkrauter commented 2 years ago

You just forked my repo and changed the name in the readme. Why?

ghost commented 2 years ago

@trustable-code Hi! I hope I have clarified any doubts. I changed the name of the repository for 4 reasons:

  1. this software version that I am developing is not the same as the official version, I would like people to know that it is a different version, I say this through the repository under a different name and description.
  2. to mark a software version with a different concept. I'm making a version with specific features - which is very different from the official version. For example, in this fork I use the tiny_sqlite library - which is not used in the official version. I put a different fork name - It's just a markup.
  3. homage to Charles Babbage.
  4. I don't want people to confuse the official repository with the fork, they are different versions.
simonkrauter commented 2 years ago

I consider this as some weird kind of spam.

ghost commented 2 years ago

@trustable-code Hi! How are you?

1. Concept

"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."

2. Here source-code

# 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()

3. Notes

  1. What do you think of the idea?
  2. "I consider this as some weird kind of spam." - I would like to help collaborate and not spam.