Skip to content

Instantly share code, notes, and snippets.

@suyash
Last active December 8, 2016 07:45
Show Gist options
  • Save suyash/a20b68a230e55a7314bedbf526c6018c to your computer and use it in GitHub Desktop.
Save suyash/a20b68a230e55a7314bedbf526c6018c to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
)
func replace(s string) string {
i := 0
ans := ""
for i < len(s)-1 {
if s[i] == ' ' && s[i+1] == ' ' {
ans += "\t"
i += 2
} else {
break
}
}
ans += s[i:]
return ans
}
func format(file string) {
f, err := os.Open(file)
if err != nil {
panic(err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
tfile := file + ".tmp"
out, err := os.Create(tfile)
if err != nil {
panic(err)
}
defer out.Close()
for scanner.Scan() {
l := scanner.Text()
rl := replace(l)
out.Write([]byte(rl + "\n"))
}
os.Remove(file)
os.Rename(tfile, file)
}
func main() {
if len(os.Args) < 2 {
panic("Wrong Usage, Expected a Directory Path or a Single File")
}
filepath.Walk(os.Args[1], func(path string, info os.FileInfo, err error) error {
fmt.Println(info.Name())
if !info.IsDir() {
format(path)
}
return err
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment