Permalink
Browse files

Use strings.TrimSuffix(s, "\n") instead of trim.LastNewline(s).

Also use strings.TrimPrefix(s, " ") instead of trim.FirstSpace(s).

This is simpler and more readable, since package strings is a part of
the standard library.

Done with:

	gofmt -w -r 'trim.LastNewline(s) -> strings.TrimSuffix(s, "\n")' .
	gofmt -w -r 'trim.FirstSpace(s) -> strings.TrimPrefix(s, " ")' .
	goimports -w .
  • Loading branch information...
dmitshur committed Dec 15, 2018
1 parent e9d7946 commit 8b9b25c33dce044ca3eb8a2495818af39bc25c9f
Showing with 28 additions and 32 deletions.
  1. +12 −13 main.go
  2. +5 −5 pkg/legacyvcs/git.go
  3. +4 −6 pkg/legacyvcs/hg.go
  4. +7 −8 pkg/u6/main.go
25 main.go
@@ -68,7 +68,6 @@ import (
"github.com/shurcooL/go/printerutil"
"github.com/shurcooL/go/reflectfind"
"github.com/shurcooL/go/reflectsource"
"github.com/shurcooL/go/trim"
"github.com/shurcooL/httpfs/union"
"github.com/shurcooL/httpgzip"
"github.com/shurcooL/markdownfmt/markdown"
@@ -377,7 +376,7 @@ type Test2Widget struct {
}

func NewTest2Widget(pos mgl64.Vec2, field *float64) *Test2Widget {
return &Test2Widget{TextBoxWidget: NewTextBoxWidgetExternalContent(pos, NewMultilineContentFuncInstant(func() string { return trim.LastNewline(goon.Sdump(*field)) }), nil), field: field}
return &Test2Widget{TextBoxWidget: NewTextBoxWidgetExternalContent(pos, NewMultilineContentFuncInstant(func() string { return strings.TrimSuffix(goon.Sdump(*field), "\n") }), nil), field: field}
}

func (w *Test2Widget) Hit(ParentPosition mgl64.Vec2) []Widgeter {
@@ -1132,7 +1131,7 @@ func (this *GoCompileErrorsTest) Update() {
lineIndex := lineNumber - 1 // Convert line number (e.g. 1) to line index (e.g. 0)

in = in[x+1:]
message := trim.FirstSpace(in)
message := strings.TrimPrefix(in, " ")

return GoCompilerError{FileUri: FileUri("file://" + fileUri), ErrorMessage: GoErrorMessage{LineIndex: lineIndex, Message: message}}
}
@@ -4146,8 +4145,8 @@ func newVfsListingPureWidget(vfs vfs.FileSystem, path string) Widgeter {

w := &VfsListingPureWidget{
FilterableSelecterWidget: NewSelecterWidget(np, ss, &SelecterWidgetOptions{SelecterWidgetType: OptionalSelection}),
vfs: vfs,
path: path,
vfs: vfs,
path: path,
}
w.FilterableSelecterWidget.SelectionChangedPost = func() { w.selectionChangedTest() }

@@ -4171,8 +4170,8 @@ func newVfsListingPureWidgetWithSelection(vfs vfs.FileSystem, path string) Widge

w := &VfsListingPureWidget{
FilterableSelecterWidget: NewSelecterWidget(np, ss, &SelecterWidgetOptions{SelecterWidgetType: SingleSelection}),
vfs: vfs,
path: path,
vfs: vfs,
path: path,
}
w.FilterableSelecterWidget.SelectionChangedPost = func() { w.selectionChangedTest() }

@@ -4827,7 +4826,7 @@ func NewTextLabelWidgetString(pos mgl64.Vec2, s string) *TextLabelWidget {
}

func NewTextLabelWidgetGoon(pos mgl64.Vec2, any interface{}) *TextLabelWidget {
mc := NewMultilineContentFuncInstant(func() string { return trim.LastNewline(goon.Sdump(any)) })
mc := NewMultilineContentFuncInstant(func() string { return strings.TrimSuffix(goon.Sdump(any), "\n") })
return NewTextLabelWidgetExternalContent(pos, mc)
}

@@ -6372,7 +6371,7 @@ func initHttpHandlers() {
cmd := exec.Command("git", "diff", "--stat", "--find-renames", "master")
cmd.Dir = goPackage.Dir.Repo.Vcs.RootPath()
if stat, err := cmd.CombinedOutput(); err == nil {
b += "\n```\n" + trim.LastNewline(string(stat)) + "\n```\n"
b += "\n```\n" + strings.TrimSuffix(string(stat), "\n") + "\n```\n"
}
}

@@ -6448,7 +6447,7 @@ func initHttpHandlers() {
cmd := exec.Command("git", "diff", "--stat", "--find-renames", "HEAD")
cmd.Dir = goPackage.Dir.Repo.Vcs.RootPath()
if stat, err := cmd.CombinedOutput(); err == nil {
b += "\n```\n" + trim.LastNewline(string(stat)) + "\n```\n"
b += "\n```\n" + strings.TrimSuffix(string(stat), "\n") + "\n```\n"
}
b += "\n```diff\n" + workingDiff + "\n```\n"
}
@@ -8602,7 +8601,7 @@ func DrawCircle(pos mathgl.Vec2d, size mathgl.Vec2d) {
for _, widget := range mousePointer.Mapping {
out += fmt.Sprintf("%T\n", widget)
}
return trim.LastNewline(out)
return strings.TrimSuffix(out, "\n")
}
w = append(w, NewCollapsibleWidget(np, NewTextLabelWidgetExternalContent(np, NewMultilineContentFuncInstant(contentFunc)), "Mouse Mapping"))
}
@@ -8611,7 +8610,7 @@ func DrawCircle(pos mathgl.Vec2d, size mathgl.Vec2d) {
for _, widget := range mousePointer.OriginMapping {
out += fmt.Sprintf("%T\n", widget)
}
return trim.LastNewline(out)
return strings.TrimSuffix(out, "\n")
}
w = append(w, NewCollapsibleWidget(np, NewTextLabelWidgetExternalContent(np, NewMultilineContentFuncInstant(contentFunc)), "Mouse Origin Mapping"))
}
@@ -8620,7 +8619,7 @@ func DrawCircle(pos mathgl.Vec2d, size mathgl.Vec2d) {
for _, widget := range keyboardPointer.OriginMapping {
out += fmt.Sprintf("%T\n", widget)
}
return trim.LastNewline(out)
return strings.TrimSuffix(out, "\n")
}
w = append(w, NewCollapsibleWidget(np, NewTextLabelWidgetExternalContent(np, NewMultilineContentFuncInstant(contentFunc)), "Keyboard Origin Mapping"))
}
@@ -3,9 +3,9 @@ package legacyvcs
import (
"os"
"os/exec"
"strings"

"github.com/shurcooL/go/osutil"
"github.com/shurcooL/go/trim"
)

type gitVcs struct {
@@ -41,7 +41,7 @@ func (this *gitVcs) GetRemote() string {
cmd.Dir = this.rootPath

if out, err := cmd.Output(); err == nil {
return trim.LastNewline(string(out))
return strings.TrimSuffix(string(out), "\n")
} else {
return ""
}
@@ -57,7 +57,7 @@ func (this *gitVcs) GetLocalBranch() string {

if out, err := cmd.Output(); err == nil {
// Since rev-parse is considered porcelain and may change, need to error-check its output.
return trim.LastNewline(string(out))
return strings.TrimSuffix(string(out), "\n")
} else {
return ""
}
@@ -109,7 +109,7 @@ func (this *gitVcs) IsContained(rev string) bool {
cmd.Dir = this.rootPath

if out, err := cmd.Output(); err == nil {
if len(out) >= 2 && trim.LastNewline(string(out[2:])) == this.GetDefaultBranch() {
if len(out) >= 2 && strings.TrimSuffix(string(out[2:]), "\n") == this.GetDefaultBranch() {
return true
}
}
@@ -125,7 +125,7 @@ func getGitRepoRoot(path string) (isGitRepo bool, rootPath string) {

if out, err := cmd.Output(); err == nil {
// Since rev-parse is considered porcelain and may change, need to error-check its output
return true, trim.LastNewline(string(out))
return true, strings.TrimSuffix(string(out), "\n")
} else {
return false, ""
}
@@ -4,16 +4,14 @@ import (
"log"
"os/exec"
"strings"

trim "github.com/shurcooL/go/trim"
)

func getHgRepoRoot(path string) (isHgRepo bool, rootPath string) {
cmd := exec.Command("hg", "root")
cmd.Dir = path

if out, err := cmd.Output(); err == nil {
return true, trim.LastNewline(string(out))
return true, strings.TrimSuffix(string(out), "\n")
} else {
return false, ""
}
@@ -46,7 +44,7 @@ func (this *hgVcs) GetRemote() string {
cmd.Dir = this.rootPath

if out, err := cmd.Output(); err == nil {
return trim.LastNewline(string(out))
return strings.TrimSuffix(string(out), "\n")
} else {
return ""
}
@@ -61,7 +59,7 @@ func (this *hgVcs) GetLocalBranch() string {
cmd.Dir = this.rootPath

if out, err := cmd.Output(); err == nil {
return trim.LastNewline(string(out))
return strings.TrimSuffix(string(out), "\n")
} else {
return ""
}
@@ -94,7 +92,7 @@ func (this *hgVcs) GetRemoteRev() string {

if out, err := cmd.Output(); err == nil {
// Get the last line of output.
lines := strings.Split(trim.LastNewline(string(out)), "\n") // Always returns at least 1 element.
lines := strings.Split(strings.TrimSuffix(string(out), "\n"), "\n") // Always returns at least 1 element.
return lines[len(lines)-1]
}
return ""
@@ -11,7 +11,6 @@ import (
"github.com/shurcooL-legacy/Conception-go/pkg/gist7480523"
"github.com/shurcooL-legacy/Conception-go/pkg/legacyvcs"
"github.com/shurcooL/go/pipeutil"
"github.com/shurcooL/go/trim"
"gopkg.in/pipe.v2"
)

@@ -25,7 +24,7 @@ func GoPackageWorkingDiff(goPackage *gist7480523.GoPackage) string {
switch goPackage.Dir.Repo.Vcs.Type() {
case legacyvcs.Git:
newFileDiff := func(line []byte) []byte {
cmd := exec.Command("git", "diff", "--no-ext-diff", "--", "/dev/null", trim.LastNewline(string(line)))
cmd := exec.Command("git", "diff", "--no-ext-diff", "--", "/dev/null", strings.TrimSuffix(string(line), "\n"))
cmd.Dir = goPackage.Dir.Repo.Vcs.RootPath()
out, err := cmd.Output()
if len(out) > 0 {
@@ -67,7 +66,7 @@ func GoPackageWorkingDiffMaster(goPackage *gist7480523.GoPackage) string {
switch goPackage.Dir.Repo.Vcs.Type() {
case legacyvcs.Git:
newFileDiff := func(line []byte) []byte {
cmd := exec.Command("git", "diff", "--no-ext-diff", "--", "/dev/null", trim.LastNewline(string(line)))
cmd := exec.Command("git", "diff", "--no-ext-diff", "--", "/dev/null", strings.TrimSuffix(string(line), "\n"))
cmd.Dir = goPackage.Dir.Repo.Vcs.RootPath()
out, err := cmd.Output()
if len(out) > 0 {
@@ -109,7 +108,7 @@ func GoPackageWorkingDiffOriginMaster(goPackage *gist7480523.GoPackage) string {
switch goPackage.Dir.Repo.Vcs.Type() {
case legacyvcs.Git:
newFileDiff := func(line []byte) []byte {
cmd := exec.Command("git", "diff", "--no-ext-diff", "--", "/dev/null", trim.LastNewline(string(line)))
cmd := exec.Command("git", "diff", "--no-ext-diff", "--", "/dev/null", strings.TrimSuffix(string(line), "\n"))
cmd.Dir = goPackage.Dir.Repo.Vcs.RootPath()
out, err := cmd.Output()
if len(out) > 0 {
@@ -158,7 +157,7 @@ func Branches(repo *exp13.VcsState, opt BranchesOptions) string {
switch repo.Vcs.Type() {
case legacyvcs.Git:
branchInfo := func(line []byte) []byte {
branch := trim.LastNewline(string(line))
branch := strings.TrimSuffix(string(line), "\n")
branchDisplay := branch
if branch == repo.VcsLocal.LocalBranch {
branchDisplay = "**" + branch + "**"
@@ -172,7 +171,7 @@ func Branches(repo *exp13.VcsState, opt BranchesOptions) string {
return []byte(fmt.Sprintf("%s | ? | ?\n", branchDisplay))
}

behindAhead := strings.Split(trim.LastNewline(string(out)), "\t")
behindAhead := strings.Split(strings.TrimSuffix(string(out), "\n"), "\t")
return []byte(fmt.Sprintf("%s | %s | %s\n", branchDisplay, behindAhead[0], behindAhead[1]))
}

@@ -199,7 +198,7 @@ func Branches(repo *exp13.VcsState, opt BranchesOptions) string {
// For example, "master\torigin/master".
func branchRemoteInfo(repo *exp13.VcsState) func(line []byte) []byte {
return func(line []byte) []byte {
branchRemote := strings.Split(trim.LastNewline(string(line)), "\t")
branchRemote := strings.Split(strings.TrimSuffix(string(line), "\n"), "\t")
if len(branchRemote) != 2 {
return []byte("error: len(branchRemote) != 2")
}
@@ -224,7 +223,7 @@ func branchRemoteInfo(repo *exp13.VcsState) func(line []byte) []byte {
return []byte(fmt.Sprintf("%s | %s | | \n", branchDisplay, remoteDisplay))
}

behindAhead := strings.Split(trim.LastNewline(string(out)), "\t")
behindAhead := strings.Split(strings.TrimSuffix(string(out), "\n"), "\t")
return []byte(fmt.Sprintf("%s | %s | %s | %s\n", branchDisplay, remote, behindAhead[0], behindAhead[1]))
}
}

0 comments on commit 8b9b25c

Please sign in to comment.