Skip to content

Commit

Permalink
Refactor the code and use function instead.
Browse files Browse the repository at this point in the history
Signed-off-by: evshary <[email protected]>
  • Loading branch information
evshary committed Oct 9, 2019
1 parent f7b0eab commit 6d7aa20
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions ros2topic/ros2topic/verb/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@
from ros2topic.verb import VerbExtension


def show_topic_info(topic_info, isPub):
print('\n' + ('Published' if isPub else 'Subscribed') + ' topics:')
for (topic_name, topic_types, pub_cnt, sub_cnt) in topic_info:
cnt = pub_cnt if isPub else sub_cnt
if cnt:
topic_types_formatted = ', '.join(topic_types)
cnt_str = str(cnt) + ' ' + ('publisher' if isPub else 'subscriber') \
+ ('s' if cnt > 1 else '')
msg = ' * {topic_name} [{topic_types_formatted}] {cnt_str}'
print(msg.format_map(locals()))


class ListVerb(VerbExtension):
"""Output a list of available topics."""

Expand Down Expand Up @@ -52,24 +64,8 @@ def main(self, *, args):
print(len(topic_names_and_types))
elif topic_names_and_types:
if args.verbose:
print('Published topics:')
for (topic_name, topic_types, pub_cnt, sub_cnt) in topic_info:
if pub_cnt < 1:
continue
topic_types_formatted = ', '.join(topic_types)
cnt_str = str(pub_cnt) + ' publisher' + ('s' if pub_cnt > 1 else '')
msg = ' * {topic_name} [{topic_types_formatted}] {cnt_str}'
print(msg.format_map(locals()))
print('')
print('Subscribed topics:')
for (topic_name, topic_types, pub_cnt, sub_cnt) in topic_info:
if sub_cnt < 1:
continue
topic_types_formatted = ', '.join(topic_types)
cnt_str = str(sub_cnt) + ' subscriber' + ('s' if sub_cnt > 1 else '')
msg = ' * {topic_name} [{topic_types_formatted}] {cnt_str}'
print(msg.format_map(locals()))
print('')
show_topic_info(topic_info, isPub=True)
show_topic_info(topic_info, isPub=False)
else:
for (topic_name, topic_types, pub_cnt, sub_cnt) in topic_info:
msg = '{topic_name}'
Expand Down

0 comments on commit 6d7aa20

Please sign in to comment.