Skip to content

Commit

Permalink
Merge pull request #37 from The-Compiler/rich
Browse files Browse the repository at this point in the history
Use rich for nicer tables
  • Loading branch information
dbrgn authored Oct 17, 2024
2 parents bf05e5e + b49acb1 commit 251c7be
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 12 deletions.
1 change: 0 additions & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ source = fahrplan
omit =
*/tests/*
*/.tox/*
*/texttable/*
setup.py
branch = True

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Possible log types:

- ...

## [1.2.0] - 2024-10-16

- [changed] Improve display by using rich instead of texttable

## [1.1.2] - 2019-10-09

- No changes, new release to fix a packaging problem
Expand Down
24 changes: 15 additions & 9 deletions fahrplan/display.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from texttable import Texttable
from rich.table import Table


# Output formats
Expand All @@ -14,7 +14,7 @@ def _get_connection_row(i, connection):
"""
sections = connection['sections']
# Create row
row = [i]
row = [str(i)]
for p in [
lambda x: [x['station_from'], x['station_to']], # Station
lambda x: [x.get('platform_from') or '-', x.get('platform_to') or '-'], # Platform
Expand All @@ -37,14 +37,20 @@ def connectionsTable(connections, output_format):
"""
Get connections in the given output format.
"""
table = Texttable(max_width=0)
table = Table(show_lines=True)
# Alignments
table.set_cols_valign(['m', 't', 't', 't', 't', 't', 'm', 't', 't'])
table.set_cols_align(['l', 'l', 'c', 'l', 'l', 'c', 'c', 'l', 'l'])
# Header
table.add_row(['#', 'Station', 'Platform', 'Date', 'Time', 'Duration', 'Chg.', 'With', 'Occupancy'])
# Define columns
table.add_column("#", justify="left", vertical="middle")
table.add_column("Station", justify="left", vertical="top")
table.add_column("Platform", justify="center", vertical="top")
table.add_column("Date", justify="left", vertical="top")
table.add_column("Time", justify="left", vertical="top")
table.add_column("Duration", justify="center", vertical="top")
table.add_column("Chg.", justify="center", vertical="middle")
table.add_column("With", justify="left", vertical="top")
table.add_column("Occupancy", justify="left", vertical="top")
# Connection rows
for i, connection in enumerate(connections):
table.add_row(_get_connection_row(i, connection))
table.add_row(*_get_connection_row(i, connection))
# Display
return table.draw()
return table
4 changes: 3 additions & 1 deletion fahrplan/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
from .display import Formats, connectionsTable
from .helpers import perror

import rich


def main():
output_format = Formats.SIMPLE
Expand Down Expand Up @@ -95,7 +97,7 @@ def main():

# 3. Output data
table = connectionsTable(connections, output_format)
print(table)
rich.print(table)

if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
requests>=1,<3
python-dateutil>=2.1,<3
texttable>=0.8.6,<2
rich>=13,<14
Binary file modified screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 251c7be

Please sign in to comment.