-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
52 lines (40 loc) · 870 Bytes
/
main.go
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
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/pokstad/offset/offset"
)
var (
line = flag.Int("line", -1, "line number in file (count starts at 1)")
col = flag.Int("column", -1, "column offset in line (count starts at 0)")
srcP = flag.String("source", "", "source file")
)
func main() {
flag.Parse()
var (
src *os.File
err error
)
// validate inputs
if *line < 1 || *col < 0 {
log.Fatal("invalid values for line and column")
}
switch {
case *srcP != "":
src, err = os.Open(*srcP)
if err != nil {
log.Fatalf("unable to open source file: %s", err)
}
default:
// use STDIN when no file is specified
src = os.Stdin
}
defer src.Close()
runeOffset, err := offset.CalcRune(src, *line, *col)
if err != nil {
log.Fatalf("unable to calculate rune offset: %s", err)
}
fmt.Fprintf(os.Stdout, "%d\n", runeOffset)
}