Skip to content

Commit

Permalink
display config and help
Browse files Browse the repository at this point in the history
  • Loading branch information
RoryQ committed Jun 7, 2021
1 parent cba1c89 commit 2c89895
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
README.md: cmd/til/main.go
go run build/usage/main.go
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ TIL-Prompt is an interactive terminal prompt for creating and managing a collect


# Installation
To install from source using golang 1.16
To install from source using golang 2.16

`go install github.com/roryq/til-prompt/cmd/til@v0.1.0`
```
go install github.com/roryq/til-prompt/cmd/til@vlatest
```

# Usage

Expand All @@ -15,5 +17,19 @@ To install from source using golang 1.16
Follow the prompts to save a new TIL entry.
A README.md is regenerated after each save.

```shell
Usage: til <command>

Flags:
-h, --help Show context-sensitive help.

Commands:
config
Displays the current configuration.

Run "til <command> --help" for more information on a command.

```

# License
[MIT](LICENSE)
28 changes: 28 additions & 0 deletions build/usage/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"fmt"
"io/ioutil"
"os/exec"
"regexp"
"strings"
)

func main() {
helpOutput, err := exec.Command("go", "run", "cmd/til/main.go", "--help").Output()
if err != nil {
panic(err)
}

readme, err := ioutil.ReadFile("README.md")
if err != nil {
panic(err)
}

re := regexp.MustCompile("```shell\\n[^`]+```")
matches := re.FindStringSubmatch(string(readme))
replaced := strings.ReplaceAll(string(readme), matches[0],
fmt.Sprintf("```shell\n%s\n```", helpOutput))

ioutil.WriteFile("README.md", []byte(replaced), 0644)
}
20 changes: 15 additions & 5 deletions cmd/til/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"fmt"
"os"

gap "github.com/muesli/go-app-paths"

"github.com/alecthomas/kong"
tea "github.com/charmbracelet/bubbletea"
gap "github.com/muesli/go-app-paths"

"github.com/roryq/til-prompt/pkg/tui"
)
Expand All @@ -18,15 +18,25 @@ var (
scope = gap.NewScope(gap.User, "til")
)

var CLI struct {
Config struct{} `cmd:"config" help:"Displays the current configuration."`
}

func main() {
config, err := tui.LoadConfig(scope)
if err != nil {
fmt.Printf("could not load config: %s\n", err)
os.Exit(1)
}

if err := tea.NewProgram(tui.NewUI(config)).Start(); err != nil {
fmt.Printf("could not start program: %s\n", err)
os.Exit(1)
ktx := kong.Parse(&CLI, kong.Name("til"))
switch ktx.Command() {
case "config":
fmt.Println(config.Sprint())
default:
if err := tea.NewProgram(tui.NewUI(config)).Start(); err != nil {
fmt.Printf("could not start program: %s\n", err)
os.Exit(1)
}
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/roryq/til-prompt
go 1.16

require (
github.com/alecthomas/kong v0.2.17
github.com/charmbracelet/bubbles v0.8.0
github.com/charmbracelet/bubbletea v0.14.0
github.com/charmbracelet/lipgloss v0.2.1
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/alecthomas/kong v0.2.17 h1:URDISCI96MIgcIlQyoCAlhOmrSw6pZScBNkctg8r0W0=
github.com/alecthomas/kong v0.2.17/go.mod h1:ka3VZ8GZNPXv9Ov+j4YNLkI8mTuhXyr/0ktSlqIydQQ=
github.com/atotto/clipboard v0.1.2 h1:YZCtFu5Ie8qX2VmVTBnrqLSiU9XOWwqNRmdT3gIQzbY=
github.com/atotto/clipboard v0.1.2/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/charmbracelet/bubbles v0.8.0 h1:+l2op90Ag37Vn+30O1hbg/0wBl+e+sxHhgY1F/rvdHs=
Expand All @@ -10,8 +12,9 @@ github.com/charmbracelet/lipgloss v0.2.1 h1:knjqLRtlcDsxss6i0CUhNLuXrNW3TQhFDXlX
github.com/charmbracelet/lipgloss v0.2.1/go.mod h1:uiZUfrHLQN14I0lJ5591WtcHyY1X76pOIPSaRKPY6dE=
github.com/containerd/console v1.0.1 h1:u7SFAJyRqWcG6ogaMAx3KjSTy1e3hT9QxqX7Jco7dRc=
github.com/containerd/console v1.0.1/go.mod h1:XUsP6YE/mKtz6bxc+I8UiKKTP04qjQL4qcS3XoQ5xkw=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/goterm v0.0.0-20190703233501-fc88cf888a3f/go.mod h1:nOFQdrUlIlx6M6ODdSpBj1NVA+VgLC6kmw60mkw34H4=
github.com/kennygrant/sanitize v1.2.4 h1:gN25/otpP5vAsO2djbMhF/LQX6R7+O1TB4yv8NzpJ3o=
github.com/kennygrant/sanitize v1.2.4/go.mod h1:LGsjYYtgxbetdg5owWB2mpgUL6e2nfw2eObZ0u0qvak=
Expand Down Expand Up @@ -63,5 +66,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
13 changes: 13 additions & 0 deletions pkg/tui/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/charmbracelet/lipgloss"

"gopkg.in/yaml.v2"

Expand Down Expand Up @@ -54,6 +57,16 @@ func LoadConfig(scope *gap.Scope) (config Config, err error) {
return config, err
}

func (c Config) Sprint() string {
b := &strings.Builder{}
headStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#04B575"))
bodyStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("244"))

b.WriteString(headStyle.Render("SaveDirectory: "))
b.WriteString(bodyStyle.Render(c.SaveDirectory))
return b.String()
}

func getDataPath(defaultPath string) string {
reader := bufio.NewScanner(os.Stdin)
fmt.Printf("No config found. Set the directory to store your til,"+
Expand Down

0 comments on commit 2c89895

Please sign in to comment.