Skip to content

Commit

Permalink
[LAN_IP|WAN_IP] Allows deactivation of single-line output (one_line)
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel FORESTIER committed May 8, 2021
1 parent 1369a69 commit 3f1c4fb
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@ and this project (partially) adheres to [Semantic Versioning](https://semver.org
- CHANGELOG.md file
- Homebrew formula in the default tap
- `CLICOLOR[_FORCE]` environment variables support
- `one_line` configuration option for `LAN_IP` & `WAN_IP` entries
- `TERM_PROGRAM_VERSION` environment variable support for `Terminal`
- `USER`, `LOGNAME` & `LNAME` environment variables support for `User` entry
- Alternative logo style support (see `-l` option with `retro` parameter for Darwin)
Expand Down
25 changes: 15 additions & 10 deletions README.md
Expand Up @@ -312,16 +312,6 @@ Below stand further descriptions for each available (default) option :
"warning_use_percent": 33.3,
"danger_use_percent": 66.7
},
{
"type": "LAN_IP",
// The maximum number of local addresses you want to display.
// `false` --> Unlimited.
"max_count": 2,
// Set to `true` if your local network does not honor RFC1918.
"show_global": false,
// Set to `false` to only display IPv4 LAN addresses.
"ipv6_support": true
},
{
"type": "Disk",
// Which filesystems to show:
Expand Down Expand Up @@ -354,13 +344,28 @@ Below stand further descriptions for each available (default) option :
"warning_use_percent": 50,
"danger_use_percent": 75
},
{
"type": "LAN_IP",
// Set to `false` not to join all IP addresses on the same line.
"one_line": true,
// The maximum number of local addresses you want to display.
// `false` --> Unlimited.
"max_count": 2,
// Set to `true` if your local network does not honor RFC1918.
"show_global": false,
// Set to `false` to only display IPv4 LAN addresses.
"ipv6_support": true
},
{
"type": "WAN_IP",
//
// As explained above, you may temporary hide entries as you wish.
// See below example to hide your public IP addresses before posting your configuration on Internet.
//"disabled": true,
//
// Set to `false` not to join all IP addresses on the same line.
"one_line": true,
//
// Below are settings relative to IPv4/IPv6 public addresses retrieval.
// I hope options are self-explanatory.
// You may set `dns_query` (or `http_url`) to `false` to disable them.
Expand Down
8 changes: 8 additions & 0 deletions archey/entries/lan_ip.py
Expand Up @@ -72,7 +72,15 @@ def output(self, output):
# If we found IP addresses, join them together nicely.
# If not, fall back on default strings according to `netifaces` availability.
if self.value:
if not self.options.get('one_line', True):
# One-line output has been disabled, add one IP address per item.
for ip_address in self.value:
output.append(self.name, ip_address)

return

text_output = ', '.join(self.value)

elif netifaces:
text_output = self._default_strings.get('no_address')
else:
Expand Down
8 changes: 8 additions & 0 deletions archey/entries/wan_ip.py
Expand Up @@ -105,7 +105,15 @@ def output(self, output):
# If we found IP addresses, join them together nicely.
# If not, fall-back on the "No address" string.
if self.value:
if not self.options.get('one_line', True):
# One-line output has been disabled, add one IP address per item.
for ip_address in self.value:
output.append(self.name, ip_address)

return

text_output = ', '.join(self.value)

elif not Environment.DO_NOT_TRACK:
text_output = self._default_strings.get('no_address')
else:
Expand Down
30 changes: 24 additions & 6 deletions archey/test/entries/test_archey_lan_ip.py
@@ -1,7 +1,7 @@
"""Test module for Archey's LAN IP addresses detection module"""

import unittest
from unittest.mock import MagicMock, patch
from unittest.mock import MagicMock, call, patch

from netifaces import AF_INET, AF_INET6, AF_LINK

Expand Down Expand Up @@ -155,16 +155,34 @@ def test_ipv6_and_limit_and_ether(self, _, __):
lan_ip = LanIP(options={'max_count': 3})

output_mock = MagicMock()
lan_ip.output(output_mock)

self.assertListEqual(
lan_ip.value,
['192.168.1.55', '2001::45:6789:abcd:6817', 'fe80::abcd:ef0:abef:dead']
)
self.assertEqual(
output_mock.append.call_args[0][1],
'192.168.1.55, 2001::45:6789:abcd:6817, fe80::abcd:ef0:abef:dead'
)

with self.subTest('Single-line combined output.'):
lan_ip.output(output_mock)
self.assertEqual(
output_mock.append.call_args[0][1],
'192.168.1.55, 2001::45:6789:abcd:6817, fe80::abcd:ef0:abef:dead'
)

output_mock.reset_mock()

with self.subTest('Multi-lines output.'):
lan_ip.options['one_line'] = False

lan_ip.output(output_mock)
self.assertEqual(output_mock.append.call_count, 3)
output_mock.append.assert_has_calls(
[
call('LAN IP', '192.168.1.55'),
call('LAN IP', '2001::45:6789:abcd:6817'),
call('LAN IP', 'fe80::abcd:ef0:abef:dead')
]
)


@patch(
'archey.entries.lan_ip.netifaces.interfaces',
Expand Down
27 changes: 21 additions & 6 deletions archey/test/entries/test_archey_wan_ip.py
@@ -1,7 +1,7 @@
"""Test module for Archey's public IP address detection module"""

import unittest
from unittest.mock import MagicMock, Mock, patch
from unittest.mock import MagicMock, Mock, call, patch

from socket import timeout as SocketTimeoutError
from subprocess import TimeoutExpired
Expand Down Expand Up @@ -108,12 +108,27 @@ def test_two_addresses(self):
"""
self.wan_ip_mock.value = ['XXX.YY.ZZ.TTT', '0123::4567:89a:dead:beef']

WanIP.output(self.wan_ip_mock, self.output_mock)
with self.subTest('Single-line combined output.'):
WanIP.output(self.wan_ip_mock, self.output_mock)

self.assertEqual(
self.output_mock.append.call_args[0][1],
"XXX.YY.ZZ.TTT, 0123::4567:89a:dead:beef"
)
self.assertEqual(
self.output_mock.append.call_args[0][1],
"XXX.YY.ZZ.TTT, 0123::4567:89a:dead:beef"
)

self.output_mock.reset_mock()

with self.subTest('Multi-lines output.'):
self.wan_ip_mock.options['one_line'] = False

WanIP.output(self.wan_ip_mock, self.output_mock)
self.assertEqual(self.output_mock.append.call_count, 2)
self.output_mock.append.assert_has_calls(
[
call('WAN IP', 'XXX.YY.ZZ.TTT'),
call('WAN IP', '0123::4567:89a:dead:beef')
]
)

def test_do_not_track(self):
"""Check whether `DO_NOT_TRACK` environment variable is correctly honored"""
Expand Down
2 changes: 2 additions & 0 deletions config.json
Expand Up @@ -54,12 +54,14 @@
},
{
"type": "LAN_IP",
"one_line": true,
"max_count": 2,
"show_global": false,
"ipv6_support": true
},
{
"type": "WAN_IP",
"one_line": true,
"ipv4": {
"dns_query": "myip.opendns.com",
"dns_resolver": "resolver1.opendns.com",
Expand Down

0 comments on commit 3f1c4fb

Please sign in to comment.