func Test_addGitInfo_not_staged(t *testing.T) {
	var conf config.Configuration

	var porc string = `## master
 M modifed.go
A  added.go
D  deleted.go
DD conflicted.go
?? not_staged.go
`

	p := powerline.NewPowerline("bash", false)

	conf.SetDefaults()
	rootSegment := addGitInfo(conf, porc, p)

	var parts []powerline.Part
	parts = append(parts, powerline.Part{Text: "master"})
	parts = append(parts, powerline.Part{Text: p.Added})
	parts = append(parts, powerline.Part{Text: p.Modified})
	parts = append(parts, powerline.Part{Text: p.Untracked})
	parts = append(parts, powerline.Part{Text: "2" + p.Removed})
	parts = append(parts, powerline.Part{Text: p.Conflicted})
	want := powerline.Segment{Foreground: conf.Colours.Git.Text,
		Background: conf.Colours.Git.BackgroundChanges,
		Parts:      parts}

	if !reflect.DeepEqual(rootSegment, &want) {
		t.Errorf("addCwd returned %+v, not %+v", rootSegment, &want)
	}
}
func Test_addCwd_home_five(t *testing.T) {
	var conf config.Configuration
	conf.SetDefaults()

	p := powerline.NewPowerline("bash", false)

	dir := "~/gocode/src/github.com/wm/powerline-shell-go"
	cwdparts := strings.Split(dir, "/")

	rootSegments := addCwd(conf, cwdparts, p)

	var parts []powerline.Part
	var want []powerline.Segment
	parts = append(parts, powerline.Part{Text: "~"})
	want = append(want, powerline.Segment{Foreground: conf.Colours.Cwd.HomeText,
		Background: conf.Colours.Cwd.HomeBackground,
		Parts:      parts})
	var subparts []powerline.Part
	subparts = append(subparts, powerline.Part{Text: "gocode"})
	subparts = append(subparts, powerline.Part{Text: p.Ellipsis})
	subparts = append(subparts, powerline.Part{Text: "powerline-shell-go"})
	want = append(want, powerline.Segment{Foreground: conf.Colours.Cwd.Text,
		Background: conf.Colours.Cwd.Background,
		Parts:      subparts})

	if !reflect.DeepEqual(rootSegments, want) {
		t.Errorf("addCwd returned %+v, not %+v", rootSegments, want)
	}
}
func Test_addHostname_with_username(t *testing.T) {
	var conf config.Configuration
	conf.SetDefaults()
	hostname, _ := os.Hostname()
	user, _ := user.Current()

	p := powerline.NewPowerline("bash", false)

	rootSegment := addHostname(conf, true, false, p)
	var parts []powerline.Part
	parts = append(parts, powerline.Part{Text: user.Username + "@" + hostname})
	want := powerline.Segment{Foreground: 16, Background: 12,
		Parts: parts}

	if !reflect.DeepEqual(rootSegment, &want) {
		t.Errorf("addCwd returned %+v, not %+v", rootSegment, &want)
	}
}
func Test_addGitInfo_no_status(t *testing.T) {
	var conf config.Configuration

	var porc string = `## master
`

	p := powerline.NewPowerline("bash", false)

	conf.SetDefaults()
	rootSegment := addGitInfo(conf, porc, p)

	var parts []powerline.Part
	parts = append(parts, powerline.Part{Text: "master"})
	want := powerline.Segment{Foreground: conf.Colours.Git.Text,
		Background: conf.Colours.Git.BackgroundDefault,
		Parts:      parts}

	if !reflect.DeepEqual(rootSegment, &want) {
		t.Errorf("addCwd returned %+v, not %+v", rootSegment, &want)
	}
}
func Test_addCwd_home(t *testing.T) {
	var conf config.Configuration
	conf.SetDefaults()

	p := powerline.NewPowerline("bash", false)

	dir := "~"
	cwdparts := strings.Split(dir, "/")

	rootSegments := addCwd(conf, cwdparts, p)

	var parts []powerline.Part
	var want []powerline.Segment
	parts = append(parts, powerline.Part{Text: "~"})
	want = append(want, powerline.Segment{Foreground: conf.Colours.Cwd.HomeText,
		Background: conf.Colours.Cwd.HomeBackground,
		Parts:      parts})

	if !reflect.DeepEqual(rootSegments, want) {
		t.Errorf("addCwd returned %+v, not %+v", rootSegments, want)
	}
}
func main() {
	var configuration config.Configuration
	var set_title string = ""
	configuration.SetDefaults()
	shell := "bash"
	last_retcode := 0

	user, err := user.Current()
	var data []byte
	if err == nil {
		data, err = ioutil.ReadFile(user.HomeDir + "/.config/powerline-shell-go/config.json")
	} else if home, found := syscall.Getenv("HOME"); found {
		data, err = ioutil.ReadFile(home + "/.config/powerline-shell-go/config.json")
	}
	if err == nil {
		err = json.Unmarshal(data, &configuration)
		if err != nil {
			fmt.Printf("configuration error(%s)> ", err)
			os.Exit(1)
		}
	}

	if len(os.Args) > 1 {
		if os.Args[1] == "version" || os.Args[1] == "build" {
			if build != "" {
				fmt.Println(build)
			} else {
				fmt.Println("unknown")
			}
			os.Exit(0)
		} else {
			shell = os.Args[1]
		}
	}

	if len(os.Args) > 2 {
		last_retcode, err = strconv.Atoi(os.Args[2])
		if err != nil {
			if os.Args[2] == "install" {
				if shell == "bash" {
					fmt.Println(`function _update_ps1() { export PS1="$(powerline-shell-go bash $? 2> /dev/null)"; };
export PROMPT_COMMAND="_update_ps1; $PROMPT_COMMAND";`)
				} else if shell == "zsh" {
					fmt.Println(`function powerline_precmd() { export PS1="$(powerline-shell-go zsh $? 2> /dev/null)"; };
function install_powerline_precmd() { for s in "${precmd_functions[@]}"; do; if [ "$s" = "powerline_precmd" ]; then; return; fi; done; precmd_functions+=(powerline_precmd); };
install_powerline_precmd;`)
				} else {
					fmt.Printf("echo Unsupported shell: %s;\n", shell)
				}
				os.Exit(0)
			}
		}
	}

	if shell != "bash" && shell != "zsh" {
		fmt.Printf("unsupported shell(%s)> ", shell)
		os.Exit(1)
	}

	var p powerline.Powerline
	if _, found := syscall.Getenv("LC_POWERLINE"); found {
		p = powerline.NewPowerline(shell, true)
		if configuration.Icons.Powerline.Added != "" {
			p.Added = configuration.Icons.Powerline.Added
		}
		if configuration.Icons.Powerline.Ahead != "" {
			p.Ahead = configuration.Icons.Powerline.Ahead
		}
		if configuration.Icons.Powerline.Behind != "" {
			p.Behind = configuration.Icons.Powerline.Behind
		}
		if configuration.Icons.Powerline.Branch != "" {
			p.Branch = configuration.Icons.Powerline.Branch
		}
		if configuration.Icons.Powerline.Conflicted != "" {
			p.Conflicted = configuration.Icons.Powerline.Conflicted
		}
		if configuration.Icons.Powerline.Detached != "" {
			p.Detached = configuration.Icons.Powerline.Detached
		}
		if configuration.Icons.Powerline.Ellipsis != "" {
			p.Ellipsis = configuration.Icons.Powerline.Ellipsis
		}
		if configuration.Icons.Powerline.Modified != "" {
			p.Modified = configuration.Icons.Powerline.Modified
		}
		if configuration.Icons.Powerline.Phases != "" {
			p.Phases = configuration.Icons.Powerline.Phases
		}
		if configuration.Icons.Powerline.ReadOnly != "" {
			p.ReadOnly = configuration.Icons.Powerline.ReadOnly
		}
		if configuration.Icons.Powerline.Removed != "" {
			p.Removed = configuration.Icons.Powerline.Removed
		}
		if configuration.Icons.Powerline.Renamed != "" {
			p.Renamed = configuration.Icons.Powerline.Renamed
		}
		if configuration.Icons.Powerline.SeparatorThin != "" {
			p.SeparatorThin = configuration.Icons.Powerline.SeparatorThin
		}
		if configuration.Icons.Powerline.Separator != "" {
			p.Separator = configuration.Icons.Powerline.Separator
		}
		if configuration.Icons.Powerline.Untracked != "" {
			p.Untracked = configuration.Icons.Powerline.Untracked
		}
	} else {
		p = powerline.NewPowerline(shell, false)
		if configuration.Icons.Plain.Added != "" {
			p.Added = configuration.Icons.Plain.Added
		}
		if configuration.Icons.Plain.Ahead != "" {
			p.Ahead = configuration.Icons.Plain.Ahead
		}
		if configuration.Icons.Plain.Behind != "" {
			p.Behind = configuration.Icons.Plain.Behind
		}
		if configuration.Icons.Plain.Branch != "" {
			p.Branch = configuration.Icons.Plain.Branch
		}
		if configuration.Icons.Plain.Conflicted != "" {
			p.Conflicted = configuration.Icons.Plain.Conflicted
		}
		if configuration.Icons.Plain.Detached != "" {
			p.Detached = configuration.Icons.Plain.Detached
		}
		if configuration.Icons.Plain.Ellipsis != "" {
			p.Ellipsis = configuration.Icons.Plain.Ellipsis
		}
		if configuration.Icons.Plain.Modified != "" {
			p.Modified = configuration.Icons.Plain.Modified
		}
		if configuration.Icons.Plain.Phases != "" {
			p.Phases = configuration.Icons.Plain.Phases
		}
		if configuration.Icons.Plain.ReadOnly != "" {
			p.ReadOnly = configuration.Icons.Plain.ReadOnly
		}
		if configuration.Icons.Plain.Removed != "" {
			p.Removed = configuration.Icons.Plain.Removed
		}
		if configuration.Icons.Plain.Renamed != "" {
			p.Renamed = configuration.Icons.Plain.Renamed
		}
		if configuration.Icons.Plain.SeparatorThin != "" {
			p.SeparatorThin = configuration.Icons.Plain.SeparatorThin
		}
		if configuration.Icons.Plain.Separator != "" {
			p.Separator = configuration.Icons.Plain.Separator
		}
		if configuration.Icons.Plain.Untracked != "" {
			p.Untracked = configuration.Icons.Plain.Untracked
		}
	}
	cwd, cwdParts := getCurrentWorkingDir()

	if term, found := syscall.Getenv("TERM"); found {
		if strings.Contains(term, "xterm") || strings.Contains(term, "rxvt") {
			set_title = p.SetTitle
		}
	}

	if configuration.ShowVirtualEnv {
		p.AppendSegment(addVirtulEnvName(configuration, getVirtualEnv()))
	}
	if _, found := syscall.Getenv("SSH_CLIENT"); found {
		p.AppendSegment(addHostname(configuration, true, true, p))
	}
	if configuration.ShowCwd {
		parts := addCwd(configuration, cwdParts, p)
		for _, element := range parts {
			p.AppendSegment(&element)
		}
	}
	if configuration.ShowWritable {
		p.AppendSegment(addLock(configuration, cwd, p))
	}
	if configuration.ShowGit {
		porcelain, err := exec.Command("git", "status", "--ignore-submodules", "-b", "--porcelain").Output()
		if err == nil {
			p.AppendSegment(addGitInfo(configuration, string(porcelain), p))
		}
	}
	if configuration.ShowHg {
		p.AppendSegment(addHgInfo(configuration, p))
	}
	if configuration.ShowReturnCode {
		p.AppendSegment(addReturnCode(configuration, last_retcode))
	}
	if configuration.BatteryWarn > 0 {
		p.AppendSegment(addBatteryWarn(configuration))
	}
	p.AppendSegment(addDollarPrompt(configuration, p.Dollar))

	fmt.Print(set_title, p.PrintSegments(), " ")
}