Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
github
hub
提交
2a6c091e
H
hub
项目概览
github
/
hub
9 个月 前同步成功
通知
3
Star
22523
Fork
2406
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hub
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
2a6c091e
编写于
2月 18, 2016
作者:
P
Pascal Corpet
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
First version of a formatter that works like `git log --format`.
上级
486df9ea
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
186 addition
and
0 deletion
+186
-0
ui/format.go
ui/format.go
+119
-0
ui/format_test.go
ui/format_test.go
+67
-0
未找到文件。
ui/format.go
0 → 100644
浏览文件 @
2a6c091e
package
ui
import
(
"strconv"
"strings"
)
// Expand expands a format string using `git log` message syntax.
func
Expand
(
format
string
,
values
map
[
string
]
string
)
string
{
f
:=
&
expander
{
values
:
values
}
return
f
.
Expand
(
format
)
}
// An expander is a stateful helper to expand a format string.
type
expander
struct
{
// formatted holds the
formatted
[]
string
// values is the map of values that should be expanded.
values
map
[
string
]
string
// skipNext is true if the next placeholder is not a place holder and can be
// output directly as such.
skipNext
bool
}
func
(
f
*
expander
)
Expand
(
format
string
)
string
{
parts
:=
strings
.
Split
(
format
,
"%"
)
f
.
formatted
=
make
([]
string
,
0
,
len
(
parts
))
f
.
append
(
parts
[
0
])
for
_
,
p
:=
range
parts
[
1
:
]
{
v
,
t
:=
f
.
expandOneVar
(
p
)
f
.
append
(
v
,
t
)
}
return
f
.
crush
()
}
func
(
f
*
expander
)
append
(
formattedText
...
string
)
{
f
.
formatted
=
append
(
f
.
formatted
,
formattedText
...
)
}
func
(
f
*
expander
)
crush
()
string
{
s
:=
strings
.
Join
(
f
.
formatted
,
""
)
f
.
formatted
=
nil
return
s
}
var
colorMap
=
map
[
string
]
string
{
"red"
:
"31"
,
"green"
:
"32"
,
"blue"
:
"34"
,
"reset"
:
""
,
}
func
(
f
*
expander
)
expandOneVar
(
format
string
)
(
expand
string
,
untouched
string
)
{
if
f
.
skipNext
{
f
.
skipNext
=
false
return
""
,
format
}
if
format
==
""
{
f
.
skipNext
=
true
return
""
,
"%"
}
if
e
,
u
,
ok
:=
f
.
expandSpecialChar
(
format
[
0
],
format
[
1
:
]);
ok
{
return
e
,
u
}
if
f
.
values
!=
nil
{
for
i
:=
1
;
i
<=
len
(
format
);
i
++
{
if
v
,
exists
:=
f
.
values
[
format
[
0
:
i
]];
exists
{
return
v
,
format
[
i
:
]
}
}
}
return
""
,
"%"
+
format
}
func
(
f
*
expander
)
expandSpecialChar
(
firstChar
byte
,
format
string
)
(
expand
string
,
untouched
string
,
wasExpanded
bool
)
{
switch
firstChar
{
case
'n'
:
return
"
\n
"
,
format
,
true
case
'C'
:
for
k
,
v
:=
range
colorMap
{
if
strings
.
HasPrefix
(
format
,
k
)
{
return
"
\0
33["
+
v
+
"m"
,
format
[
len
(
k
)
:
],
true
}
}
// TODO: Add custom color as specified in color.branch.* options.
// TODO: Handle auto-coloring.
case
'x'
:
if
len
(
format
)
>=
2
{
if
v
,
err
:=
strconv
.
ParseInt
(
format
[
:
2
],
16
,
32
);
err
==
nil
{
return
string
(
v
),
format
[
2
:
],
true
}
}
case
'+'
:
if
e
,
u
:=
f
.
expandOneVar
(
format
);
e
!=
""
{
return
"
\n
"
+
e
,
u
,
true
}
else
{
return
""
,
u
,
true
}
case
' '
:
if
e
,
u
:=
f
.
expandOneVar
(
format
);
e
!=
""
{
return
" "
+
e
,
u
,
true
}
else
{
return
""
,
u
,
true
}
case
'-'
:
if
e
,
u
:=
f
.
expandOneVar
(
format
);
e
!=
""
{
return
e
,
u
,
true
}
else
{
f
.
append
(
strings
.
TrimRight
(
f
.
crush
(),
"
\n
"
))
return
""
,
u
,
true
}
}
return
""
,
""
,
false
}
ui/format_test.go
0 → 100644
浏览文件 @
2a6c091e
package
ui
import
(
"testing"
)
func
TestExpand
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
name
string
format
string
values
map
[
string
]
string
expect
string
}{
{
name
:
"Simple example"
,
format
:
"The author of %h was %an, %ar%nThe title was >>%s<<%n"
,
values
:
map
[
string
]
string
{
"h"
:
"fe6e0ee"
,
"an"
:
"Junio C Hamano"
,
"ar"
:
"23 hours ago"
,
"s"
:
"t4119: test autocomputing -p<n> for traditional diff input."
,
},
expect
:
"The author of fe6e0ee was Junio C Hamano, 23 hours ago
\n
The title was >>t4119: test autocomputing -p<n> for traditional diff input.<<
\n
"
,
},
{
name
:
"Percent sign, middle and trailing"
,
format
:
"%%a %%b %"
,
values
:
map
[
string
]
string
{
"a"
:
"A variable that should not be used."
},
expect
:
"%a %b %"
,
},
{
name
:
"Colors"
,
format
:
"%Cred%r %Cgreen%g %Cblue%b%Creset normal"
,
values
:
map
[
string
]
string
{
"r"
:
"RED"
,
"g"
:
"GREEN"
,
"b"
:
"BLUE"
},
expect
:
"
\0
33[31mRED
\0
33[32mGREEN
\0
33[34mBLUE
\0
33[m normal"
,
},
{
name
:
"Byte from hex code"
,
format
:
"%x00 %x3712%x61 %x%x1%xga"
,
expect
:
"
\x00
\x37
12a %x%x1%xga"
,
},
{
name
:
"plus modifier, conditional line"
,
format
:
"line1%+a line2%+b line3"
,
values
:
map
[
string
]
string
{
"a"
:
"A"
,
"b"
:
""
},
expect
:
"line1
\n
A line2 line3"
,
},
{
name
:
"blank modifier, conditional blank"
,
format
:
"word1% a word2% b word3"
,
values
:
map
[
string
]
string
{
"a"
:
"A"
,
"b"
:
""
},
expect
:
"word1 A word2 word3"
,
},
{
name
:
"minus modifier, crush preceding line-feeds"
,
format
:
"word1%n%n%-a"
,
values
:
map
[
string
]
string
{
"a"
:
""
},
expect
:
"word1"
,
},
}
for
_
,
test
:=
range
tests
{
if
got
:=
Expand
(
test
.
format
,
test
.
values
);
got
!=
test
.
expect
{
t
.
Errorf
(
"%s: Expand(%q, ...) = %q, want %q"
,
test
.
name
,
test
.
format
,
got
,
test
.
expect
)
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录