提交 7b4b4356 编写于 作者: W wuzewu

fix command line output format bug in windows

上级 0b42516f
......@@ -16,7 +16,9 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
color_dict = {
from paddlehub.common.utils import is_windows
linux_color_dict = {
"white": "\033[1;37m%s\033[0m",
"black": "\033[30m%s\033[0m",
"dark_gray": "\033[1;30m%s\033[0m",
......@@ -35,8 +37,17 @@ color_dict = {
"yellow": "\033[1;33m%s\033[0m"
}
windows_color_dict = {key: "%s" for key in linux_color_dict}
def get_color_dict():
if is_windows():
return windows_color_dict
return linux_color_dict
def colorful_text(color, text):
color_dict = get_color_dict()
if color not in color_dict:
color = color_dict['blue']
else:
......@@ -44,6 +55,12 @@ def colorful_text(color, text):
return color % text
def get_ph_value():
if is_windows():
return 0
return 11
class TablePrinter:
def __init__(self,
titles,
......@@ -76,7 +93,7 @@ class TablePrinter:
for index, title in enumerate(self.titles):
if self.title_colors[index]:
title = colorful_text(self.title_colors[index], title)
_ph = 11
_ph = get_ph_value()
else:
_ph = 0
title_text += ("{0:%s%d}|" %
......@@ -108,7 +125,7 @@ class TablePrinter:
split_text = content[offset[index]:offset[index] + length]
if colors[index] and split_text:
split_text = colorful_text(colors[index], split_text)
_ph = 11
_ph = get_ph_value()
else:
_ph = 0
......
......@@ -34,7 +34,12 @@ class ListCommand(BaseCommand):
def exec(self, argv):
all_modules = default_module_manager.all_modules()
tp = TablePrinter(titles=["ModuleName", "Path"], placeholders=[25, 50])
if utils.is_windows():
placeholders = [20, 40]
else:
placeholders = [25, 50]
tp = TablePrinter(
titles=["ModuleName", "Path"], placeholders=placeholders)
for module_name, module_dir in all_modules.items():
tp.add_line(contents=[module_name, module_dir])
print(tp.get_text())
......
......@@ -44,9 +44,13 @@ class SearchCommand(BaseCommand):
resource_name = argv[0]
resource_list = default_hub_server.search_resource(resource_name)
if utils.is_windows():
placeholders = [20, 8, 8, 20]
else:
placeholders = [30, 8, 8, 25]
tp = TablePrinter(
titles=["ResourceName", "Type", "Version", "Summary"],
placeholders=[30, 8, 8, 25])
placeholders=placeholders)
for resource_name, resource_type, resource_version, resource_summary in resource_list:
if resource_type == "Module":
colors = ["yellow", None, None, None]
......
......@@ -20,6 +20,7 @@ import os
import argparse
from paddlehub.common.logger import logger
from paddlehub.common import utils
from paddlehub.commands.base_command import BaseCommand, ENTRY
from paddlehub.commands.cml_utils import TablePrinter
from paddlehub.module.manager import default_module_manager
......@@ -42,9 +43,13 @@ class ShowCommand(BaseCommand):
def show_model_info(self, model_info_file):
model_info = yaml_parser.parse(model_info_file)
if utils.is_windows():
placeholders = [15, 40]
else:
placeholders = [15, 50]
tp = TablePrinter(
titles=["ModelName", model_info['name']],
placeholders=[15, 50],
placeholders=placeholders,
title_colors=["yellow", None],
title_aligns=["^", "<"])
tp.add_line(
......@@ -72,9 +77,13 @@ class ShowCommand(BaseCommand):
def show_module_info(self, module_dir):
module = Module(module_dir=module_dir)
if utils.is_windows():
placeholders = [15, 40]
else:
placeholders = [15, 50]
tp = TablePrinter(
titles=["ModuleName", module.name],
placeholders=[15, 50],
placeholders=placeholders,
title_colors=["light_red", None],
title_aligns=["^", "<"])
tp.add_line(
......
......@@ -20,6 +20,7 @@ import os
import time
import multiprocessing
import hashlib
import platform
import paddle
import paddle.fluid as fluid
......@@ -28,6 +29,14 @@ from paddlehub.module import module_desc_pb2
from paddlehub.common.logger import logger
def get_platform():
return platform.platform()
def is_windows():
return get_platform().lower().startswith("windows")
def to_list(input):
if not isinstance(input, list):
if not isinstance(input, tuple):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册