-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from LinuxSuRen/clean-up-jcli-dep
Clean up all dep of jcli
- Loading branch information
Showing
10 changed files
with
485 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package pkg | ||
|
||
import ( | ||
"math" | ||
"strings" | ||
"unicode" | ||
"unicode/utf8" | ||
) | ||
|
||
const ( | ||
// AlignLeft align left | ||
AlignLeft = 0 | ||
// AlignCenter align center | ||
AlignCenter = 1 | ||
// AlignRight align right | ||
AlignRight = 2 | ||
) | ||
|
||
// Pad give a pad | ||
func Pad(s, pad string, width int, align int) string { | ||
switch align { | ||
case AlignCenter: | ||
return PadCenter(s, pad, width) | ||
case AlignRight: | ||
return PadLeft(s, pad, width) | ||
default: | ||
return PadRight(s, pad, width) | ||
} | ||
} | ||
|
||
// PadRight pas as right | ||
func PadRight(s, pad string, width int) string { | ||
gap := widthValue(s, width) | ||
if gap > 0 { | ||
return s + strings.Repeat(string(pad), gap) | ||
} | ||
return s | ||
} | ||
|
||
// PadLeft pad as left | ||
func PadLeft(s, pad string, width int) string { | ||
gap := widthValue(s, width) | ||
if gap > 0 { | ||
return strings.Repeat(string(pad), gap) + s | ||
} | ||
return s | ||
} | ||
|
||
// PadCenter pad as center | ||
func PadCenter(s, pad string, width int) string { | ||
gap := widthValue(s, width) | ||
if gap > 0 { | ||
gapLeft := int(math.Ceil(float64(gap / 2))) | ||
gapRight := gap - gapLeft | ||
return strings.Repeat(string(pad), gapLeft) + s + strings.Repeat(string(pad), gapRight) | ||
} | ||
return s | ||
} | ||
|
||
func isHan(s string) (isHan bool) { | ||
wh := []rune(s) | ||
for _, r := range wh { | ||
if unicode.Is(unicode.Han, r) { | ||
isHan = true | ||
} else if unicode.Is(unicode.Hiragana, r) { | ||
isHan = true | ||
} else if unicode.Is(unicode.Katakana, r) { | ||
isHan = true | ||
} else if unicode.Is(unicode.Common, r) { | ||
isHan = true | ||
} else { | ||
isHan = false | ||
break | ||
} | ||
} | ||
return | ||
} | ||
|
||
func countCN(s string) (count int) { | ||
wh := []rune(s) | ||
for _, r := range wh { | ||
if unicode.Is(unicode.Han, r) { | ||
count++ | ||
} else if unicode.Is(unicode.Hiragana, r) { | ||
count++ | ||
} else if unicode.Is(unicode.Katakana, r) { | ||
count++ | ||
} else if unicode.Is(unicode.Common, r) && len(string(r)) != 1 { | ||
count++ | ||
} | ||
} | ||
return | ||
} | ||
|
||
func widthValue(s string, width int) (gap int) { | ||
l := utf8.RuneCountInString(s) | ||
ln := len(s) | ||
isHan := isHan(s) | ||
count := countCN(s) | ||
if ln != l { | ||
if isHan { | ||
gap = width - (ln - l) | ||
} else { | ||
gap = width - (ln - count) | ||
} | ||
} else { | ||
gap = width - l | ||
} | ||
return | ||
} | ||
|
||
// Lenf counts the number | ||
func Lenf(han string) (l int) { | ||
ln := len(han) | ||
l = utf8.RuneCountInString(han) | ||
isHan := isHan(han) | ||
count := countCN(han) | ||
if ln != l { | ||
if isHan { | ||
l = ln - l | ||
} else { | ||
l = ln - count | ||
} | ||
|
||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package pkg | ||
|
||
import ( | ||
"github.com/golang/mock/gomock" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Padding util test", func() { | ||
var ( | ||
ctrl *gomock.Controller | ||
) | ||
|
||
BeforeEach(func() { | ||
ctrl = gomock.NewController(GinkgoT()) | ||
}) | ||
|
||
AfterEach(func() { | ||
ctrl.Finish() | ||
}) | ||
|
||
Context("isHan test", func() { | ||
var han string | ||
It("english test", func() { | ||
han = "ZSOMZAYNHG" | ||
Expect(isHan(han)).To(Equal(false)) | ||
}) | ||
It("chinese test", func() { | ||
han = "构建一个自由风格的软件项目" | ||
Expect(isHan(han)).To(Equal(true)) | ||
}) | ||
It("hk test", func() { | ||
han = "建置 Free-Style 軟體專案" | ||
Expect(isHan(han)).To(Equal(false)) | ||
}) | ||
It("japanese test", func() { | ||
han = "フリースタイル・プロジェクトのビルド" | ||
Expect(isHan(han)).To(Equal(true)) | ||
}) | ||
It("japanese and chinese and english test", func() { | ||
han = "フリースタイル・プ中ロジasdェクトのビルド" | ||
Expect(isHan(han)).To(Equal(false)) | ||
}) | ||
}) | ||
|
||
Context("countCN test", func() { | ||
var han string | ||
It("english test", func() { | ||
han = "ZSOMZAYNHG" | ||
Expect(countCN(han)).To(Equal(0)) | ||
}) | ||
It("chinese test", func() { | ||
han = "构建一个自由风格的软件项目" | ||
Expect(countCN(han)).To(Equal(13)) | ||
}) | ||
It("hk test", func() { | ||
han = "建置 Free-Style 軟體專案" | ||
Expect(countCN(han)).To(Equal(6)) | ||
}) | ||
It("japanese test", func() { | ||
han = "フリースタイル・プロジェクトのビルド" | ||
Expect(countCN(han)).To(Equal(18)) | ||
}) | ||
It("japanese and chinese and english test", func() { | ||
han = "フリースタイル・プ中ロジasdェクトのビルド" | ||
Expect(countCN(han)).To(Equal(19)) | ||
}) | ||
}) | ||
|
||
Context("Lenf test", func() { | ||
var han string | ||
It("english test", func() { | ||
han = "ZSOMZAYNHG" | ||
Expect(Lenf(han) == len(han)).To(Equal(true)) | ||
}) | ||
It("chinese test", func() { | ||
han = "构建一个自由风格的软件项目" | ||
Expect(Lenf(han) == len(han)).To(Equal(false)) | ||
}) | ||
It("hk test", func() { | ||
han = "建置 Free-Style 軟體專案" | ||
Expect(Lenf(han) == len(han)).To(Equal(false)) | ||
}) | ||
It("japanese test", func() { | ||
han = "フリースタイル・プロジェクトのビルド" | ||
Expect(Lenf(han) == len(han)).To(Equal(false)) | ||
}) | ||
It("japanese and chinese and english test", func() { | ||
han = "フリースタイル・プ中ロジasdェクトのビルド" | ||
Expect(Lenf(han) == len(han)).To(Equal(false)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package pkg | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
// ReflectFieldValueAsString returns the value of a field | ||
func ReflectFieldValueAsString(v reflect.Value, field string) string { | ||
return fmt.Sprint(reflect.Indirect(v).FieldByName(field)) | ||
} |
Oops, something went wrong.