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 "io" 14 "log" 15 "os" 16 "strconv" 17 18 "github.com/creack/pty" 19 "github.com/kr/text/colwriter" 20) 21 22func main() { 23 var width int 24 var flag uint 25 args := os.Args[1:] 26 for len(args) > 0 && len(args[0]) > 0 && args[0][0] == '-' { 27 if len(args[0]) > 1 { 28 width, _ = strconv.Atoi(args[0][1:]) 29 } else { 30 flag |= colwriter.BreakOnColon 31 } 32 args = args[1:] 33 } 34 if width < 1 { 35 _, width, _ = pty.Getsize(os.Stdout) 36 } 37 if width < 1 { 38 width = 80 39 } 40 41 w := colwriter.NewWriter(os.Stdout, width, flag) 42 if len(args) > 0 { 43 for _, s := range args { 44 if f, err := os.Open(s); err == nil { 45 copyin(w, f) 46 f.Close() 47 } else { 48 log.Println(err) 49 } 50 } 51 } else { 52 copyin(w, os.Stdin) 53 } 54} 55 56func copyin(w *colwriter.Writer, r io.Reader) { 57 if _, err := io.Copy(w, r); err != nil { 58 log.Println(err) 59 } 60 if err := w.Flush(); err != nil { 61 log.Println(err) 62 } 63} 64