func summary(colour bool) { text := "TMSU" if colour { text = ansi.Bold(text) } fmt.Println(text) fmt.Println() var maxWidth int for _, command := range helpCommands { maxWidth = int(math.Max(float64(maxWidth), float64(len(command.Name)))) } for _, command := range helpCommands { if command.Hidden && log.Verbosity < 2 { continue } synopsis := command.Synopsis if !colour { synopsis = ansi.Strip(synopsis) } line := fmt.Sprintf(" %-*v %v", maxWidth, command.Name, synopsis) if command.Hidden && colour { line = ansi.Yellow(line) } terminal.PrintWrapped(line) } fmt.Println() text = "Global options:" if colour { text = ansi.Bold(text) } fmt.Println(text) fmt.Println() printOptions(globalOptions) fmt.Println() terminal.PrintWrapped("Specify subcommand name for detailed help on a particular subcommand, e.g. tmsu help files") }
func PrintColumnsWidth(items []string, width int) { ansi.Sort(items) padding := 2 // minimum column padding var colWidths []int var calcWidth int cols := 0 rows := 1 // add a row until everything fits or we have every item on its own row for calcWidth = width + 1; calcWidth > width && rows <= len(items); rows++ { cols = 0 colWidths = make([]int, 0, width) calcWidth = -padding // last column has no padding // try to place items into columns for index, item := range items { col := index / rows if col >= len(colWidths) { // add column cols++ colWidths = append(colWidths, 0) calcWidth += padding } itemLength := len(ansi.Strip(item)) if itemLength > colWidths[col] { // widen column calcWidth += -colWidths[col] + itemLength colWidths[col] = itemLength } if calcWidth > width { // exceeded width break } } } rows-- // apportion any remaining space between the columns if cols > 2 && rows > 1 { padding = (width-calcWidth)/(cols-1) + 2 if padding < 2 { padding = 2 } } // render for rowIndex := 0; rowIndex < rows; rowIndex++ { for columnIndex := 0; columnIndex < cols; columnIndex++ { itemIndex := rows*columnIndex + rowIndex if itemIndex >= len(items) { break } item := items[itemIndex] fmt.Print(item) if columnIndex < cols-1 { itemLength := len(ansi.Strip(item)) padding := (colWidths[columnIndex] + padding) - itemLength fmt.Print(strings.Repeat(" ", padding)) } } fmt.Println() } }
func describeCommand(commandName string, colour bool) { command := findCommand(helpCommands, commandName) if command == nil { fmt.Printf("No such command '%v'.\n", commandName) return } // usages for _, usage := range command.Usages { if colour { usage = ansi.Bold(usage) } terminal.PrintWrapped(usage) } // description fmt.Println() description := colorize(command.Description) if !colour { description = ansi.Strip(description) } terminal.PrintWrapped(description) // examples if command.Examples != nil && len(command.Examples) > 0 { fmt.Println() text := "Examples:" if colour { text = ansi.Bold(text) } fmt.Println(text) fmt.Println() for _, example := range command.Examples { example = " " + strings.Replace(example, "\n", "\n ", -1) // preserve indent terminal.PrintWrapped(example) } } // aliases if command.Aliases != nil && len(command.Aliases) > 0 { fmt.Println() if command.Aliases != nil { text := "Aliases:" if colour { text = ansi.Bold(text) } fmt.Print(text) for _, alias := range command.Aliases { fmt.Print(" " + alias) } } fmt.Println() } // options if command.Options != nil && len(command.Options) > 0 { fmt.Println() text := "Options:" if colour { text = ansi.Bold(text) } fmt.Println(text) fmt.Println() printOptions(command.Options) } }
func PrintWrappedWidth(text string, maxWidth int) { if maxWidth == 0 { fmt.Println(string(text)) return } word := "" width := 0 indent := 0 for _, r := range string(text) + string(ETX) { if r == ' ' || r == '\n' || r == ETX { // tabulation if ansi.Strip(word) == "" && r == ' ' { fmt.Print(" ") width += 1 indent = width continue } charsNeeded := len(word) if width > 0 { charsNeeded += 1 // space } if width+charsNeeded > maxWidth { // wrap onto new line fmt.Println() width = 0 if indent > 0 { // print indent on new line fmt.Print(strings.Repeat(" ", indent)) width += indent } } else { if width > indent { // add space between words fmt.Print(" ") width += 1 } } if ansi.Strip(word) == "" && indent > 0 { // print indent on new line fmt.Print(strings.Repeat(" ", indent)) width += indent } fmt.Print(word) width += len(ansi.Strip(word)) word = "" if r == '\n' { // start a new line fmt.Println() width = 0 indent = 0 } } else { // add character to word word += string(r) } } fmt.Println() }