根据文件,它应该很简单:
@contextmanager
def gitlab_section(name, header):
print(f'\033[0Ksection_start:{int(time.time())}:{name}[collapsed=true]\r\033[0K{header}')
yield
print(f'\033[0Ksection_end:{int(time.time())}:{name}:\r\033[0K')
但是,这样使用它:
with gitlab_section('first', 'First'):
print('first section')
print("After first section")
with gitlab_section('second', 'Second'):
print('second section')
使“After..”行折叠到第一部分。
我用了一个可怕的破解方法,每次破解后都会启动一个虚拟部分
section_end
:
def unique():
return int(random.random() * 1000000000)
@contextmanager
def gitlab_section(name, header):
sys.stdout.flush()
sectionid = unique() # in case someone uses the same name twice...
print(f'\033[0Ksection_start:{int(time.time())}:{name}_{sectionid}[collapsed=true]\r\033[0K{header}')
yield
sys.stdout.flush()
print(f'\nsection_end:{int(time.time())}:{name}_{sectionid}:\r\033[0K')
# start a dummy section without a header
print(f'\033[0Ksection_start:{int(time.time())}:dummy_{unique()}\r\033[0K')
sys.stdout.flush()
这个
\e[0K
上似乎不需要前缀
section_end
(gitlab生成的输出不使用它)。
虽然这是有效的,但它似乎非常粗鲁。我已经彻底阅读了文档,但也许我在第一个例子中遗漏了一些内容。。?
嵌套的可折叠部分怎么办,这可能吗?