Skip to content

Commit 06dec82

Browse files
authored
Merge pull request #93 from cadenmyers13/color-print
style: Add ANSI colors to console printing
2 parents 47456c8 + f099e74 commit 06dec82

File tree

5 files changed

+58
-25
lines changed

5 files changed

+58
-25
lines changed

news/color-print.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* Add color to console print statements.
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

src/diffpy/cmi/packsmanager.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@
2828
__all__ = ["PacksManager", "get_package_dir"]
2929

3030

31+
class Styles:
32+
RESET = "\033[0m"
33+
# styles
34+
BOLD = "\033[1m"
35+
UNDER = "\033[4m"
36+
# colors
37+
RED = "\033[31m"
38+
GREEN = "\033[32m"
39+
YELLOW = "\033[33m"
40+
BLUE = "\033[34m"
41+
MAGENTA = "\033[35m"
42+
CYAN = "\033[36m"
43+
44+
3145
def get_package_dir(root_path=None):
3246
"""Get the package directory as a context manager.
3347
@@ -353,20 +367,19 @@ def install_pack(self, identifier: str | Path) -> None:
353367
def print_packs(self) -> None:
354368
"""Print information about available packs."""
355369
uninstalled_packs, installed_packs = [], []
370+
s = Styles()
356371
for pack in self.available_packs():
357372
if self.check_pack(pack):
358373
installed_packs.append(pack)
359374
else:
360375
uninstalled_packs.append(pack)
361-
print("Installed Packs:")
362-
print("----------------")
376+
print(f"{s.BOLD}{s.UNDER}{s.BLUE}Installed Packs:{s.RESET}")
363377
for pack in installed_packs:
364378
if not installed_packs:
365379
print(" (none)")
366380
else:
367381
print(f" {pack}")
368-
print("\nAvailable Packs:")
369-
print("----------------")
382+
print(f"\n{s.BOLD}{s.UNDER}{s.BLUE}Available Packs:{s.RESET}")
370383
if not uninstalled_packs:
371384
print(" (all packs installed)")
372385
else:
@@ -375,11 +388,11 @@ def print_packs(self) -> None:
375388

376389
def print_examples(self) -> None:
377390
"""Print information about available examples."""
378-
print("\nExamples:")
379-
print("---------")
391+
s = Styles()
392+
print(f"\n{s.BOLD}{s.UNDER}{s.CYAN}Examples:{s.RESET}")
380393
examples_dict = self.available_examples()
381394
for pack, examples in examples_dict.items():
382-
print(f" {pack}:")
395+
print(f" {s.BOLD}{pack}:{s.RESET}")
383396
for ex_name, _ in examples:
384397
print(f" - {ex_name}")
385398

src/diffpy/cmi/profilesmanager.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
presence_check,
2727
)
2828
from diffpy.cmi.log import plog
29-
from diffpy.cmi.packsmanager import PacksManager, get_package_dir
29+
from diffpy.cmi.packsmanager import PacksManager, Styles, get_package_dir
3030

3131
__all__ = ["Profile", "ProfilesManager"]
3232

@@ -234,21 +234,20 @@ def install(self, identifier: Union[str, Path]) -> None:
234234

235235
def print_profiles(self) -> None:
236236
"""Print available and installed profiles."""
237+
s = Styles()
237238
installed_profiles, uninstalled_profiles = [], []
238239
for profile_name in self.available_profiles():
239240
if self.check_profile(profile_name):
240241
installed_profiles.append(profile_name)
241242
else:
242243
uninstalled_profiles.append(profile_name)
243-
print("\nInstalled Profiles:")
244-
print("-------------------")
244+
print(f"\n{s.BOLD}{s.UNDER}{s.MAGENTA}Installed Profiles:{s.RESET}")
245245
if not installed_profiles:
246246
print(" (none)")
247247
else:
248248
for profile in installed_profiles:
249249
print(f" {profile}")
250-
print("\nAvailable Profiles:")
251-
print("-------------------")
250+
print(f"\n{s.BOLD}{s.UNDER}{s.MAGENTA}Available Profiles:{s.RESET}")
252251
if not uninstalled_profiles:
253252
print(" (all profiles installed)")
254253
else:

tests/test_packsmanager.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pytest
66

77
from diffpy.cmi import installer
8-
from diffpy.cmi.packsmanager import PacksManager
8+
from diffpy.cmi.packsmanager import PacksManager, Styles
99

1010

1111
def paths_and_names_match(expected, actual, root):
@@ -345,24 +345,22 @@ def test_copy_examples_force(example_cases, expected_paths, force):
345345
assert copied_path.read_text() == original_path.read_text()
346346

347347

348+
s = Styles()
348349
install_params = [
349350
( # input: packs to install
350351
# expected: output showing packA installed but not packB
351352
("packA",),
352-
"""Installed Packs:
353-
----------------
353+
f"""{s.BOLD}{s.UNDER}{s.BLUE}Installed Packs:{s.RESET}
354354
packA
355355
356-
Available Packs:
357-
----------------
356+
{s.BOLD}{s.UNDER}{s.BLUE}Available Packs:{s.RESET}
358357
packB
359358
360-
Examples:
361-
---------
362-
packA:
359+
{s.BOLD}{s.UNDER}{s.CYAN}Examples:{s.RESET}
360+
{s.BOLD}packA:{s.RESET}
363361
- ex1
364362
- ex2
365-
packB:
363+
{s.BOLD}packB:{s.RESET}
366364
- ex1
367365
- ex3
368366
- ex4""",

tests/test_profilesmanager.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import pytest
22
import yaml
33

4+
from diffpy.cmi.packsmanager import Styles
45
from diffpy.cmi.profilesmanager import ProfilesManager
56

7+
s = Styles()
68
install_params = [
79
( # input: profiles to install
810
# expected: print_profile output showing profileA
911
# installed but not profileB
1012
("profileA",),
11-
"""Installed Profiles:
12-
-------------------
13+
f"""{s.BOLD}{s.UNDER}{s.MAGENTA}Installed Profiles:{s.RESET}
1314
profileA
1415
15-
Available Profiles:
16-
-------------------
16+
{s.BOLD}{s.UNDER}{s.MAGENTA}Available Profiles:{s.RESET}
1717
profileB
1818
""",
1919
),

0 commit comments

Comments
 (0)