Use glamour and termev to render/colorize content #181

Merged
lunny merged 9 commits from 6543/tea:use-glamour into master 2020-09-19 16:00:55 +00:00
4 changed files with 44 additions and 5 deletions
Showing only changes of commit 763aeb5e68 - Show all commits

@ -100,12 +100,15 @@ func init() {
yamlConfigPath = filepath.Join(dir, "tea.yml") yamlConfigPath = filepath.Join(dir, "tea.yml")
} }
func splitRepo(repoPath string) (string, string) { func getOwnerAndRepo(repoPath, user string) (string, string) {
if len(repoPath) == 0 {
return "", ""
}
p := strings.Split(repoPath, "/") p := strings.Split(repoPath, "/")
if len(p) >= 2 { if len(p) >= 2 {
return p[0], p[1] return p[0], p[1]
} }
return repoPath, "" return user, repoPath
} }
func getActiveLogin() (*Login, error) { func getActiveLogin() (*Login, error) {

@ -7,6 +7,8 @@ package cmd
import ( import (
"log" "log"
"code.gitea.io/tea/modules/utils"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@ -81,16 +83,29 @@ var AllDefaultFlags = append([]cli.Flag{
// initCommand returns repository and *Login based on flags // initCommand returns repository and *Login based on flags
func initCommand() (*Login, string, string) { func initCommand() (*Login, string, string) {
var login *Login
err := loadConfig(yamlConfigPath) err := loadConfig(yamlConfigPath)
if err != nil { if err != nil {
log.Fatal("load config file failed ", yamlConfigPath) log.Fatal("load config file failed ", yamlConfigPath)
} }
login, repoPath, err := curGitRepoPath(repoValue) if login, err = getActiveLogin(); err != nil {
log.Fatal(err.Error())
}
exist, err := utils.PathExists(repoValue)
if err != nil { if err != nil {
log.Fatal(err.Error()) log.Fatal(err.Error())
} }
if exist || len(repoValue) == 0 {
login, repoValue, err = curGitRepoPath(repoValue)
if err != nil {
log.Fatal(err.Error())
}
}
if loginValue != "" { if loginValue != "" {
login = getLoginByName(loginValue) login = getLoginByName(loginValue)
if login == nil { if login == nil {
@ -98,7 +113,7 @@ func initCommand() (*Login, string, string) {
} }
} }
owner, repo := splitRepo(repoPath) owner, repo := getOwnerAndRepo(repoValue, login.User)
return login, owner, repo return login, owner, repo
} }

@ -250,7 +250,7 @@ var CmdLabelDelete = cli.Command{
Name: "delete", Name: "delete",
Usage: "Delete a label", Usage: "Delete a label",
Description: `Delete a label`, Description: `Delete a label`,
Action: runLabelCreate, Action: runLabelDelete,
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.IntFlag{ &cli.IntFlag{
Name: "id", Name: "id",

21
modules/utils/path.go Normal file

@ -0,0 +1,21 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package utils
import (
"os"
)
// PathExists returns whether the given file or directory exists or not
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}