Skip to content

Commit

Permalink
fix: add a left border before exec logs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Sep 20, 2023
1 parent 9e120d8 commit 3998796
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 24 deletions.
11 changes: 10 additions & 1 deletion internal/git/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@ import (
)

type Exec interface {
SetRootPath(root string)
Cmd(cmd string) (string, error)
CmdArgs(args ...string) (string, error)
CmdLines(cmd string) ([]string, error)
RawCmd(cmd string) (string, error)
}

type OsExec struct{}
type OsExec struct {
root string
}

// NewOsExec returns an object that executes given commands
// in the OS.
func NewOsExec() *OsExec {
return &OsExec{}
}

func (o *OsExec) SetRootPath(root string) {
o.root = root
}

// Cmd runs plain string command. Trims spaces around output.
func (o *OsExec) Cmd(cmd string) (string, error) {
args := strings.Split(cmd, " ")
Expand Down Expand Up @@ -68,9 +75,11 @@ func (o *OsExec) rawExecArgs(args ...string) (string, error) {
log.Debug("[lefthook] cmd: ", args)

cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = o.root
cmd.Env = append(os.Environ(), "LEFTHOOK=0")

out, err := cmd.CombinedOutput()
log.Debug("[lefthook] dir: ", o.root)
log.Debug("[lefthook] err: ", err)
log.Debug("[lefthook] out: ", string(out))
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions internal/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func NewRepository(fs afero.Fs, git Exec) (*Repository, error) {
gitPath = filepath.Join(rootPath, gitPath)
}

git.SetRootPath(rootPath)

return &Repository{
Fs: fs,
Git: git,
Expand Down Expand Up @@ -317,6 +319,9 @@ func (r *Repository) extractFiles(lines []string) ([]string, error) {
}

func (r *Repository) isFile(path string) (bool, error) {
if !strings.HasPrefix(path, r.RootPath) {
path = filepath.Join(r.RootPath, path)
}
stat, err := r.Fs.Stat(path)
if err != nil {
if os.IsNotExist(err) {
Expand Down
2 changes: 2 additions & 0 deletions internal/git/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type GitMock struct {
cases map[string]string
}

func (g GitMock) SetRootPath(_root string) {}

func (g GitMock) Cmd(cmd string) (string, error) {
res, err := g.RawCmd(cmd)
if err != nil {
Expand Down
15 changes: 2 additions & 13 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,8 @@ func (l *Lefthook) Run(hookName string, args RunArgs, gitArgs []string) error {

if !logSettings.SkipMeta() {
log.Box(
strings.Join(
[]string{
log.Cyan("🥊 lefthook"),
log.Gray(fmt.Sprintf("v%s", version.Version(false))),
},
" ",
),
strings.Join(
[]string{
log.Gray("hook:"),
log.Bold(hookName),
}, " ",
),
log.Cyan("🥊 lefthook ")+log.Gray(fmt.Sprintf("v%s", version.Version(false))),
log.Gray("hook: ")+log.Bold(hookName),
)
}

Expand Down
25 changes: 17 additions & 8 deletions internal/lefthook/run/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"sync"
"sync/atomic"

"github.com/charmbracelet/lipgloss"
"github.com/spf13/afero"

"github.com/evilmartians/lefthook/internal/config"
Expand All @@ -29,6 +30,7 @@ type status int8
const (
executableFileMode os.FileMode = 0o751
executableMask os.FileMode = 0o111
execLogPadding = 2
)

var surroundingQuotesRegexp = regexp.MustCompile(`^'(.*)'$`)
Expand Down Expand Up @@ -478,11 +480,14 @@ func (r *Runner) logSkip(name, reason string) {
return
}

log.Info(
log.Cyan(fmt.Sprintf("\n %s", log.Bold(name))),
log.Gray("(skip)"),
log.Yellow(reason),
)
log.Styled().
WithLeftBorder(lipgloss.NormalBorder()).
WithPadding(execLogPadding).
Info(
log.Cyan(log.Bold(name)) + " " +
log.Gray("(skip)") + " " +
log.Yellow(reason),
)
}

func (r *Runner) logExecute(name string, err error, out io.Reader) {
Expand All @@ -495,13 +500,17 @@ func (r *Runner) logExecute(name string, err error, out io.Reader) {
case r.SkipSettings.SkipExecutionInfo():
execLog = ""
case err != nil:
execLog = log.Red(fmt.Sprintf("\n %s > ", name))
execLog = log.Red(fmt.Sprintf("%s > ", name))
default:
execLog = log.Cyan(fmt.Sprintf("\n %s > ", name))
execLog = log.Cyan(fmt.Sprintf("%s > ", name))
}

if execLog != "" {
log.Info(execLog)
log.Styled().
WithLeftBorder(lipgloss.ThickBorder()).
WithPadding(execLogPadding).
Info(execLog)
log.Info()
}

if err == nil && r.SkipSettings.SkipExecutionOutput() {
Expand Down
2 changes: 2 additions & 0 deletions internal/lefthook/run/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type GitMock struct {
commands []string
}

func (g *GitMock) SetRootPath(_root string) {}

func (g *GitMock) Cmd(cmd string) (string, error) {
g.mux.Lock()
g.commands = append(g.commands, cmd)
Expand Down
2 changes: 2 additions & 0 deletions internal/lefthook/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

type GitMock struct{}

func (g GitMock) SetRootPath(_root string) {}

func (g GitMock) Cmd(_cmd string) (string, error) {
return "", nil
}
Expand Down
53 changes: 51 additions & 2 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ const (
spinnerText = " waiting"
)

type StyleLogger struct {
style lipgloss.Style
}

type Logger struct {
level Level
out io.Writer
Expand Down Expand Up @@ -90,6 +94,33 @@ func StopSpinner() {
std.spinner.Stop()
}

func Styled() StyleLogger {
return StyleLogger{
style: lipgloss.NewStyle(),
}
}

func (s StyleLogger) WithLeftBorder(border lipgloss.Border) StyleLogger {
s.style = s.style.BorderStyle(border).BorderLeft(true).BorderForeground(colorCyan)

return s
}

func (s StyleLogger) WithPadding(m int) StyleLogger {
s.style = s.style.PaddingLeft(m)

return s
}

func (s StyleLogger) Info(str string) {
Info(
lipgloss.JoinVertical(
lipgloss.Left,
s.style.Render(str),
),
)
}

func Debug(args ...interface{}) {
res := fmt.Sprint(args...)
std.Debug(color(colorGray).Render(res))
Expand All @@ -103,6 +134,16 @@ func Info(args ...interface{}) {
std.Info(args...)
}

func InfoPad(s string) {
Info(
lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderLeft(true).
BorderForeground(colorCyan).
Render(s),
)
}

func Infof(format string, args ...interface{}) {
std.Infof(format, args...)
}
Expand Down Expand Up @@ -217,8 +258,16 @@ func Box(left, right string) {
Info(
lipgloss.JoinHorizontal(
lipgloss.Top,
lipgloss.NewStyle().Border(lipgloss.RoundedBorder(), true, false, true, true).BorderForeground(colorBorder).Padding(0, 1).Render(left),
lipgloss.NewStyle().Border(lipgloss.RoundedBorder(), true, true, true, false).BorderForeground(colorBorder).Padding(0, 1).Render(right),
lipgloss.NewStyle().
Border(lipgloss.RoundedBorder(), true, false, true, true).
BorderForeground(colorBorder).
Padding(0, 1).
Render(left),
lipgloss.NewStyle().
Border(lipgloss.RoundedBorder(), true, true, true, false).
BorderForeground(colorBorder).
Padding(0, 1).
Render(right),
),
)
}
Expand Down

0 comments on commit 3998796

Please sign in to comment.