-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathutils.py
More file actions
68 lines (51 loc) · 1.61 KB
/
utils.py
File metadata and controls
68 lines (51 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
"""Utility functions for picker"""
from __future__ import annotations
import os
import pprint
import subprocess
from typing import Any, Optional
from colorama import Fore
class Color:
"""Terminal color output helper"""
@staticmethod
def print_focus(data: str) -> None:
"""Print focused message in yellow"""
print(Fore.YELLOW + data + Fore.RESET)
@staticmethod
def print_success(data: str) -> None:
"""Print success message in green"""
print(Fore.LIGHTGREEN_EX + data + Fore.RESET)
@staticmethod
def print_failed(data: str) -> None:
"""Print failure message in red"""
print(Fore.LIGHTRED_EX + data + Fore.RESET)
@staticmethod
def print(data: Any) -> None:
"""Pretty print any data structure"""
pprint.pprint(data)
def popen(cmd: str) -> str:
"""
Execute shell command and return output
Args:
cmd: Shell command to execute
Returns:
Command output as string
"""
with subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).stdout as source:
return source.read().decode(encoding="utf-8")
def getenv(key_name: str, pick: bool = False) -> Optional[str]:
"""
Get environment variable with optional PICKER_ prefix
Args:
key_name: Environment variable name
pick: If True, try PICKER_ prefixed version first
Returns:
Environment variable value or None
"""
if pick:
if key := os.getenv(f"PICKER_{key_name}"):
return key
return os.getenv(key_name)
return os.getenv(key_name)