返回顶部

收藏

命令行画表格

更多
/*
filename:drawTable.go
created by:Justay
date: 20140111
*/
package main

import (
    "fmt"
)

const (
    T11 string = "┌"
    T12 string = "┬"
    T13 string = "┐"
    T21 string = "├"
    T22 string = "┼"
    T23 string = "┤"
    T31 string = "└"
    T32 string = "┴"
    T33 string = "┘"
    TH  string = "-" //"─"
    TV  string = "│"
)

//字段定义,包含字段名称和长度
type fied struct {
    Flabel string
    Flen   int
}

func main() {
    var a fied
    a.Flabel = "中国x"
    a.Flen = 6
    var b fied
    b.Flabel = "pepol你e"
    b.Flen = 8
    var c fied
    c.Flabel = "details"
    c.Flen = 10

    var f []fied

    f = append(f, a)
    f = append(f, b)
    f = append(f, c)

    var db [][]string
    var s1 []string
    s1 = append(s1, "s234中")
    s1 = append(s1, "ab")
    s1 = append(s1, "20120109时")

    db = append(db, s1)
    db = append(db, s1)
    db = append(db, s1)
    db = append(db, s1)
    db = append(db, s1)
    db = append(db, s1)
    db = append(db, s1)

    PrintTable(f, db)
}

func PrintTable(f []fied, s [][]string) {
    PrintHeader(f)
    for _, v := range s {
        PrintBody(v, f)
    }
    PrintButtom(f)
}

func PrintHeader(f []fied) {
    fleng := len(f)
    //printer top line
    var topLineStr string
    for i, v := range f {

        switch i {
        case 0:
            var midstr string
            for x := 1; x <= v.Flen; x++ {
                midstr = midstr + TH
            }
            topLineStr = topLineStr + T11 + midstr
            //如果只有一个字段
            if fleng == 1 {
                topLineStr = topLineStr + T13
            }

        case fleng - 1:
            var midstr string
            for x := 1; x <= v.Flen; x++ {
                midstr = midstr + TH
            }
            topLineStr = topLineStr + T12 + midstr + T13
        default:
            var midstr string
            for x := 1; x <= v.Flen; x++ {
                midstr = midstr + TH
            }
            topLineStr = topLineStr + T12 + midstr
        }
    }
    fmt.Println(topLineStr)

    //print feild label
    var labstr string
    for fj, fv := range f {
        var midStr string
        tr := []rune(fv.Flabel)
        trLen := len(tr)
        var spLen int
        spLen = fv.Flen - trLen - (len(fv.Flabel)-trLen)/2
        for s := 1; s <= spLen; s++ {
            midStr = midStr + " "
        }
        labstr = labstr + TV + midStr + fv.Flabel
        if fj == fleng-1 {
            labstr = labstr + TV
        }

    }
    fmt.Println(labstr)

    //print buttom of head
    var butStr string
    for bi, bv := range f {
        var midstr string
        for x := 1; x <= bv.Flen; x++ {
            midstr = midstr + TH
        }
        switch bi {
        case 0:
            butStr = T21 + midstr
            if fleng == 1 {
                butStr = butStr + T23
            }
        case fleng - 1:
            butStr = butStr + T22 + midstr + T23
        default:
            butStr = butStr + T22 + midstr
        }
    }
    fmt.Println(butStr)
}
func PrintBody(s []string, f []fied) {
    sLen := len(s)
    fLen := len(f)
    if sLen > fLen {
        fmt.Println("err!")
    } else {
        var fstr string
        for i, v := range s {
            ts := []rune(v)
            tsLen := len(ts)
            stLen := len(v)
            spLen := f[i].Flen - tsLen - (stLen-tsLen)/2
            var midstr string
            for j := 1; j <= spLen; j++ {
                midstr = midstr + " "
            }
            fstr = fstr + TV + midstr + v
            if i == sLen-1 {
                fstr = fstr + TV
            }
        }
        fmt.Println(fstr)
    }
}
func PrintButtom(f []fied) {
    var fleng int = len(f)
    var butStr string
    for bi, bv := range f {
        var midstr string
        for x := 1; x <= bv.Flen; x++ {
            midstr = midstr + TH
        }
        switch bi {
        case 0:
            butStr = T31 + midstr
            if fleng == 1 {
                butStr = butStr + T33
            }
        case fleng - 1:
            butStr = butStr + T32 + midstr + T33
        default:
            butStr = butStr + T32 + midstr
        }
    }
    fmt.Println(butStr)
}
//该片段来自于http://outofmemory.cn

标签:golang,基础

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. 无闻 发表 2014-01-29 17:53:00 《Go Web基础》视频教程
  2. jack.zh 发表 2015-05-21 20:12:00 Go并发编程基础(译)
  3. 无闻 发表 2014-01-16 07:00:00 《Go编程基础》视频教程
  4. 无闻 发表 2014-01-29 17:53:00 《Go Web基础》视频教程
  5. 博主 发表 2014-01-24 08:28:00 Go语言基础
  6. 无闻 发表 2014-01-16 07:00:00 《Go编程基础》视频教程
  7. 博主 发表 2011-10-20 16:00:00 PHP Extension开发基础
  8. musiXboy 发表 2013-03-15 00:22:00 读者投稿:Google Reader,忘不了你
  9. musiXboy 发表 2013-03-17 18:53:10 读者投稿:关闭 Google Reader 是作恶之举?
  10. 王朝阳 发表 2013-03-01 00:00:00 旅游规划编制的基础思路
  11. polaris 发表 2013-01-23 13:46:58 Go加密解密之DES
  12. monnand@gmail.com (m 发表 2012-03-19 06:03:08 Go语言中的map的性能分析