1// Command mc prints in multiple columns. 2// 3// Usage: mc [-] [-N] [file...] 4// 5// Mc splits the input into as many columns as will fit in N 6// print positions. If the output is a tty, the default N is 7// the number of characters in a terminal line; otherwise the 8// default N is 80. Under option - each input line ending in 9// a colon ':' is printed separately. 10package main 11 12import ( 13 "github.com/kr/pty" 14 "github.com/kr/text/colwriter" 15 "io" 16 "log" 17 "os" 18 "strconv" 19) 20 21func main() { 22 var width int 23 var flag uint 24 args := os.Args[1:] 25 for len(args) > 0 && len(args[0]) > 0 && args[0][0] == '-' { 26 if len(args[0]) > 1 { 27 width, _ = strconv.Atoi(args[0][1:]) 28 } else { 29 flag |= colwriter.BreakOnColon 30 } 31 args = args[1:] 32 } 33 if width < 1 { 34 _, width, _ = pty.Getsize(os.Stdout) 35 } 36 if width < 1 { 37 width = 80 38 } 39 40 w := colwriter.NewWriter(os.Stdout, width, flag) 41 if len(args) > 0 { 42 for _, s := range args { 43 if f, err := os.Open(s); err == nil { 44 copyin(w, f) 45 f.Close() 46 } else { 47 log.Println(err) 48 } 49 } 50 } else { 51 copyin(w, os.Stdin) 52 } 53} 54 55func copyin(w *colwriter.Writer, r io.Reader) { 56 if _, err := io.Copy(w, r); err != nil { 57 log.Println(err) 58 } 59 if err := w.Flush(); err != nil { 60 log.Println(err) 61 } 62} 63