Skip to content

Commit

Permalink
Merge pull request #228 from mschneider82/refactor
Browse files Browse the repository at this point in the history
use happy path for better code readability
  • Loading branch information
justjanne authored Jan 7, 2021
2 parents e24fc64 + b24328d commit d7acc0b
Show file tree
Hide file tree
Showing 23 changed files with 300 additions and 278 deletions.
27 changes: 14 additions & 13 deletions segment-aws.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package main

import (
pwl "github.com/justjanne/powerline-go/powerline"
"os"

pwl "github.com/justjanne/powerline-go/powerline"
)

func segmentAWS(p *powerline) []pwl.Segment {
profile := os.Getenv("AWS_PROFILE")
region := os.Getenv("AWS_DEFAULT_REGION")
if profile != "" {
var r string
if region != "" {
r = " (" + region + ")"
}
return []pwl.Segment{{
Name: "aws",
Content: profile + r,
Foreground: p.theme.AWSFg,
Background: p.theme.AWSBg,
}}
if profile == "" {
return []pwl.Segment{}
}
var r string
if region != "" {
r = " (" + region + ")"
}
return []pwl.Segment{}
return []pwl.Segment{{
Name: "aws",
Content: profile + r,
Foreground: p.theme.AWSFg,
Background: p.theme.AWSBg,
}}
}
9 changes: 5 additions & 4 deletions segment-cwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ func getColor(p *powerline, pathSegment pathSegment, isLastDir bool) (uint8, uin
func segmentCwd(p *powerline) (segments []pwl.Segment) {
cwd := p.cwd

if p.cfg.CwdMode == "plain" {
switch p.cfg.CwdMode {
case "plain":
if strings.HasPrefix(cwd, p.userInfo.HomeDir) {
cwd = "~" + cwd[len(p.userInfo.HomeDir):]
}
Expand All @@ -172,7 +173,7 @@ func segmentCwd(p *powerline) (segments []pwl.Segment) {
Foreground: p.theme.CwdFg,
Background: p.theme.PathBg,
})
} else {
default:
pathSegments := cwdToPathSegments(p, cwd)

if p.cfg.CwdMode == "dironly" {
Expand Down Expand Up @@ -213,11 +214,11 @@ func segmentCwd(p *powerline) (segments []pwl.Segment) {
}
first := pathSegments[0]
pathSegments = make([]pathSegment, 0)
if (first.home || first.alias) {
if first.home || first.alias {
pathSegments = append(pathSegments, first)
}
pathSegments = append(pathSegments, pathSegment{
path: path,
path: path,
})
}
}
Expand Down
16 changes: 8 additions & 8 deletions segment-docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ func segmentDocker(p *powerline) []pwl.Segment {
}
}

if docker != "" {
return []pwl.Segment{{
Name: "docker",
Content: docker,
Foreground: p.theme.DockerMachineFg,
Background: p.theme.DockerMachineBg,
}}
if docker == "" {
return []pwl.Segment{}
}
return []pwl.Segment{}
return []pwl.Segment{{
Name: "docker",
Content: docker,
Foreground: p.theme.DockerMachineFg,
Background: p.theme.DockerMachineBg,
}}
}
19 changes: 10 additions & 9 deletions segment-dotenv.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
pwl "github.com/justjanne/powerline-go/powerline"
"os"

pwl "github.com/justjanne/powerline-go/powerline"
)

func segmentDotEnv(p *powerline) []pwl.Segment {
Expand All @@ -15,13 +16,13 @@ func segmentDotEnv(p *powerline) []pwl.Segment {
break
}
}
if dotEnv {
return []pwl.Segment{{
Name: "dotenv",
Content: "\u2235",
Foreground: p.theme.DotEnvFg,
Background: p.theme.DotEnvBg,
}}
if !dotEnv {
return []pwl.Segment{}
}
return []pwl.Segment{}
return []pwl.Segment{{
Name: "dotenv",
Content: "\u2235",
Foreground: p.theme.DotEnvFg,
Background: p.theme.DotEnvBg,
}}
}
75 changes: 38 additions & 37 deletions segment-duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func segmentDuration(p *powerline) []pwl.Segment {
durationValue := strings.Trim(p.cfg.Duration, "'\"")
durationMinValue := strings.Trim(p.cfg.DurationMin, "'\"")

hasPrecision := strings.Index(durationValue, ".") != -1
hasPrecision := strings.Contains(durationValue, ".")

durationFloat, err := strconv.ParseFloat(durationValue, 64)
durationMinFloat, _ := strconv.ParseFloat(durationMinValue, 64)
Expand All @@ -58,42 +58,43 @@ func segmentDuration(p *powerline) []pwl.Segment {

duration := time.Duration(durationFloat * float64(time.Second.Nanoseconds()))

if duration > 0 {
var content string
ns := duration.Nanoseconds()
if ns > hours {
hrs := ns / hours
ns -= hrs * hours
mins := ns / minutes
content = fmt.Sprintf("%dh %dm", hrs, mins)
} else if ns > minutes {
mins := ns / minutes
ns -= mins * minutes
secs := ns / seconds
content = fmt.Sprintf("%dm %ds", mins, secs)
} else if !hasPrecision {
secs := ns / seconds
content = fmt.Sprintf("%ds", secs)
} else if ns > seconds {
secs := ns / seconds
ns -= secs * seconds
millis := ns / milliseconds
content = fmt.Sprintf("%ds %dms", secs, millis)
} else if ns > milliseconds {
millis := ns / milliseconds
ns -= millis * milliseconds
micros := ns / microseconds
content = fmt.Sprintf("%dms %d\u00B5s", millis, micros)
} else {
content = fmt.Sprintf("%d\u00B5s", ns/microseconds)
}
if duration <= 0 {
return []pwl.Segment{}
}

return []pwl.Segment{{
Name: "duration",
Content: content,
Foreground: p.theme.DurationFg,
Background: p.theme.DurationBg,
}}
var content string
ns := duration.Nanoseconds()
if ns > hours {
hrs := ns / hours
ns -= hrs * hours
mins := ns / minutes
content = fmt.Sprintf("%dh %dm", hrs, mins)
} else if ns > minutes {
mins := ns / minutes
ns -= mins * minutes
secs := ns / seconds
content = fmt.Sprintf("%dm %ds", mins, secs)
} else if !hasPrecision {
secs := ns / seconds
content = fmt.Sprintf("%ds", secs)
} else if ns > seconds {
secs := ns / seconds
ns -= secs * seconds
millis := ns / milliseconds
content = fmt.Sprintf("%ds %dms", secs, millis)
} else if ns > milliseconds {
millis := ns / milliseconds
ns -= millis * milliseconds
micros := ns / microseconds
content = fmt.Sprintf("%dms %d\u00B5s", millis, micros)
} else {
content = fmt.Sprintf("%d\u00B5s", ns/microseconds)
}
return []pwl.Segment{}

return []pwl.Segment{{
Name: "duration",
Content: content,
Foreground: p.theme.DurationFg,
Background: p.theme.DurationBg,
}}
}
30 changes: 16 additions & 14 deletions segment-exitcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"fmt"
"strconv"

"github.com/justjanne/powerline-go/exitcode"
pwl "github.com/justjanne/powerline-go/powerline"
"strconv"
)

var exitCodes = map[int]string{
Expand All @@ -31,18 +32,19 @@ func getMeaningFromExitCode(exitCode int) string {

func segmentExitCode(p *powerline) []pwl.Segment {
var meaning string
if p.cfg.PrevError != 0 {
if p.cfg.NumericExitCodes {
meaning = strconv.Itoa(p.cfg.PrevError)
} else {
meaning = getMeaningFromExitCode(p.cfg.PrevError)
}
return []pwl.Segment{{
Name: "exit",
Content: meaning,
Foreground: p.theme.CmdFailedFg,
Background: p.theme.CmdFailedBg,
}}
if p.cfg.PrevError == 0 {
return []pwl.Segment{}
}
return []pwl.Segment{}
if p.cfg.NumericExitCodes {
meaning = strconv.Itoa(p.cfg.PrevError)
} else {
meaning = getMeaningFromExitCode(p.cfg.PrevError)
}

return []pwl.Segment{{
Name: "exit",
Content: meaning,
Foreground: p.theme.CmdFailedFg,
Background: p.theme.CmdFailedBg,
}}
}
20 changes: 10 additions & 10 deletions segment-gcp.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main

import (
pwl "github.com/justjanne/powerline-go/powerline"
"log"
"os/exec"
"strings"

pwl "github.com/justjanne/powerline-go/powerline"
)

func segmentGCP(p *powerline) []pwl.Segment {
Expand All @@ -14,14 +15,13 @@ func segmentGCP(p *powerline) []pwl.Segment {
}

project := strings.TrimSuffix(string(out), "\n")
if project != "" {
return []pwl.Segment{{
Name: "gcp",
Content: project,
Foreground: p.theme.GCPFg,
Background: p.theme.GCPBg,
}}
if project == "" {
return []pwl.Segment{}
}

return []pwl.Segment{}
return []pwl.Segment{{
Name: "gcp",
Content: project,
Foreground: p.theme.GCPFg,
Background: p.theme.GCPBg,
}}
}
3 changes: 2 additions & 1 deletion segment-git.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package main

import (
"fmt"
pwl "github.com/justjanne/powerline-go/powerline"
"os"
"os/exec"
"path"
"regexp"
"strconv"
"strings"

pwl "github.com/justjanne/powerline-go/powerline"
)

type repoStats struct {
Expand Down
9 changes: 5 additions & 4 deletions segment-gitlite.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
pwl "github.com/justjanne/powerline-go/powerline"
"strings"

pwl "github.com/justjanne/powerline-go/powerline"
)

func segmentGitLite(p *powerline) []pwl.Segment {
Expand All @@ -25,10 +26,10 @@ func segmentGitLite(p *powerline) []pwl.Segment {
status := strings.TrimSpace(out)
var branch string

if status != "HEAD" {
branch = status
} else {
if status == "HEAD" {
branch = getGitDetachedBranch(p)
} else {
branch = status
}

return []pwl.Segment{{
Expand Down
Loading

0 comments on commit d7acc0b

Please sign in to comment.