wmtools/calculate.nim

35 lines
1.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}'"
var ans_full = execCmdEx(ans_cmd_full)
ans_full.output.stripLineEnd()
var ans_terse = execCmdEx(ans_cmd_terse)
ans_terse.output.stripLineEnd()
let answers = @[strip(ans_full.output),
strip(ans_terse.output)]
let args = concat(answers,@["exit"])
var cmd = outputData(info, args)
cmd.stripLineEnd()
if cmd in answers:
discard execCmd("echo " & quote(cmd) & " | xclip")
elif cmd == "exit" or cmd == "":
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()