Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
PKGS = $(shell go list ./...)
PKGS_WITH_TESTS := $(shell go list -f '{{if .TestGoFiles}}{{.ImportPath}}{{end}}' ./...)

VERSION := $(shell git describe --tags --dirty --always 2>/dev/null || echo "dev")
VERSION := $(shell git describe --tags --always 2>/dev/null || echo "dev")

#
# Build targets
Expand All @@ -17,6 +17,14 @@ build:
-o bin/tldr \
./main.go

.PHONY: install
install:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go install \
-trimpath \
-ldflags="-s -w -X github.com/TheRootDaemon/tlgc/version.Version=$(VERSION)" \
.

#
# Development targets
#
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# tlgc

A tldr client written in Go.

## Install

```sh
go install github.com/TheRootDaemon/tlgc@latest
```

## Usage

See `tlgc --help` for all options.
4 changes: 4 additions & 0 deletions cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,13 @@ type CLI struct {
// Compact strips empty lines from output.
Compact bool

NoCompact bool

// Raw prints pages in raw markdown.
Raw bool

NoRaw bool

// Quiet suppresses informational and warning messages.
Quiet bool

Expand Down
6 changes: 6 additions & 0 deletions cmd/count_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ func (v *countValue) String() string {
return fmt.Sprintf("%d", *v.count)
}

// IsBoolFlag returns true so flag.Parse treats --verbose as a boolean flag
// that does not consume the next argument.
func (v *countValue) IsBoolFlag() bool {
return true
}

// Set increments the counter each time the flag is encountered.
func (v *countValue) Set(string) error {
*v.count++
Expand Down
251 changes: 251 additions & 0 deletions cmd/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
package cmd

import (
"fmt"
"strings"

"github.com/TheRootDaemon/tlgc/termcolor"
"github.com/TheRootDaemon/tlgc/version"
)

func help() {
printUsage()
printFlags()
printFooter()
}

func printUsage() {
fmt.Printf(
"tlgc %s (implementing client specification v2.3)\n\n",
version.Version,
)
fmt.Printf(
"%s tldr [OPTIONS] [PAGE]...\n\n",
termcolor.Sprint("bold underline", "Usage:"),
)
fmt.Printf(
"%s\n",
termcolor.Sprint("bold underline", "Arguments:"),
)
fmt.Printf(" [PAGE]... The tldr page to show\n\n")
}

func printFlags() {
type flagEntry struct {
short string
long string
arg string
description string
}

flags := []flagEntry{
{
short: "-u",
long: "--update",
description: "Update the cache",
},
{
short: "-l",
long: "--list",
description: "List all pages in the current platform",
},
{
short: "-a",
long: "--list-all",
description: "List all pages",
},
{
short: "-s",
long: "--search",
arg: "<KEYWORD>", description: "Search for pages containing a keyword",
},
{
long: "--list-platforms",
description: "List available platforms",
},
{
long: "--list-languages",
description: "List installed languages",
},
{
short: "-i",
long: "--info",
description: "Show cache information",
},
{
short: "-r",
long: "--render",
arg: "<FILE>", description: "Render the specified tldr page",
},
{
long: "--clean-cache",
description: "Interactively delete contents of the cache directory",
},

{
long: "--gen-config",
description: "Print the default config",
},

{
long: "--config-path",
description: "Print the default config path",
},
{
short: "-p",
long: "--platform",
arg: "<PLATFORM>", description: "Specify the platform to use (linux, osx, windows, etc.)",
},
{
short: "-L",
long: "--language",
arg: "<LANGUAGE_CODE>", description: "Specify the languages to use",
},
{
long: "--short-options",
description: "Display short options wherever possible (e.g. '-s')",
},

{
long: "--long-options",
description: "Display long options wherever possible (e.g. '--long')",
},

{
long: "--edit",
description: "Display a link to edit the shown page on GitHub",
},

{
short: "-o",
long: "--offline",
description: "Do not update the cache, even if it is stale",
},
{
short: "-c",
long: "--compact",
description: "Strip empty lines from output",
},
{
long: "--no-compact",
description: "Do not strip empty lines from output (overrides --compact)",
},
{
short: "-R",
long: "--raw",
description: "Print pages in raw markdown instead of rendering them",
},
{long: "--no-raw", description: "Render pages instead of printing raw file contents (overrides --raw)"},
{
short: "-q",
long: "--quiet",
description: "Suppress status messages and warnings",
},
{long: "--verbose...", description: "Be more verbose (can be specified twice)"},
{
long: "--color",
arg: "<WHEN>",
description: "Specify when to enable color [default: auto] [possible values: auto, always, never]",
},
{
long: "--config",
arg: "<FILE>",
description: "Specify an alternative path to the config file",
},
{
short: "-v",
long: "--version",
description: "Print version",
},
{
short: "-h",
long: "--help",
description: "Print help",
},
}

maxShort, maxLong := 0, 0
for _, f := range flags {
updateColumnWidths(
&maxShort,
&maxLong,
f.short,
f.long,
f.arg,
)
}

fmt.Printf(
"%s\n",
termcolor.Sprint("bold underline", "Options:"),
)

for _, f := range flags {
printFlag(
maxShort,
maxLong,
f.short,
f.long,
f.arg,
f.description,
)
}
}

func printFooter() {
fmt.Printf("\nSee https://github.com/TheRootDaemon/tlgc for more information.\n")
}

func updateColumnWidths(
maxShort,
maxLong *int,
short, long, arg string,
) {
shortWidth := len(short)
if short != "" && long != "" {
shortWidth++
}

*maxShort = max(*maxShort, shortWidth)

if long == "" {
return
}

longWidth := len(long) + 1
if arg != "" {
longWidth += len(arg)
}

*maxLong = max(*maxLong, longWidth)
}

func printFlag(
maxShort,
maxLong int,
short, long, arg, description string,
) {
shortText := short
if shortText != "" && long != "" {
shortText += ","
}

longText := long
if arg != "" {
longText += " " + arg
}

longDisplay := termcolor.Sprint("bold", long)
if arg != "" {
longDisplay += " " + arg
}

fmt.Printf(
" %s%s %s%s %s\n",
termcolor.Sprint("bold", shortText),
strings.Repeat(" ", maxShort-len(shortText)),
longDisplay,
strings.Repeat(" ", maxLong-len(longText)),
" "+description,
)
}
Loading
Loading