Skip to content
This repository has been archived by the owner on Aug 3, 2022. It is now read-only.

Commit

Permalink
Some more tweaks to output
Browse files Browse the repository at this point in the history
  • Loading branch information
eprovst committed Dec 16, 2018
1 parent fa8bfd5 commit f818f88
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
53 changes: 31 additions & 22 deletions pages/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,35 @@
package pages

import (
"bytes"
"fmt"
"strings"

"github.com/logrusorgru/aurora"
"go.etcd.io/bbolt"
)

const (
normal = aurora.Color(0)
heading = aurora.BoldFm
note = normal
description = aurora.GreenFg | aurora.BoldFm
verbatim = aurora.BoldFm | aurora.RedFg
example = normal
)

// Show shows help for a command
func Show(database *bbolt.DB, commands []string) {
// Get the page
err := database.View(
func(tx *bbolt.Tx) error {
// Open the pages bucket, creating it if it doesn't yet exist
tx.CreateBucketIfNotExists(pagesBucket)
buck := tx.Bucket(pagesBucket)
bucket := tx.Bucket(pagesBucket)

// Print all the given commands
for _, command := range commands {
page := buck.Get([]byte(command))
page := bucket.Get([]byte(command))
prettyPrint(command, page)
}

Expand All @@ -50,19 +60,17 @@ func Show(database *bbolt.DB, commands []string) {
func prettyPrint(command string, page []byte) {
// The page is not in the database
if page == nil {
fmt.Println()
fmt.Println(aurora.Bold(command), "documentation is not available.")
fmt.Println("Consider making a Pull Request to https://github.com/tldr-pages/tldr")
fmt.Println()
fmt.Print("\n ", aurora.Colorize(command, heading), " documentation is not available.")
fmt.Print("\n ", "Consider making a Pull Request to https://github.com/tldr-pages/tldr", "\n\n")
return
}

// Add an blank line in front of the page
fmt.Println()

// Pretty print the lines in the page
for _, line := range strings.Split(string(page), "\n") {
line = strings.TrimSpace(line)
for _, lineB := range bytes.Split(page, []byte{'\n'}) {
line := string(bytes.TrimSpace(lineB))

if len(line) == 0 {
// Skip empty lines
Expand All @@ -71,19 +79,20 @@ func prettyPrint(command string, page []byte) {

switch line[0] {
case '#':
processLine(line[1:], aurora.BoldFm)
fmt.Print(" ")
processLine(line[1:], heading)

case '>':
processLine(line[1:], 0)
fmt.Print(" ")
processLine(line[1:], note)

case '-':
fmt.Print("\n- ")
processLine(line[1:], aurora.GreenFg|aurora.BoldFm)
processLine(line[1:], description)

default:
// Print the parsed line
fmt.Print(" ")
processLine(line, 0)
processLine(line, normal)
}
}

Expand Down Expand Up @@ -126,25 +135,25 @@ func processLine(line string, defaultStyle aurora.Color) {
fmt.Println()
}

func processVerbatim(verbatim string) {
func processVerbatim(line string) {
// Our parsing method would fail on {{}} or }}{{, but as
// these a no-ops we can safely remove them.
verbatim = strings.Replace(verbatim, "{{}}", "", -1)
verbatim = strings.Replace(verbatim, "}}{{", "", -1)
line = strings.Replace(line, "{{}}", "", -1)
line = strings.Replace(line, "}}{{", "", -1)

inOptional := strings.HasPrefix(verbatim, "{{")
for _, segment := range strings.Split(verbatim, "{{") {
inExample := strings.HasPrefix(line, "{{")
for _, segment := range strings.Split(line, "{{") {
for _, part := range strings.Split(segment, "}}") {
if inOptional {
if inExample {
// Optional
fmt.Print(part)
fmt.Print(aurora.Colorize(part, example))

} else {
// Verbatim
fmt.Print(aurora.Red(part).Bold())
fmt.Print(aurora.Colorize(part, verbatim))
}

inOptional = !inOptional
inExample = !inExample
}
}

Expand Down
5 changes: 3 additions & 2 deletions pages/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ func downloadZip(url string) (*zip.Reader, error) {
return nil, err
}

defer resp.Body.Close()

// Read the entire body into a byte array
zipFile, err := ioutil.ReadAll(resp.Body)

// Close the body
resp.Body.Close()

if err != nil {
return nil, err
}
Expand Down

0 comments on commit f818f88

Please sign in to comment.