blob: a9faf76ac99e5fdc6b0763bca22199f92b08c11f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
package main
import (
"fmt"
"os"
"strings"
"codeberg.org/snonux/perc/internal"
"codeberg.org/snonux/perc/internal/calculator"
"codeberg.org/snonux/perc/internal/repl"
"github.com/mattn/go-isatty"
)
func main() {
output, err := runCommand(os.Args)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
fmt.Println(output)
}
func runCommand(args []string) (string, error) {
if len(args) < 2 {
// No args provided - check if stdin is a TTY for REPL mode
if isatty.IsTerminal(os.Stdin.Fd()) {
repl.RunREPL()
return "", nil
}
printUsage()
return "", fmt.Errorf("no input provided")
}
if args[1] == "version" {
return internal.Version, nil
}
// Check for --repl flag
if args[1] == "--repl" || args[1] == "repl" {
repl.RunREPL()
return "", nil
}
input := strings.Join(args[1:], " ")
result, err := calculator.Parse(input)
if err != nil {
return "", err
}
return result, nil
}
func printUsage() {
fmt.Println("Usage: perc <calculation>")
fmt.Println(" perc version")
fmt.Println(" perc [--repl|repl]")
fmt.Println("\nExamples:")
fmt.Println(" perc 20% of 150")
fmt.Println(" perc what is 20% of 150")
fmt.Println(" perc 30 is what % of 150")
fmt.Println(" perc 30 is 20% of what")
fmt.Println("\nStart REPL mode interactively by running without arguments:")
fmt.Println(" perc")
}
|