Skip to content
This repository was archived by the owner on Jun 12, 2026. It is now read-only.

1995parham-learning/article-layout-renderer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Article Layout Renderer

Render a set of articles into a fixed-width, newspaper-style grid in the terminal. Each article is a row of side-by-side text columns; the text inside a column is word-wrapped to that column's width, and articles are framed by horizontal rules.

Input

The input is an array of arrays of objects (articles):

  • The outer array is the list of articles, rendered top to bottom.

  • Each article is an array of columns, rendered left to right.

  • Each column is an object describing one block of text:

    Field Type Meaning
    text str The content to display.
    width int The column's inner width, in characters.

A single global constant gwidth defines the inner width of the horizontal rules that separate the articles.

gwidth = 32
articles = [
    [
        {"text": "This is a short article.", "width": 15},
        {"text": "Now for a longer article. This one has a lot of text.", "width": 16},
    ],
    [
        {"text": "Another article with medium length.", "width": 32},
    ],
]

Output rules

For every article:

  1. Word wrap each column's text to its width. Wrapping happens on spaces only — a word is never split. Each wrapped piece becomes one line of that column.
  2. Align columns side by side. An article is as tall as its tallest column; shorter columns are padded with blank lines so every column has the same number of lines.
  3. Print row by row. For each line index, print the corresponding piece of every column, left-justified and padded to its width, separated and bordered by |.
  4. Frame the articles. A horizontal rule +{'-' * gwidth}+ is printed before the first article and after every article.

Wrapping detail

A column is wrapped by repeatedly taking the longest prefix that fits within width characters, breaking at the last space at or before position width + 1. The remainder continues on the next line until everything fits.

Note: a single word longer than width is not handled — the layout assumes each word fits within its column width.

Example

Running the sample input above produces:

+--------------------------------+
|This is a short|Now for a longer|
|article.       |article. This   |
|               |one has a lot of|
|               |text.           |
+--------------------------------+
|Another article with medium     |
|length.                         |
+--------------------------------+

Note that the column widths (15 + 16) plus their separating/bordering pipes are expected to line up with gwidth (32) so the rules match the columns.

Running

python3 main.py

Development

This project uses uv for dependency management and ruff for linting and formatting.

uv sync                    # install dev dependencies (pytest, ruff)
uv run pytest -v           # run the test suite
uv run ruff check          # lint
uv run ruff format         # auto-format (use --check in CI)

Linting and tests run automatically on every push and pull request via GitHub Actions (see .github/workflows/ci.yml): a lint job runs ruff, and a test job runs pytest across Python 3.9–3.13.

License

Licensed under the GNU General Public License v3.0.

About

Fixed-width, newspaper-style article layout renderer — word-wraps text into side-by-side columns (Python coding challenge)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages