1
0
mirror of https://github.com/golang/go synced 2024-11-18 20:04:52 -07:00
go/usr/gri/pretty/untab.go

59 lines
1.1 KiB
Go
Raw Normal View History

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag";
"fmt";
"io";
"os";
"tabwriter";
)
var (
tabwidth = flag.Int("tabwidth", 4, "tab width");
usetabs = flag.Bool("usetabs", false, "align with tabs instead of blanks");
)
func error(format string, params ...) {
fmt.Printf(format, params);
os.Exit(1);
}
func untab(name string, src *os.File, dst *tabwriter.Writer) {
n, err := io.Copy(src, dst);
if err != nil {
error("error while processing %s (%v)", name, err);
}
//dst.Flush();
}
func main() {
flag.Parse();
padchar := byte(' ');
if *usetabs {
padchar = '\t';
}
dst := tabwriter.NewWriter(os.Stdout, *tabwidth, 1, padchar, 0);
if flag.NArg() > 0 {
for i := 0; i < flag.NArg(); i++ {
name := flag.Arg(i);
src, err := os.Open(name, os.O_RDONLY, 0);
if err != nil {
error("could not open %s (%v)\n", name, err);
}
untab(name, src, dst);
src.Close(); // ignore errors
}
} else {
// no files => use stdin
untab("/dev/stdin", os.Stdin, dst);
}
}