當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Segment.Background方法代碼示例

本文整理匯總了Golang中github.com/scottweston/powerline-shell-go/powerline.Segment.Background方法的典型用法代碼示例。如果您正苦於以下問題:Golang Segment.Background方法的具體用法?Golang Segment.Background怎麽用?Golang Segment.Background使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/scottweston/powerline-shell-go/powerline.Segment的用法示例。


在下文中一共展示了Segment.Background方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: addHgInfo

func addHgInfo(conf config.Configuration, p powerline.Powerline) *powerline.Segment {
	var fmt_str string

	segment := powerline.Segment{}

	branch_colour := conf.Colours.Hg.BackgroundDefault
	text_colour := conf.Colours.Hg.Text

	hg, err := exec.Command("hg", "sum", "--color=never", "-y").Output()

	if err == nil {
		// branch:
		reBranch := regexp.MustCompile(`(?m)^branch: (.*)$`)
		matchBranch := reBranch.FindStringSubmatch(string(hg))

		// commit:
		// %d modified, %d added, %d removed, %d renamed, %d copied
		// %d deleted, %d unknown, %d unresolved, %d subrepos
		reModifed := regexp.MustCompile(`(?m)^commit:.* (.*) modified`)
		res_mod := reModifed.FindStringSubmatch(string(hg))
		reUntracked := regexp.MustCompile(`(?m)^commit:.* (.*) unknown`)
		res_untrk := reUntracked.FindStringSubmatch(string(hg))
		reAdded := regexp.MustCompile(`(?m)^commit:.* (.*) added`)
		res_added := reAdded.FindStringSubmatch(string(hg))
		reRemoved := regexp.MustCompile(`(?m)^commit:.* (.*) removed`)
		res_remove := reRemoved.FindStringSubmatch(string(hg))
		reClean := regexp.MustCompile(`(?m)^commit:.*\(clean\)`)
		res_clean := reClean.FindStringSubmatch(string(hg))

		// update:
		reUpdate := regexp.MustCompile(`(?m)^update: (.*) new`)
		res_update := reUpdate.FindStringSubmatch(string(hg))

		// phases:
		rePublic := regexp.MustCompile(`(?m)^phases:.* (.*) public`)
		res_public := rePublic.FindStringSubmatch(string(hg))
		reDraft := regexp.MustCompile(`(?m)^phases:.* (.*) draft`)
		res_draft := reDraft.FindStringSubmatch(string(hg))
		reSecret := regexp.MustCompile(`(?m)^phases:.* (.*) secret`)
		res_secret := reSecret.FindStringSubmatch(string(hg))

		if len(res_clean) == 0 {
			branch_colour = conf.Colours.Hg.BackgroundChanges
		}

		segment.Background = branch_colour
		segment.Foreground = text_colour
		segment.Weight = conf.Weights.Segments.Hg

		// branch name
		if len(matchBranch) > 0 {
			branch := matchBranch[1]
			branch_fmt := branch
			if conf.BranchMaxLength > 3 {
				if len(branch) > conf.BranchMaxLength {
					sml := int(conf.BranchMaxLength/2 - 1)
					if sml > 0 {
						branch_fmt = branch[0:sml] + p.Ellipsis + branch[len(branch)-sml:]
					}
				}
			}
			if branch != "default" {
				fmt_str = p.Branch + " " + branch_fmt
			} else {
				fmt_str = branch_fmt
			}
			segment.Parts = append(segment.Parts, powerline.Part{Text: fmt_str, Weight: conf.Weights.Parts.Branch})
		}

		// phases
		if len(res_public) > 0 || len(res_draft) > 0 || len(res_secret) > 0 {
			var public int = 0
			var draft int = 0
			var secret int = 0
			if len(res_public) > 0 {
				public, _ = strconv.Atoi(res_public[1])
			}
			if len(res_draft) > 0 {
				draft, _ = strconv.Atoi(res_draft[1])
			}
			if len(res_secret) > 0 {
				secret, _ = strconv.Atoi(res_secret[1])
			}
			total := public + draft + secret
			if total == 1 {
				fmt_str = p.Phases
			} else {
				fmt_str = fmt.Sprintf("%d%s", total, p.Phases)
			}
			segment.Parts = append(segment.Parts, powerline.Part{Text: fmt_str, Weight: conf.Weights.Parts.Phases})
		}

		// updated files
		if len(res_update) > 0 {
			if res_update[1] != "1" {
				fmt_str = fmt.Sprintf("%s%s", res_update[1], p.Behind)
			} else {
				fmt_str = p.Behind
			}
			segment.Parts = append(segment.Parts, powerline.Part{Text: fmt_str, Weight: conf.Weights.Parts.Sync})
//.........這裏部分代碼省略.........
開發者ID:scottweston,項目名稱:powerline-shell-go,代碼行數:101,代碼來源:powerline-shell.go

示例2: addGitInfo

func addGitInfo(conf config.Configuration, porcelain string, p powerline.Powerline) *powerline.Segment {
	var fmt_str string

	segment := powerline.Segment{}

	branch_colour := conf.Colours.Git.BackgroundDefault
	text_colour := conf.Colours.Git.Text

	// what branch
	reBranch := regexp.MustCompile(`(?m)^## (([^ \.\n]+).*|.* on (\S+))$`)
	matchBranch := reBranch.FindStringSubmatch(porcelain)

	// detached?
	reDetached := regexp.MustCompile(`(?m)^## .* \(no branch\)`)
	matchDetached := reDetached.FindStringSubmatch(porcelain)

	// are we ahead/behind
	reStatus := regexp.MustCompile(`(?m)^## .* \[(ahead|behind) ([0-9]+)\]`)
	matchStatus := reStatus.FindStringSubmatch(porcelain)

	// renamed files
	rename, _ := regexp.Compile(`(?m)^R. `)
	rename_res := rename.FindAllString(porcelain, -1)

	// added files
	add, _ := regexp.Compile(`(?m)^A. `)
	add_res := add.FindAllString(porcelain, -1)

	// modified files
	mod, _ := regexp.Compile(`(?m)^.M `)
	mod_res := mod.FindAllString(porcelain, -1)

	// uncommitted files
	uncom, _ := regexp.Compile(`(?m)^\?\? `)
	uncom_res := uncom.FindAllString(porcelain, -1)

	// removed files
	del, _ := regexp.Compile(`(?m)^(D.|.D) `)
	del_res := del.FindAllString(porcelain, -1)

	// conflicted files
	cfd, _ := regexp.Compile(`(?m)^DD|AU|UD|UA|DU|AA|UU .*$`)
	cfd_res := cfd.FindAllString(porcelain, -1)

	// any changes at all?
	if len(rename_res) > 0 || len(add_res) > 0 || len(mod_res) > 0 || len(uncom_res) > 0 || len(del_res) > 0 || len(cfd_res) > 0 {
		branch_colour = conf.Colours.Git.BackgroundChanges
	}

	segment.Background = branch_colour
	segment.Foreground = text_colour
	segment.Weight = conf.Weights.Segments.Git

	// branch name
	if len(matchBranch) > 0 {
		branch := matchBranch[2]
		branch_fmt := branch
		if conf.BranchMaxLength > 3 {
			if len(branch) > conf.BranchMaxLength {
				sml := int(conf.BranchMaxLength/2 - 1)
				if sml > 0 {
					branch_fmt = branch[0:sml] + p.Ellipsis + branch[len(branch)-sml:]
				}
			}
		}

		if len(matchDetached) > 0 {
			fmt_str = p.Detached + " "
		} else {
			fmt_str = ""
		}
		if branch != "master" {
			fmt_str = fmt.Sprintf("%s%s ", fmt_str, p.Branch)
		}
		fmt_str = fmt.Sprintf("%s%s", fmt_str, branch_fmt)
		segment.Parts = append(segment.Parts, powerline.Part{Text: fmt_str, Weight: conf.Weights.Parts.Branch})
	}

	// ahead/behind
	if len(matchStatus) > 0 {
		num, _ := strconv.Atoi(matchStatus[2])

		if matchStatus[1] == "behind" {
			if num > 1 {
				fmt_str = fmt.Sprintf("%s%s", matchStatus[2], p.Behind)
			} else {
				fmt_str = p.Behind
			}
		} else if matchStatus[1] == "ahead" {
			if num > 1 {
				fmt_str = fmt.Sprintf("%s%s", matchStatus[2], p.Ahead)
			} else {
				fmt_str = p.Ahead
			}
		} else {
			fmt_str = "unk"
		}
		segment.Parts = append(segment.Parts, powerline.Part{Text: fmt_str, Weight: conf.Weights.Parts.Sync})
	}

//.........這裏部分代碼省略.........
開發者ID:scottweston,項目名稱:powerline-shell-go,代碼行數:101,代碼來源:powerline-shell.go


注:本文中的github.com/scottweston/powerline-shell-go/powerline.Segment.Background方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。