Skip to content

Commit

Permalink
feat(all): add output prefix >
Browse files Browse the repository at this point in the history
  • Loading branch information
caizhengxin committed Dec 27, 2019
1 parent 7517bfb commit e719d9e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Added

* ``IntField`` support ``max_value``, ``min_value``
* ``StringField` support ``max_length``, ``min_length``, ``regex``
* Add output prefix ``>``.

Changed
*******
Expand Down
12 changes: 5 additions & 7 deletions interact/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Author: JanKinCai
# @Date: 2019-12-12 12:52:19
# @Last Modified by: JanKinCai
# @Last Modified time: 2019-12-27 22:06:37
# @Last Modified time: 2019-12-28 00:26:54
from __future__ import print_function
import sys
import json
Expand Down Expand Up @@ -44,12 +44,13 @@ class Interact(object):
"double": FloatField,
}

def __init__(self, iconfig: dict, *args, **kwargs):
def __init__(self, iconfig: dict, prefix=">", *args, **kwargs):
"""
init
"""

self.iconfig = iconfig
self.prefix = prefix
self.cmd_input_items = self.parser()

def get_mapping_type(self, name: str) -> Optional[Callable[[Any, str, Optional[list]], Any]]:
Expand Down Expand Up @@ -91,18 +92,15 @@ def _parser(self, iconfig) -> dict:

for k, v in iconfig.items():

description = v.get("description")

if not description:
v["description"] = k
v["description"] = v.get("description") or k

whenstr = v.get("when")

if whenstr is not None and not when(whenstr, items):
items[k] = None
continue

items[k] = self.get_mapping_type(v["type"])(**v).do()
items[k] = self.get_mapping_type(v["type"])(prefix=self.prefix, **v).do()

return items

Expand Down
12 changes: 9 additions & 3 deletions interact/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Author: JanKinCai
# @Date: 2019-12-23 12:37:34
# @Last Modified by: JanKinCai
# @Last Modified time: 2019-12-27 23:48:03
# @Last Modified time: 2019-12-28 00:32:41
# import sys
import re
from typing import (
Expand All @@ -21,12 +21,13 @@ class BaseField(object):
message = "Invalided `{value}`"
valid_type: Any = None

def __init__(self, default: Any = None, description: str = "", *args, **kwargs):
def __init__(self, default: Any = None, description: str = "", prefix=">", *args, **kwargs):
"""
init
"""

self.default = default
self.prefix = prefix
self.descripiton = description

def is_default_valid(self) -> bool:
Expand Down Expand Up @@ -151,6 +152,7 @@ def to_input(self) -> str:
"""

v = f"{self.pre_input()}{self.descripiton} {self.do_default()}{self.post_input()}: "
v = f"{self.prefix} {v}" if self.prefix else v

return input(v)

Expand Down Expand Up @@ -410,7 +412,10 @@ def pre_input(self) -> str:
]

choice_list = [f" {i} - {chi}" for i, chi in enumerate(self.choice, 1)]
choice_list.append(f"Choose from")

prefix = "Choose from"
prefix = f"{self.prefix} {prefix}" if self.prefix else prefix
choice_list.append(prefix)
input_list.extend(choice_list)

return "\n".join(input_list)
Expand All @@ -423,5 +428,6 @@ def to_input(self) -> str:
"""

v = f"{self.pre_input()} {self.do_default()}{self.post_input()}: "
v = f"{self.prefix} {v}" if self.prefix else v

return input(v)

0 comments on commit e719d9e

Please sign in to comment.