Skip to content

Commit

Permalink
Merge pull request #38 from markus-oberhumer/honor-LC_ALL
Browse files Browse the repository at this point in the history
Honor the LC_ALL environment variable.
  • Loading branch information
mattn committed Jan 25, 2020
2 parents a4df4dd + eea4277 commit 5885066
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
5 changes: 4 additions & 1 deletion runewidth_posix.go
Expand Up @@ -62,7 +62,10 @@ func isEastAsian(locale string) bool {

// IsEastAsian return true if the current locale is CJK
func IsEastAsian() bool {
locale := os.Getenv("LC_CTYPE")
locale := os.Getenv("LC_ALL")
if locale == "" {
locale = os.Getenv("LC_CTYPE")
}
if locale == "" {
locale = os.Getenv("LANG")
}
Expand Down
32 changes: 25 additions & 7 deletions runewidth_posix_test.go
Expand Up @@ -9,6 +9,25 @@ import (
"testing"
)

type envVars struct {
lang string
lc_all string
lc_ctype string
}

func saveEnv() envVars {
return envVars{
lang: os.Getenv("LANG"),
lc_all: os.Getenv("LC_ALL"),
lc_ctype: os.Getenv("LC_CTYPE"),
}
}
func restoreEnv(env *envVars) {
os.Setenv("LANG", env.lang)
os.Setenv("LC_ALL", env.lc_all)
os.Setenv("LC_CTYPE", env.lc_ctype)
}

func TestIsEastAsian(t *testing.T) {
testcases := []struct {
locale string
Expand All @@ -29,8 +48,9 @@ func TestIsEastAsian(t *testing.T) {
}

func TestIsEastAsianLCCTYPE(t *testing.T) {
lcctype := os.Getenv("LC_CTYPE")
defer os.Setenv("LC_CTYPE", lcctype)
env := saveEnv()
defer restoreEnv(&env)
os.Setenv("LC_ALL", "")

testcases := []struct {
lcctype string
Expand All @@ -52,11 +72,9 @@ func TestIsEastAsianLCCTYPE(t *testing.T) {
}

func TestIsEastAsianLANG(t *testing.T) {
lcctype := os.Getenv("LC_CTYPE")
defer os.Setenv("LC_CTYPE", lcctype)
lang := os.Getenv("LANG")
defer os.Setenv("LANG", lang)

env := saveEnv()
defer restoreEnv(&env)
os.Setenv("LC_ALL", "")
os.Setenv("LC_CTYPE", "")

testcases := []struct {
Expand Down

0 comments on commit 5885066

Please sign in to comment.