Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
jenkinsci
sub-group
Jenkins Cli
提交
5beb636b
J
Jenkins Cli
项目概览
jenkinsci
/
sub-group
/
Jenkins Cli
通知
0
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
Jenkins Cli
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
5beb636b
编写于
6月 29, 2019
作者:
LinuxSuRen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add util to render table
上级
ee8c7a87
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
144 addition
and
0 deletion
+144
-0
util/padding.go
util/padding.go
+50
-0
util/table.go
util/table.go
+94
-0
未找到文件。
util/padding.go
0 → 100644
浏览文件 @
5beb636b
package
util
import
(
"math"
"strings"
"unicode/utf8"
)
const
(
ALIGN_LEFT
=
0
ALIGN_CENTER
=
1
ALIGN_RIGHT
=
2
)
func
Pad
(
s
,
pad
string
,
width
int
,
align
int
)
string
{
switch
align
{
case
ALIGN_CENTER
:
return
PadCenter
(
s
,
pad
,
width
)
case
ALIGN_RIGHT
:
return
PadLeft
(
s
,
pad
,
width
)
default
:
return
PadRight
(
s
,
pad
,
width
)
}
}
func
PadRight
(
s
,
pad
string
,
width
int
)
string
{
gap
:=
width
-
utf8
.
RuneCountInString
(
s
)
if
gap
>
0
{
return
s
+
strings
.
Repeat
(
string
(
pad
),
gap
)
}
return
s
}
func
PadLeft
(
s
,
pad
string
,
width
int
)
string
{
gap
:=
width
-
utf8
.
RuneCountInString
(
s
)
if
gap
>
0
{
return
strings
.
Repeat
(
string
(
pad
),
gap
)
+
s
}
return
s
}
func
PadCenter
(
s
,
pad
string
,
width
int
)
string
{
gap
:=
width
-
utf8
.
RuneCountInString
(
s
)
if
gap
>
0
{
gapLeft
:=
int
(
math
.
Ceil
(
float64
(
gap
/
2
)))
gapRight
:=
gap
-
gapLeft
return
strings
.
Repeat
(
string
(
pad
),
gapLeft
)
+
s
+
strings
.
Repeat
(
string
(
pad
),
gapRight
)
}
return
s
}
util/table.go
0 → 100644
浏览文件 @
5beb636b
package
util
import
(
"fmt"
"io"
"unicode/utf8"
)
// const (
// ALIGN_LEFT = 0
// ALIGN_CENTER = 1
// ALIGN_RIGHT = 2
// )
type
Table
struct
{
Out
io
.
Writer
Rows
[][]
string
ColumnWidths
[]
int
ColumnAlign
[]
int
Separator
string
}
func
CreateTable
(
out
io
.
Writer
)
Table
{
return
Table
{
Out
:
out
,
Separator
:
" "
,
}
}
// Clear removes all rows while preserving the layout
func
(
t
*
Table
)
Clear
()
{
t
.
Rows
=
[][]
string
{}
}
// AddRow adds a new row to the table
func
(
t
*
Table
)
AddRow
(
col
...
string
)
{
t
.
Rows
=
append
(
t
.
Rows
,
col
)
}
func
(
t
*
Table
)
Render
()
{
// lets figure out the max widths of each column
for
_
,
row
:=
range
t
.
Rows
{
for
ci
,
col
:=
range
row
{
l
:=
utf8
.
RuneCountInString
(
col
)
t
.
ColumnWidths
=
ensureArrayCanContain
(
t
.
ColumnWidths
,
ci
)
if
l
>
t
.
ColumnWidths
[
ci
]
{
t
.
ColumnWidths
[
ci
]
=
l
}
}
}
out
:=
t
.
Out
for
_
,
row
:=
range
t
.
Rows
{
lastColumn
:=
len
(
row
)
-
1
for
ci
,
col
:=
range
row
{
if
ci
>
0
{
fmt
.
Fprint
(
out
,
t
.
Separator
)
}
l
:=
t
.
ColumnWidths
[
ci
]
align
:=
t
.
GetColumnAlign
(
ci
)
if
ci
>=
lastColumn
&&
align
!=
ALIGN_CENTER
&&
align
!=
ALIGN_RIGHT
{
fmt
.
Fprint
(
out
,
col
)
}
else
{
fmt
.
Fprint
(
out
,
Pad
(
col
,
" "
,
l
,
align
))
}
}
fmt
.
Fprint
(
out
,
"
\n
"
)
}
}
// SetColumnsAligns sets the alignment of the columns
func
(
t
*
Table
)
SetColumnsAligns
(
colAligns
[]
int
)
{
t
.
ColumnAlign
=
colAligns
}
// GetColumnAlign return the column alignment
func
(
t
*
Table
)
GetColumnAlign
(
i
int
)
int
{
t
.
ColumnAlign
=
ensureArrayCanContain
(
t
.
ColumnAlign
,
i
)
return
t
.
ColumnAlign
[
i
]
}
// SetColumnAlign sets the column alignment for the given column index
func
(
t
*
Table
)
SetColumnAlign
(
i
int
,
align
int
)
{
t
.
ColumnAlign
=
ensureArrayCanContain
(
t
.
ColumnAlign
,
i
)
t
.
ColumnAlign
[
i
]
=
align
}
func
ensureArrayCanContain
(
array
[]
int
,
idx
int
)
[]
int
{
diff
:=
idx
+
1
-
len
(
array
)
for
i
:=
0
;
i
<
diff
;
i
++
{
array
=
append
(
array
,
0
)
}
return
array
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录