Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
github
hub
提交
dede9106
H
hub
项目概览
github
/
hub
11 个月 前同步成功
通知
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,发现更多精彩内容 >>
提交
dede9106
编写于
12月 22, 2018
作者:
A
Andrew Janke
提交者:
Mislav Marohnić
12月 28, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
256-color terminal support
上级
de3f86fc
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
129 addition
and
41 deletion
+129
-41
commands/issue.go
commands/issue.go
+3
-2
utils/color.go
utils/color.go
+113
-0
utils/color_test.go
utils/color_test.go
+13
-0
utils/utils.go
utils/utils.go
+0
-32
utils/utils_test.go
utils/utils_test.go
+0
-7
未找到文件。
commands/issue.go
浏览文件 @
dede9106
...
...
@@ -595,8 +595,9 @@ func formatLabel(label github.IssueLabel, colorize bool) string {
}
func
colorizeLabel
(
label
github
.
IssueLabel
,
color
*
utils
.
Color
)
string
{
return
fmt
.
Sprintf
(
"
\0
33[38;5;%d;48;2;%d;%d;%dm %s
\0
33[m"
,
getSuitableLabelTextColor
(
color
),
color
.
Red
,
color
.
Green
,
color
.
Blue
,
label
.
Name
)
bgColorCode
:=
utils
.
RgbToTermColorCode
(
color
)
return
fmt
.
Sprintf
(
"
\0
33[38;5;%d;48;%sm %s
\0
33[m"
,
getSuitableLabelTextColor
(
color
),
bgColorCode
,
label
.
Name
)
}
func
getSuitableLabelTextColor
(
color
*
utils
.
Color
)
int
{
...
...
utils/color.go
0 → 100644
浏览文件 @
dede9106
package
utils
import
(
"fmt"
"math"
"os"
"strconv"
)
func
init
()
{
initColorCube
()
}
type
Color
struct
{
Red
int64
Green
int64
Blue
int64
}
func
NewColor
(
hex
string
)
(
*
Color
,
error
)
{
red
,
err
:=
strconv
.
ParseInt
(
hex
[
0
:
2
],
16
,
16
)
if
err
!=
nil
{
return
nil
,
err
}
green
,
err
:=
strconv
.
ParseInt
(
hex
[
2
:
4
],
16
,
16
)
if
err
!=
nil
{
return
nil
,
err
}
blue
,
err
:=
strconv
.
ParseInt
(
hex
[
4
:
6
],
16
,
16
)
if
err
!=
nil
{
return
nil
,
err
}
return
&
Color
{
Red
:
red
,
Green
:
green
,
Blue
:
blue
,
},
nil
}
func
(
c
*
Color
)
Brightness
()
float32
{
return
(
0.299
*
float32
(
c
.
Red
)
+
0.587
*
float32
(
c
.
Green
)
+
0.114
*
float32
(
c
.
Blue
))
/
255
}
func
(
c
*
Color
)
Distance
(
other
*
Color
)
float64
{
return
math
.
Sqrt
(
float64
(
math
.
Pow
(
float64
(
c
.
Red
-
other
.
Red
),
2
)
+
math
.
Pow
(
float64
(
c
.
Green
-
other
.
Green
),
2
)
+
math
.
Pow
(
float64
(
c
.
Blue
-
other
.
Blue
),
2
)))
}
var
x6colorIndexes
=
[
6
]
int64
{
0
,
95
,
135
,
175
,
215
,
255
}
var
x6colorCube
[
216
]
Color
func
initColorCube
()
{
i
:=
0
for
iR
:=
0
;
iR
<
6
;
iR
++
{
for
iG
:=
0
;
iG
<
6
;
iG
++
{
for
iB
:=
0
;
iB
<
6
;
iB
++
{
x6colorCube
[
i
]
=
Color
{
x6colorIndexes
[
iR
],
x6colorIndexes
[
iG
],
x6colorIndexes
[
iB
],
}
i
++
}
}
}
}
func
ditherTo256ColorCode
(
color
*
Color
)
(
code
int
)
{
iMatch
:=
-
1
minDistance
:=
float64
(
99999
)
for
i
:=
0
;
i
<
216
;
i
++
{
distance
:=
color
.
Distance
(
&
x6colorCube
[
i
])
if
distance
<
minDistance
{
iMatch
=
i
minDistance
=
distance
}
}
return
iMatch
+
16
}
var
non24bitColorTerms
=
[]
string
{
"Apple_Terminal"
,
}
var
isTerm24bitColorCapableCache
bool
var
isTerm24bitColorCapableCacheIsInit
bool
=
false
func
isTerm24bitColorCapable
()
bool
{
if
!
isTerm24bitColorCapableCacheIsInit
{
isTerm24bitColorCapableCache
=
true
myTermProg
:=
os
.
Getenv
(
"TERM_PROGRAM"
)
for
_
,
brokenTerm
:=
range
non24bitColorTerms
{
if
myTermProg
==
brokenTerm
{
isTerm24bitColorCapableCache
=
false
break
}
}
isTerm24bitColorCapableCacheIsInit
=
true
}
return
isTerm24bitColorCapableCache
}
func
RgbToTermColorCode
(
color
*
Color
)
string
{
if
isTerm24bitColorCapable
()
{
return
fmt
.
Sprintf
(
"2;%d;%d;%d"
,
color
.
Red
,
color
.
Green
,
color
.
Blue
)
}
else
{
intCode
:=
ditherTo256ColorCode
(
color
)
return
fmt
.
Sprintf
(
"5;%d"
,
intCode
)
}
}
utils/color_test.go
0 → 100644
浏览文件 @
dede9106
package
utils
import
(
"github.com/bmizerany/assert"
"testing"
)
func
TestColorBrightness
(
t
*
testing
.
T
)
{
c
,
err
:=
NewColor
(
"880000"
)
assert
.
Equal
(
t
,
nil
,
err
)
actual
:=
c
.
Brightness
()
assert
.
Equal
(
t
,
float32
(
0.15946665406227112
),
actual
)
}
utils/utils.go
浏览文件 @
dede9106
...
...
@@ -7,7 +7,6 @@ import (
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
...
...
@@ -83,37 +82,6 @@ func IsOption(confirm, short, long string) bool {
return
strings
.
EqualFold
(
confirm
,
short
)
||
strings
.
EqualFold
(
confirm
,
long
)
}
type
Color
struct
{
Red
int64
Green
int64
Blue
int64
}
func
NewColor
(
hex
string
)
(
*
Color
,
error
)
{
red
,
err
:=
strconv
.
ParseInt
(
hex
[
0
:
2
],
16
,
16
)
if
err
!=
nil
{
return
nil
,
err
}
green
,
err
:=
strconv
.
ParseInt
(
hex
[
2
:
4
],
16
,
16
)
if
err
!=
nil
{
return
nil
,
err
}
blue
,
err
:=
strconv
.
ParseInt
(
hex
[
4
:
6
],
16
,
16
)
if
err
!=
nil
{
return
nil
,
err
}
return
&
Color
{
Red
:
red
,
Green
:
green
,
Blue
:
blue
,
},
nil
}
func
(
c
*
Color
)
Brightness
()
float32
{
return
(
0.299
*
float32
(
c
.
Red
)
+
0.587
*
float32
(
c
.
Green
)
+
0.114
*
float32
(
c
.
Blue
))
/
255
}
func
TimeAgo
(
t
time
.
Time
)
string
{
duration
:=
timeNow
()
.
Sub
(
t
)
minutes
:=
duration
.
Minutes
()
...
...
utils/utils_test.go
浏览文件 @
dede9106
...
...
@@ -69,10 +69,3 @@ func TestTimeAgo(t *testing.T) {
actual
=
TimeAgo
(
yearsAgo
)
assert
.
Equal
(
t
,
"2 years ago"
,
actual
)
}
func
TestColorBrightness
(
t
*
testing
.
T
)
{
c
,
err
:=
NewColor
(
"880000"
)
assert
.
Equal
(
t
,
nil
,
err
)
actual
:=
c
.
Brightness
()
assert
.
Equal
(
t
,
float32
(
0.15946665406227112
),
actual
)
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录