Repository: frostming/python-cfonts

Issue:
Background colour doesn't span entire line
While kicking the tires, I noticed that my output differed from the tutorial:

![image](https://user-images.githubusercontent.com/3307295/42124988-0220ea0a-7c6e-11e8-922a-e551d5c023b7.png)

I also tried outside of `ipython` from the CLI without any improvement.

- Terminal editor: kitty
- Shell: `zsh`
- `TERM=screen-256color`

Please let me know if there's any other information you need.

Really nice package, thanks for sharing!

PS: Also tried konsole terminal emulator with the same result.

Discussion:

Patch:
diff --git a/.gitignore b/.gitignore
index 3dfcc77..f5f8a5c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,3 +6,4 @@ build/
 .mypy_cache/
 .pytest_cache/
 _build/
+.vscode/
diff --git a/.travis.yml b/.travis.yml
index b9261b5..5c8bfa4 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -5,7 +5,7 @@ python:
   - "3.4"
   - "3.5"
   - "3.6"
-  - "3.7-dev"
+  - "nightly"
 
 install:
   - pip install --upgrade pipenv
diff --git a/README.md b/README.md
index 73ff3ec..7d2daa6 100644
--- a/README.md
+++ b/README.md
@@ -69,5 +69,6 @@ Ported by [@frostming](https://github.com/frostming), under GPLv2 license. See [
 
 ## Changelog
 
-- **v0.3.0** Supports Python 2.7
-- **v0.2.0** Initial commit and testing
+- **v0.3.1** Fix a bug that the background doesn't span the full width.
+- **v0.3.0** Supports Python 2.7.
+- **v0.2.0** Initial commit and testing.
diff --git a/cfonts/__version__.py b/cfonts/__version__.py
index 98f0094..f19df34 100644
--- a/cfonts/__version__.py
+++ b/cfonts/__version__.py
@@ -6,4 +6,4 @@
     :license: GNU GPLv2
     :author: Frost Ming<mianghong@gmail.com>
 """
-__version__ = '0.3.0'
+__version__ = '0.3.1'
diff --git a/cfonts/core.py b/cfonts/core.py
index 43d70ac..ffd6d61 100644
--- a/cfonts/core.py
+++ b/cfonts/core.py
@@ -280,14 +280,20 @@ def render(
 
         output = align_text(output, line_length, font_face.lines, align, size)
 
-    write = "\n".join(output)
-    if font_face.colors <= 1:
-        write = colorize(write, font_face.colors, colors)
     if space:
-        write = "\n\n{}\n\n".format(write)
+        # Blank lines at the beginning and end
+        output = [""] * 2 + output + [""] * 2
     if background != BGCOLORS.transparent:
+        # Fill whitespaces to the full width.
+        # See https://github.com/frostming/python-cfonts/issues/3
+        output = [(line + " " * (size[0] - len(_strip_color(line)))) for line in output]
         bg_color = "bg" + background
-        write = ansi_styles[bg_color][0] + "\n" + write + ansi_styles[bg_color][1]
+        if output:
+            output[0] = ansi_styles[bg_color][0] + output[0]
+            output[-1] += ansi_styles[bg_color][1]
+    write = "\n".join(output)
+    if font_face.colors <= 1:
+        write = colorize(write, font_face.colors, colors)
 
     return write
 
@@ -302,3 +308,8 @@ def say(text, **options):
     write = render(text, **options)
     if write:
         click.echo(write)
+
+
+def _strip_color(text):
+    REGEX = re.compile(r'\x1b\[\d+?m')
+    return REGEX.sub('', text)


Test patch:
diff --git a/tests/test_render.py b/tests/test_render.py
index 7f6906d..1154d40 100644
--- a/tests/test_render.py
+++ b/tests/test_render.py
@@ -12,7 +12,7 @@ def test_render_console():
 
 def test_render_console_with_color():
     text = render("text", font="console", colors=["red"], size=(100, 10))
-    assert text == "\n\n\x1b[31mtext\x1b[39m\n\n"
+    assert text == "\x1b[31m\n\ntext\n\n\x1b[39m"
 
 
 def test_render_invalid_input():
@@ -166,12 +166,12 @@ def test_render_remove_space():
 
 
 def test_render_background():
-    assert render("text", background="red", size=(100, 10)) == (
-        "\x1b[41m\n\n\n"
-        " ████████╗ ███████╗ ██╗  ██╗ ████████╗ \n"
-        " ╚══██╔══╝ ██╔════╝ ╚██╗██╔╝ ╚══██╔══╝ \n"
-        "    ██║    █████╗    ╚███╔╝     ██║    \n"
-        "    ██║    ██╔══╝    ██╔██╗     ██║    \n"
-        "    ██║    ███████╗ ██╔╝ ██╗    ██║    \n"
-        "    ╚═╝    ╚══════╝ ╚═╝  ╚═╝    ╚═╝    \n\n\x1b[49m"
-    )
+    assert render("text", background="red", size=(50, 10)) == (
+        "\x1b[41m{0}\n{0}\n"
+        " ████████╗ ███████╗ ██╗  ██╗ ████████╗ {1}\n"
+        " ╚══██╔══╝ ██╔════╝ ╚██╗██╔╝ ╚══██╔══╝ {1}\n"
+        "    ██║    █████╗    ╚███╔╝     ██║    {1}\n"
+        "    ██║    ██╔══╝    ██╔██╗     ██║    {1}\n"
+        "    ██║    ███████╗ ██╔╝ ██╗    ██║    {1}\n"
+        "    ╚═╝    ╚══════╝ ╚═╝  ╚═╝    ╚═╝    {1}\n{0}\n{0}\x1b[49m"
+    ).format(" " * 50, " " * 11)