1
0
This repository has been archived on 2023-12-27. You can view files and clone it, but cannot push or open issues or pull requests.
phone-number-normalizer/normalizer.go

74 lines
1.1 KiB
Go
Raw Normal View History

2015-05-16 23:22:38 -07:00
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"time"
)
func getFromClipboard() string {
var out bytes.Buffer
xsel := exec.Command("xsel")
xsel.Stdout = &out
err := xsel.Run()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
return out.String()
}
func writeToClipboard(s string) {
xsel := exec.Command("xclip", "-selection", "clipboard")
xsel.Stdin = strings.NewReader(s)
err := xsel.Run()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
func main() {
replacer := strings.NewReplacer(
"(", "",
")", "",
"-", "",
".", "",
" ", "",
)
previous := getFromClipboard()
for {
result := getFromClipboard()
if result == previous || result == "" {
time.Sleep(100 * time.Millisecond)
continue
}
previous = result
if strings.HasPrefix(result, "1") {
result = result[1:]
}
if strings.HasPrefix(result, "+1") {
result = result[2:]
}
result = replacer.Replace(result)
if len(result) != 10 {
fmt.Println("Not a phone number:", result)
continue
}
result = fmt.Sprintf("+1%s", result)
writeToClipboard(result)
fmt.Println("Converted", previous, "into", result)
}
}