Skip to content

Commit

Permalink
feat: support for table generation
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed May 8, 2021
1 parent c0b3d7d commit 234bc90
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
20 changes: 20 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,20 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

* Support for table generation using the `table()` method

### Changed

*

### Fixed

*
47 changes: 47 additions & 0 deletions examples/basic/misc.py
@@ -0,0 +1,47 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Hive Appier Framework
# Copyright (c) 2008-2020 Hive Solutions Lda.
#
# This file is part of Hive Appier Framework.
#
# Hive Appier Framework is free software: you can redistribute it and/or modify
# it under the terms of the Apache License as published by the Apache
# Foundation, either version 2.0 of the License, or (at your option) any
# later version.
#
# Hive Appier Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# Apache License for more details.
#
# You should have received a copy of the Apache License along with
# Hive Appier Framework. If not, see <http://www.apache.org/licenses/>.

__author__ = "João Magalhães <joamag@hive.pt>"
""" The author(s) of the module """

__version__ = "1.0.0"
""" The version of the module """

__revision__ = "$LastChangedRevision$"
""" The revision number of the module """

__date__ = "$LastChangedDate$"
""" The last change date of the module """

__copyright__ = "Copyright (c) 2008-2020 Hive Solutions Lda."
""" The copyright for the module """

__license__ = "Apache License, Version 2.0"
""" The license for the module """

import appier_console

appier_console.table({
"Microsoft" : "Bill Gates",
"Apple" : "Steve Jobs",
"Tesla" : "Elon Musk",
"Amazon" : "Jeff Bezos"
})
2 changes: 1 addition & 1 deletion src/appier_console/__init__.py
Expand Up @@ -41,6 +41,6 @@
from .base import COLOR_RESET, COLOR_WHITE, COLOR_BLACK, COLOR_BLUE, COLOR_LIGHT_BLUE, COLOR_GREEN,\
COLOR_LIGHT_GREEN, COLOR_CYAN, COLOR_LIGHT_CYAN, COLOR_RED, COLOR_LIGHT_RED, COLOR_PURPLE,\
COLOR_LIGHT_PURPLE, COLOR_BROWN, COLOR_YELLOW, COLOR_GRAY, COLOR_LIGHT_GRAY, CLEAR_LINE, COLORS,\
LoaderThread, ctx_loader, colored
LoaderThread, ctx_loader, colored, table
from .http import ctx_http_callbacks
from .util import is_tty, is_ansi, is_color
12 changes: 12 additions & 0 deletions src/appier_console/base.py
Expand Up @@ -347,6 +347,18 @@ def colored(value, color = COLOR_RED):
color = COLORS.get(color, color)
return color + value + COLOR_RESET

def table(pairs, header = True, footer = True, sort = True):
if isinstance(pairs, dict): pairs = appier.legacy.items(pairs)
if sort: pairs.sort()
max_key = max(len(key) for key, _value in pairs)
max_value = max(len(str(value)) for _key, value in pairs)
if header: print("-" * (max_key + max_value + 7))
for key, value in pairs:
key_s = key.ljust(max_key)
value_s = value.ljust(max_value)
print("| %s | %s |" % (key_s, value_s))
if footer: print("-" * (max_key + max_value + 7))

if __name__ == "__main__":
spinners = appier.conf("SPINNERS", None, cast = list)
timeout = appier.conf("TIMEOUT", 3.0, cast = float)
Expand Down

0 comments on commit 234bc90

Please sign in to comment.