wmtools/calculate.nim
2022-05-09 19:56:26 +01:00

32 lines
1 KiB
Nim

import base
import std/[osproc,strutils,sequtils]
#using qalc as it has a lot of nice inbuilt features
#
proc doCalculation(calc: string) =
var info = newInfo("Calculator")
let ans_cmd_terse = "echo \"" & calc & "\" | qalc +u8 -terse -color=never | awk '!/^>/ && !/^$/ {gsub(/^[ \\t]+|[ \\t]+$/, \"\", $0); print}'"
let ans_cmd_full = "echo \"" & calc & "\" | qalc +u8 -color=never | awk '!/^>/ && !/^$/ {gsub(/^[ \\t]+|[ \\t]+$/, \"\", $0); print}'"
let ans_full = execCmdEx(ans_cmd_full)
let ans_terse = execCmdEx(ans_cmd_terse)
let answers = @[strip(ans_terse.output),
strip(ans_full.output)]
let args = concat(answers,@["exit"])
let cmd = outputData(info, args)
if cmd in answers:
discard execCmd("echo \"" & strip(cmd) & "\" | xclip")
elif cmd == "exit":
return
else:
doCalculation(cmd)
proc main() =
var info = newInfo("Calculator")
let args = @["exit"]
let cmd = outputData(info, args)
if cmd != "":
doCalculation(cmd)
return
if isMainModule:
main()