Skip to content

Commit

Permalink
custom internal_ip segment display format
Browse files Browse the repository at this point in the history
internal_ip new `ip_format` argument:
    - accepts `addr` and `interface` as arguments
    - the default format is '{addr}' (backward compatibility)

Ref powerline#1993
  • Loading branch information
Christophe Delord committed Mar 31, 2019
1 parent 3b85be6 commit bf0d4d8
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions powerline/segments/common/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def render(self, ip, **kwargs):
try:
import netifaces
except ImportError:
def internal_ip(pl, interface='auto', ipv=4):
def internal_ip(pl, interface='auto', ipv=4, ip_format='u{addr}'):
return None
else:
_interface_starts = {
Expand Down Expand Up @@ -106,7 +106,7 @@ def _interface_key(interface):
else:
return 0

def internal_ip(pl, interface='auto', ipv=4):
def internal_ip(pl, interface='auto', ipv=4, ip_format=u'{addr}'):
family = netifaces.AF_INET6 if ipv == 6 else netifaces.AF_INET
if interface == 'auto':
try:
Expand All @@ -122,7 +122,8 @@ def internal_ip(pl, interface='auto', ipv=4):
return None
addrs = netifaces.ifaddresses(interface)
try:
return addrs[family][0]['addr']
addr = addrs[family][0]['addr']
return ip_format.format(addr=addr, interface=interface)
except (KeyError, IndexError):
pl.info("No IPv{0} address found for interface {1}", ipv, interface)
return None
Expand Down Expand Up @@ -152,6 +153,10 @@ def internal_ip(pl, interface='auto', ipv=4):
:param int ipv:
4 or 6 for ipv4 and ipv6 respectively, depending on which IP address you
need exactly.
:param str ip_format:
address format string, will be passed ``addr`` and ``interface`` as the
arguments.
''')


Expand Down

0 comments on commit bf0d4d8

Please sign in to comment.