Example #1
0
func printMessages(w io.Writer, msgs []*wl2k.Message) {
	rows := make([][]string, len(msgs))
	for i, msg := range msgs {
		to := msg.To()[0].Addr
		if len(msg.To()) > 1 {
			to = to + ", ..."
		}

		var flags string
		if mailbox.IsUnread(msg) {
			flags += "N" // New
		}

		rows[i] = []string{
			fmt.Sprintf("%2d", i),
			flags,
			msg.Subject(),
			msg.From().Addr,
			msg.Date().String(),
			to,
		}
	}
	t := gotabulate.Create(rows)
	t.SetHeaders([]string{"i", "Flags", "Subject", "From", "Date", "To"})
	t.SetAlign("left")
	t.SetWrapStrings(true)
	t.SetMaxCellSize(60)
	fmt.Fprintln(w, t.Render("simple"))
}
Example #2
0
func (s Shell) Send(envelope_ Envelope, message string) error {
	s.logger.Info("\n")
	// NOTE recipient doesn't make a lot of sense here
	table := gotabulate.Create([][]string{
		[]string{envelope_.Title, envelope_.Recipient, message},
	})
	table.SetHeaders(TABLE_HEADER)
	fmt.Println(table.Render("simple"))
	return nil
}
Example #3
0
func archives(sc scorebotClienter) {
	body, _ := sc.Archives()
	var archives archivesRecord
	json.Unmarshal(body, &archives)

	archiveRows := make([][]string, len(archives.Archives))

	for i, archive := range archives.Archives {
		archiveRows[i] = []string{archive.ID, archive.RangeEnd}
	}

	t := gotabulate.Create(archiveRows)
	t.SetHeaders([]string{"ID", "Archived At"})
	fmt.Println(t.Render("simple"))
}
Example #4
0
func medals(sc scorebotClienter) {
	body, _ := sc.Medals()
	var medals []medalRecord
	json.Unmarshal(body, &medals)

	sort.Sort(sort.Reverse(medalsByValue(medals)))

	medalsRows := make([][]string, len(medals))

	for i, medal := range medals {
		medalsRows[i] = []string{medal.Reaction, strconv.Itoa(medal.Value)}
	}

	t := gotabulate.Create(medalsRows)
	t.SetHeaders([]string{"Reaction", "Value"})
	fmt.Println(t.Render("simple"))
}
Example #5
0
func scores(sc scorebotClienter) {
	body, _ := sc.Scores()
	var scores []scoreRecord
	json.Unmarshal(body, &scores)

	sort.Sort(sort.Reverse(scoresByScore(scores)))

	scoreRows := make([][]string, len(scores))

	for i, score := range scores {
		scoreRows[i] = []string{score.Name, strconv.Itoa(score.Score)}
	}

	t := gotabulate.Create(scoreRows)
	t.SetHeaders([]string{"Name", "Score"})
	fmt.Println(t.Render("simple"))
}
Example #6
0
// Tabular returns a tabular Exportable representation of the Dataset.
// format is either grid, simple, condensed or markdown.
func (d *Dataset) Tabular(format string) *Exportable {
	back := d.Records()
	t := gotabulate.Create(back)

	if format == TabularCondensed || format == TabularMarkdown {
		rendered := regexp.MustCompile("\n\n\\s").ReplaceAllString(t.Render("simple"), "\n ")
		if format == TabularMarkdown {
			firstLine := regexp.MustCompile("-\\s+-").ReplaceAllString(strings.Split(rendered, "\n")[0], "- | -")
			// now just locate the position of pipe characterds, and set them
			positions := make([]int, 0, d.cols-1)
			x := 0
			for _, c := range firstLine {
				if c == '|' {
					positions = append(positions, x)
				}
				x += utf8.RuneLen(c)
			}

			b := newBuffer()
			lines := strings.Split(rendered, "\n")
			for _, line := range lines[1 : len(lines)-2] {
				ipos := 0
				b.WriteString("| ")
				for _, pos := range positions {
					if ipos < len(line) && pos < len(line) {
						b.WriteString(line[ipos:pos])
						b.WriteString(" | ")
						ipos = pos + 1
					}
				}
				if ipos < len(line) {
					b.WriteString(line[ipos:])
				}
				b.WriteString(" | \n")
			}
			return newExportable(b)
		}
		return newExportableFromString(rendered)
	}
	return newExportableFromString(t.Render(format))
}
Example #7
0
func projectOverviewListing(projectRepo *repo.Projects, buildRepo repo.BuildResultRepo) error {
	projects, err := presenter.CreateProjectListing(projectRepo, buildRepo)
	if err != nil {
		return err
	}

	var rows [][]string
	for _, proj := range projects {
		rows = append(rows, []string{proj.Name, proj.Timestamp.String(), proj.Status})
	}

	tabulate := gotabulate.Create(rows)
	tabulate.SetAlign("center")

	// Set Headers
	tabulate.SetHeaders([]string{"Project", "Last build time", "Status"})

	// Render
	fmt.Println(tabulate.Render("grid"))
	return nil
}
Example #8
0
func projectShowDetail(projectName string, projectRepo *repo.Projects, buildRepo repo.BuildResultRepo) (err error) {
	builds, err := presenter.CreateBuildResultListing(projectName, buildRepo)
	if err != nil {
		return fmt.Errorf("Failed to create build results listing %s: %s", projectName, err)
	}

	project := projectRepo.FindProject(projectName)
	if project == nil {
		return fmt.Errorf("Project %s not found", projectName)
	}

	// holds all the builds per commit
	var buildsByCommit []*CommitBuilds

	// finding all the builds which belong to a commit
	for _, b := range builds {
		if idx := findBuildIndex(buildsByCommit, b.Commit); idx >= 0 {
			buildsByCommit[idx].Builds = append(buildsByCommit[idx].Builds, b)
			continue
		}
		buildsByCommit = append(buildsByCommit, &CommitBuilds{Commit: b.Commit, Builds: []*viewmodel.BuildInfo{b}})
	}

	// creating the header row
	var headers []string
	headers = []string{""}
	for _, builder := range project.GetBuilder() {
		headers = append(headers, builder.Name)
		for i := 0; i < len(builder.Slave)-1; i++ {
			headers = append(headers, "")
		}
	}

	// columnBuilderSlave is needed to keep track of how many rows we have
	// and what key we look for. Key consists of builder+slave. (so we can look up later if we have any builds for that cell)
	var columnBuilderSlave []string
	var rows [][]string

	// Create cells Columns Table
	var row []string
	row = []string{""}
	for _, builder := range project.GetBuilder() {
		for _, slave := range builder.Slave {
			columnBuilderSlave = append(columnBuilderSlave, builder.Name+slave)
			row = append(row, slave)
		}
	}
	rows = append(rows, row)

	// Adding an empty seperator row:
	rows = append(rows, []string{})

	for _, buildinfo := range buildsByCommit {
		row = []string{buildinfo.Builds[0].ShortCommit}
		for i := 0; i < len(columnBuilderSlave); i++ {

			if found := FindBuildByKey(columnBuilderSlave[i], buildinfo.Builds); found != nil {
				row = append(row, found.Status)
			}

		}
		rows = append(rows, row)
	}
	tabulate := gotabulate.Create(rows)
	tabulate.SetAlign("center")
	// Set Headers
	tabulate.SetHeaders(headers)

	// Render
	fmt.Println(tabulate.Render("grid"))
	return
}
Example #9
0
func Run() {
	fmt.Printf("开始测速...\n")
	fmt.Printf("获取客户端信息和服务器列表开始...\n")
	cc, err := GetClientInfo()
	if err != nil {
		fmt.Printf("错误: %v\n", err)
		return
	}
	//fmt.Printf("%s %s", cc, err)
	srvs, err := GetServerLists()
	if err != nil {
		fmt.Printf("错误: %v\n", err)
		return
	}
	fmt.Printf("获取客户端信息和服务器列表完成...\n")
	cfg := Config{
		LicenseKey: cc.License,
		IP:         net.ParseIP(cc.ClientConfig.Ip),
		Lat:        cc.ClientConfig.Lat,
		Long:       cc.ClientConfig.Long,
		ISP:        cc.ClientConfig.ISP,
	}
	ignoreIDs := make(map[uint]bool, 1)
	strIDs := strings.Split(cc.ServerConfig.IgnoreIDs, ",")
	for i := range strIDs {
		x, err := strconv.ParseUint(strIDs[i], 10, 32)
		if err != nil {
			continue
		}
		ignoreIDs[uint(x)] = false
	}
	if err := populateServers(&cfg, srvs.Servers, ignoreIDs); err != nil {
		fmt.Printf("错误: %v\n", err)
		return
	}

	if len(cfg.Servers) <= 0 {
		fmt.Printf("没找到合适的测速服务器!\n")
		return
	}
	var headers []string
	var data [][]string
	var testServers []Testserver
	//	if *search == "" {
	fmt.Printf("获取距离最近的服务器...\n")
	if testServers, err = autoGetTestServers(&cfg); err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		os.Exit(-1)
	}
	fmt.Printf("%d Closest responding servers:\n", len(testServers))
	for i := range testServers {
		data = append(data, []string{fmt.Sprintf("%d", i),
			testServers[i].Name, testServers[i].Sponsor,
			fmt.Sprintf("%.02f", testServers[i].Distance),
			fmt.Sprintf("%s", testServers[i].Latency)})
	}
	headers = []string{"ID", "Name", "Sponsor", "Distance (km)", "Latency (ms)"}
	//	} else {
	//		if testServers, err = getSearchServers(cfg, *search); err != nil {
	//			fmt.Fprintf(os.Stderr, "%s\n", err)
	//			os.Exit(-1)
	//		}
	//		headers = []string{"ID", "Name", "Sponsor", "Distance (km)"}
	//		fmt.Printf("%d Matching servers:\n", len(testServers))
	//		for i := range testServers {
	//			data = append(data, []string{fmt.Sprintf("%d", i),
	//				testServers[i].Name, testServers[i].Sponsor,
	//				fmt.Sprintf("%.02f", testServers[i].Distance)})
	//		}

	//	}
	t := gotabulate.Create(data)
	t.SetHeaders(headers)
	t.SetWrapStrings(false)
	fmt.Printf("%s", t.Render(tableFormat))
	fmt.Printf("Enter server ID for bandwidth test, or \"quit\" to exit\n")
	for {
		s, err := prompt.Basic("ID> ", true)
		if err != nil {
			fmt.Printf("input failure \"%v\"\n", err)
			os.Exit(-1)
		}
		//be REALLY forgiving on exit logic
		if strings.HasPrefix(strings.ToLower(s), "exit") {
			os.Exit(0)
		}
		if strings.HasPrefix(strings.ToLower(s), "quit") {
			os.Exit(0)
		}

		//try to convert the string to a number
		id, err := strconv.ParseUint(s, 10, 64)
		if err != nil {
			fmt.Fprintf(os.Stderr, "\"%s\" is not a valid id\n", s)
			continue
		}
		if id > uint64(len(testServers)) {
			fmt.Fprintf(os.Stderr, "No server with ID \"%d\" available\n", id)
			continue
		}
		if err = fullTest(testServers[id]); err != nil {
			if err == io.EOF {
				fmt.Fprintf(os.Stderr, "Error, the remote server kicked us.\n")
				fmt.Fprintf(os.Stderr, "Maximum request size may have changed\n")
			} else {
				fmt.Fprintf(os.Stderr, "Test failed with unknown 错误: %v\n", err)
			}
			os.Exit(-1)
		} else {
			break //we are done
		}
	}
}
Example #10
0
func main() {
	cfg, err := stdn.GetConfig()
	if err != nil {
		fmt.Printf("ERROR: %v\n", err)
		return
	}
	if len(cfg.Servers) <= 0 {
		fmt.Printf("No acceptable servers found\n")
		return
	}
	var headers []string
	var data [][]string
	var testServers []stdn.Testserver
	if *search == "" {
		fmt.Printf("Gathering server list and testing...\n")
		if testServers, err = autoGetTestServers(cfg); err != nil {
			fmt.Fprintf(os.Stderr, "%s\n", err)
			os.Exit(-1)
		}
		fmt.Printf("%d Closest responding servers:\n", len(testServers))
		for i := range testServers {
			data = append(data, []string{fmt.Sprintf("%d", i),
				testServers[i].Name, testServers[i].Sponsor,
				fmt.Sprintf("%.02f", testServers[i].Distance),
				fmt.Sprintf("%s", testServers[i].Latency)})
		}
		headers = []string{"ID", "Name", "Sponsor", "Distance (km)", "Latency (ms)"}
	} else {
		if testServers, err = getSearchServers(cfg, *search); err != nil {
			fmt.Fprintf(os.Stderr, "%s\n", err)
			os.Exit(-1)
		}
		headers = []string{"ID", "Name", "Sponsor", "Distance (km)"}
		fmt.Printf("%d Matching servers:\n", len(testServers))
		for i := range testServers {
			data = append(data, []string{fmt.Sprintf("%d", i),
				testServers[i].Name, testServers[i].Sponsor,
				fmt.Sprintf("%.02f", testServers[i].Distance)})
		}

	}
	t := gotabulate.Create(data)
	t.SetHeaders(headers)
	t.SetWrapStrings(false)
	fmt.Printf("%s", t.Render(tableFormat))
	fmt.Printf("Enter server ID for bandwidth test, or \"quit\" to exit\n")
	for {
		s, err := prompt.Basic("ID> ", true)
		if err != nil {
			fmt.Printf("input failure \"%v\"\n", err)
			os.Exit(-1)
		}
		//be REALLY forgiving on exit logic
		if strings.HasPrefix(strings.ToLower(s), "exit") {
			os.Exit(0)
		}
		if strings.HasPrefix(strings.ToLower(s), "quit") {
			os.Exit(0)
		}

		//try to convert the string to a number
		id, err := strconv.ParseUint(s, 10, 64)
		if err != nil {
			fmt.Fprintf(os.Stderr, "\"%s\" is not a valid id\n", s)
			continue
		}
		if id > uint64(len(testServers)) {
			fmt.Fprintf(os.Stderr, "No server with ID \"%d\" available\n", id)
			continue
		}
		if err = fullTest(testServers[id]); err != nil {
			if err == io.EOF {
				fmt.Fprintf(os.Stderr, "Error, the remote server kicked us.\n")
				fmt.Fprintf(os.Stderr, "Maximum request size may have changed\n")
			} else {
				fmt.Fprintf(os.Stderr, "Test failed with unknown error: %v\n", err)
			}
			os.Exit(-1)
		} else {
			break //we are done
		}
	}
	//Pausing to allow the windows to stay open
	_, err = prompt.Basic("Enter \"quit\" to exit> ", true)
	if err != nil {
		fmt.Printf("input failure \"%v\"\n", err)
		os.Exit(-1)
	}

}