at long last, reformat all Python code with 2 space indents
This commit is contained in:
parent
74e978d78e
commit
dbd76019c8
|
@ -4,22 +4,22 @@
|
|||
base16_name = "eighties"
|
||||
name = "base16-" + base16_name
|
||||
base16_colors = [
|
||||
"#2d2d2d", # 0
|
||||
"#393939", # 1
|
||||
"#515151", # 2
|
||||
"#747369", # 3
|
||||
"#a09f93", # 4
|
||||
"#d3d0c8", # 5
|
||||
"#e8e6df", # 6
|
||||
"#f2f0ec", # 7
|
||||
"#f2777a", # 8
|
||||
"#f99157", # 9
|
||||
"#ffcc66", # a
|
||||
"#99cc99", # b
|
||||
"#66cccc", # c
|
||||
"#6699cc", # d
|
||||
"#cc99cc", # e
|
||||
"#d27b53", # f
|
||||
"#2d2d2d", # 0
|
||||
"#393939", # 1
|
||||
"#515151", # 2
|
||||
"#747369", # 3
|
||||
"#a09f93", # 4
|
||||
"#d3d0c8", # 5
|
||||
"#e8e6df", # 6
|
||||
"#f2f0ec", # 7
|
||||
"#f2777a", # 8
|
||||
"#f99157", # 9
|
||||
"#ffcc66", # a
|
||||
"#99cc99", # b
|
||||
"#66cccc", # c
|
||||
"#6699cc", # d
|
||||
"#cc99cc", # e
|
||||
"#d27b53", # f
|
||||
]
|
||||
|
||||
bg = base16_colors[0x0]
|
||||
|
@ -32,18 +32,18 @@ selection_bg = base16_colors[0x2]
|
|||
selection_fg = fg
|
||||
|
||||
ansi_colors = [
|
||||
base16_colors[int(i, 16)] for i in "0 8 B A D E C 5 3 8 B A D E C 7 9 F 1 2 4 6".split()
|
||||
base16_colors[int(i, 16)] for i in "0 8 B A D E C 5 3 8 B A D E C 7 9 F 1 2 4 6".split()
|
||||
]
|
||||
|
||||
link_color = ansi_colors[0xC]
|
||||
|
||||
css_variables_prefix = "dotfiles-colorscheme-"
|
||||
css_variables = {
|
||||
"bg": bg,
|
||||
"fg": fg,
|
||||
"selection-bg": selection_bg,
|
||||
"selection-fg": selection_fg,
|
||||
"cursor-bg": cursor_bg,
|
||||
"cursor-fg": cursor_fg,
|
||||
**{"base-{:02X}".format(index): color for index, color in enumerate(base16_colors)},
|
||||
"bg": bg,
|
||||
"fg": fg,
|
||||
"selection-bg": selection_bg,
|
||||
"selection-fg": selection_fg,
|
||||
"cursor-bg": cursor_bg,
|
||||
"cursor-fg": cursor_fg,
|
||||
**{"base-{:02X}".format(index): color for index, color in enumerate(base16_colors)},
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import _theme as theme
|
|||
|
||||
|
||||
print(
|
||||
"""\
|
||||
"""\
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
|
@ -14,9 +14,9 @@ print(
|
|||
|
||||
|
||||
def print_color(key_name, color):
|
||||
r, g, b = [float(int(color[2 * i + 1:2 * i + 3], 16)) / 255 for i in range(3)]
|
||||
print(
|
||||
"""\
|
||||
r, g, b = [float(int(color[2 * i + 1:2 * i + 3], 16)) / 255 for i in range(3)]
|
||||
print(
|
||||
"""\
|
||||
<key>{} Color</key>
|
||||
<dict>
|
||||
<key>Color Space</key>
|
||||
|
@ -29,7 +29,7 @@ def print_color(key_name, color):
|
|||
<real>{}</real>
|
||||
</dict>\
|
||||
""".format(key_name, r, g, b)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
print_color("Background", theme.bg)
|
||||
|
@ -40,7 +40,7 @@ print_color("Cursor Text", theme.cursor_fg)
|
|||
print_color("Selection Color", theme.selection_bg)
|
||||
print_color("Selected Text Color", theme.selection_fg)
|
||||
for index, color in enumerate(theme.ansi_colors[:16]):
|
||||
print_color("Ansi " + str(index), color)
|
||||
print_color("Ansi " + str(index), color)
|
||||
print_color("Link", theme.link_color)
|
||||
|
||||
print("""\
|
||||
|
|
|
@ -4,7 +4,7 @@ import _theme as theme
|
|||
|
||||
|
||||
def print_color(key_name, color):
|
||||
print("{} {}".format(key_name, color))
|
||||
print("{} {}".format(key_name, color))
|
||||
|
||||
|
||||
print_color("background", theme.bg)
|
||||
|
@ -14,7 +14,7 @@ print_color("cursor_text_color", theme.cursor_fg)
|
|||
print_color("selection_background", theme.selection_bg)
|
||||
print_color("selection_foreground", theme.selection_fg)
|
||||
for index, color in enumerate(theme.ansi_colors[:16]):
|
||||
print_color("color" + str(index), color)
|
||||
print_color("color" + str(index), color)
|
||||
print_color("url_color", theme.link_color)
|
||||
|
||||
print_color("active_border_color", theme.ansi_colors[2])
|
||||
|
|
|
@ -4,9 +4,9 @@ import _theme as theme
|
|||
import os
|
||||
|
||||
with open(os.path.join(os.path.dirname(__file__), "prismjs-theme-src.css")) as f:
|
||||
css_src = f.read()
|
||||
css_src = f.read()
|
||||
|
||||
for var_name, color in theme.css_variables.items():
|
||||
css_src = css_src.replace("var(--{}{})".format(theme.css_variables_prefix, var_name), color)
|
||||
css_src = css_src.replace("var(--{}{})".format(theme.css_variables_prefix, var_name), color)
|
||||
|
||||
print(css_src)
|
||||
|
|
|
@ -8,4 +8,4 @@ import _theme as theme
|
|||
# 0,0,0,0,170,170,170,170,85,85,85,85,255,255,255,255
|
||||
|
||||
for i in range(3):
|
||||
print(",".join([str(int(color[2 * i + 1:2 * i + 3], 16)) for color in theme.ansi_colors[:16]]))
|
||||
print(",".join([str(int(color[2 * i + 1:2 * i + 3], 16)) for color in theme.ansi_colors[:16]]))
|
||||
|
|
|
@ -4,11 +4,11 @@ import _theme as theme
|
|||
|
||||
|
||||
def print_color(key_name, color):
|
||||
print("{}={}".format(key_name, color))
|
||||
print("{}={}".format(key_name, color))
|
||||
|
||||
|
||||
print_color("background", theme.bg)
|
||||
print_color("foreground", theme.fg)
|
||||
print_color("cursor", theme.cursor_bg)
|
||||
for index, color in enumerate(theme.ansi_colors[:16]):
|
||||
print_color("color" + str(index), color)
|
||||
print_color("color" + str(index), color)
|
||||
|
|
|
@ -5,5 +5,5 @@ import _theme as theme
|
|||
|
||||
print(":root {")
|
||||
for var_name, color in theme.css_variables.items():
|
||||
print(" --{}{}: {};".format(theme.css_variables_prefix, var_name, color))
|
||||
print(" --{}{}: {};".format(theme.css_variables_prefix, var_name, color))
|
||||
print("}")
|
||||
|
|
|
@ -8,15 +8,15 @@ print("let dotfiles_colorscheme_base16_name = '{}'".format(theme.base16_name))
|
|||
print("let dotfiles_colorscheme_base16_colors = [")
|
||||
gui_to_cterm_mapping = [0, 18, 19, 8, 20, 7, 21, 15, 1, 16, 3, 2, 6, 4, 5, 17]
|
||||
for colors_pair in zip(theme.base16_colors[:16], gui_to_cterm_mapping):
|
||||
print("\\ {{'gui': '{}', 'cterm': '{:>02}'}},".format(*colors_pair))
|
||||
print("\\ {{'gui': '{}', 'cterm': '{:>02}'}},".format(*colors_pair))
|
||||
print("\\ ]")
|
||||
|
||||
|
||||
def print_terminal_color(key_name, color):
|
||||
print("let terminal_color_{} = '{}'".format(key_name, color))
|
||||
print("let terminal_color_{} = '{}'".format(key_name, color))
|
||||
|
||||
|
||||
print_terminal_color("background", theme.bg)
|
||||
print_terminal_color("foreground", theme.fg)
|
||||
for index, color in enumerate(theme.ansi_colors[:16]):
|
||||
print_terminal_color(str(index), color)
|
||||
print_terminal_color(str(index), color)
|
||||
|
|
|
@ -5,27 +5,27 @@ import json
|
|||
|
||||
|
||||
ANSI_COLOR_NAMES = [
|
||||
"Black",
|
||||
"Red",
|
||||
"Green",
|
||||
"Yellow",
|
||||
"Blue",
|
||||
"Magenta",
|
||||
"Cyan",
|
||||
"White",
|
||||
"Black",
|
||||
"Red",
|
||||
"Green",
|
||||
"Yellow",
|
||||
"Blue",
|
||||
"Magenta",
|
||||
"Cyan",
|
||||
"White",
|
||||
]
|
||||
|
||||
colors = {
|
||||
"terminal.background": theme.bg,
|
||||
"terminal.foreground": theme.fg,
|
||||
"terminal.selectionBackground": theme.selection_bg,
|
||||
"terminalCursor.background": theme.cursor_fg,
|
||||
"terminalCursor.foreground": theme.cursor_bg,
|
||||
"terminal.background": theme.bg,
|
||||
"terminal.foreground": theme.fg,
|
||||
"terminal.selectionBackground": theme.selection_bg,
|
||||
"terminalCursor.background": theme.cursor_fg,
|
||||
"terminalCursor.foreground": theme.cursor_bg,
|
||||
}
|
||||
|
||||
for color_brightness in [False, True]:
|
||||
for color_index, color_name in enumerate(ANSI_COLOR_NAMES):
|
||||
color = theme.ansi_colors[color_index + int(color_brightness) * len(ANSI_COLOR_NAMES)]
|
||||
colors["terminal.ansi" + ("Bright" if color_brightness else "") + color_name] = color
|
||||
for color_index, color_name in enumerate(ANSI_COLOR_NAMES):
|
||||
color = theme.ansi_colors[color_index + int(color_brightness) * len(ANSI_COLOR_NAMES)]
|
||||
colors["terminal.ansi" + ("Bright" if color_brightness else "") + color_name] = color
|
||||
|
||||
print(json.dumps(colors, ensure_ascii=False, indent=2))
|
||||
|
|
|
@ -3,17 +3,17 @@
|
|||
import _theme as theme
|
||||
|
||||
for attr in [
|
||||
"bg",
|
||||
"fg",
|
||||
"cursor_bg",
|
||||
"cursor_fg",
|
||||
"selection_bg",
|
||||
"selection_fg",
|
||||
"link_color",
|
||||
"bg",
|
||||
"fg",
|
||||
"cursor_bg",
|
||||
"cursor_fg",
|
||||
"selection_bg",
|
||||
"selection_fg",
|
||||
"link_color",
|
||||
]:
|
||||
color = getattr(theme, attr)
|
||||
print("colorscheme_{}={}".format(attr, color[1:]))
|
||||
color = getattr(theme, attr)
|
||||
print("colorscheme_{}={}".format(attr, color[1:]))
|
||||
print("colorscheme_ansi_colors=(")
|
||||
for color in theme.ansi_colors:
|
||||
print(" {}".format(color[1:]))
|
||||
print(" {}".format(color[1:]))
|
||||
print(")")
|
||||
|
|
|
@ -6,6 +6,8 @@ let s:ignored_errors = []
|
|||
let s:ignored_errors += ['E111']
|
||||
" Indent is not a multiple of 4 for comments
|
||||
let s:ignored_errors += ['E114']
|
||||
" Indent for continuation lines is smaller than expected
|
||||
let s:ignored_errors += ['E121']
|
||||
" Line too long
|
||||
let s:ignored_errors += ['E501']
|
||||
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
[style]
|
||||
based_on_style = google
|
||||
column_limit = 99
|
||||
indent_width = 4
|
||||
indent_width = 2
|
||||
continuation_indent_width = 2
|
||||
blank_lines_between_top_level_imports_and_variables = 2
|
||||
dedent_closing_brackets = true
|
||||
coalesce_brackets = true
|
||||
|
|
|
@ -5,89 +5,89 @@ from pathlib import Path
|
|||
from typing import Iterable, NoReturn
|
||||
|
||||
if os.name == "posix":
|
||||
DOTFILES_CONFIG_DIR: Path = Path.home() / ".config" / "dotfiles"
|
||||
DOTFILES_CACHE_DIR: Path = Path.home() / ".cache" / "dotfiles"
|
||||
DOTFILES_CONFIG_DIR: Path = Path.home() / ".config" / "dotfiles"
|
||||
DOTFILES_CACHE_DIR: Path = Path.home() / ".cache" / "dotfiles"
|
||||
|
||||
|
||||
def platform_not_supported_error() -> NoReturn:
|
||||
raise Exception("platform '{}' is not supported!".format(sys.platform))
|
||||
raise Exception("platform '{}' is not supported!".format(sys.platform))
|
||||
|
||||
|
||||
def run_chooser(choices: Iterable[str], prompt: str = None, async_read: bool = False) -> int:
|
||||
supports_result_index = True
|
||||
if os.isatty(sys.stderr.fileno()):
|
||||
process_args = [
|
||||
"fzf",
|
||||
"--with-nth=2..",
|
||||
"--height=50%",
|
||||
"--reverse",
|
||||
"--tiebreak=index",
|
||||
]
|
||||
supports_result_index = False
|
||||
elif sys.platform == "darwin":
|
||||
process_args = ["choose", "-i"]
|
||||
elif os.name == "posix":
|
||||
process_args = ["rofi", "-dmenu", "-i", "-format", "i"]
|
||||
if prompt is not None:
|
||||
process_args += ["-p", prompt]
|
||||
if async_read:
|
||||
process_args += ["-async-pre-read", "0"]
|
||||
else:
|
||||
platform_not_supported_error()
|
||||
supports_result_index = True
|
||||
if os.isatty(sys.stderr.fileno()):
|
||||
process_args = [
|
||||
"fzf",
|
||||
"--with-nth=2..",
|
||||
"--height=50%",
|
||||
"--reverse",
|
||||
"--tiebreak=index",
|
||||
]
|
||||
supports_result_index = False
|
||||
elif sys.platform == "darwin":
|
||||
process_args = ["choose", "-i"]
|
||||
elif os.name == "posix":
|
||||
process_args = ["rofi", "-dmenu", "-i", "-format", "i"]
|
||||
if prompt is not None:
|
||||
process_args += ["-p", prompt]
|
||||
if async_read:
|
||||
process_args += ["-async-pre-read", "0"]
|
||||
else:
|
||||
platform_not_supported_error()
|
||||
|
||||
chooser_process = subprocess.Popen(process_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
chooser_process = subprocess.Popen(process_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
||||
|
||||
with chooser_process.stdin as pipe:
|
||||
for index, choice in enumerate(choices):
|
||||
assert "\n" not in choice
|
||||
if not supports_result_index:
|
||||
pipe.write(str(index).encode())
|
||||
pipe.write(b" ")
|
||||
pipe.write(choice.encode())
|
||||
pipe.write(b"\n")
|
||||
with chooser_process.stdin as pipe:
|
||||
for index, choice in enumerate(choices):
|
||||
assert "\n" not in choice
|
||||
if not supports_result_index:
|
||||
pipe.write(str(index).encode())
|
||||
pipe.write(b" ")
|
||||
pipe.write(choice.encode())
|
||||
pipe.write(b"\n")
|
||||
|
||||
exit_code: int = chooser_process.wait()
|
||||
if exit_code != 0:
|
||||
raise Exception("chooser process failed with exit code {}".format(exit_code))
|
||||
exit_code: int = chooser_process.wait()
|
||||
if exit_code != 0:
|
||||
raise Exception("chooser process failed with exit code {}".format(exit_code))
|
||||
|
||||
chosen_index = int(chooser_process.stdout.read().strip().split()[0])
|
||||
return chosen_index
|
||||
chosen_index = int(chooser_process.stdout.read().strip().split()[0])
|
||||
return chosen_index
|
||||
|
||||
|
||||
def send_notification(title: str, message: str, url: str = None) -> None:
|
||||
if sys.platform == "darwin":
|
||||
process_args = [
|
||||
"terminal-notifier",
|
||||
"-title",
|
||||
title,
|
||||
"-message",
|
||||
message,
|
||||
"-open",
|
||||
]
|
||||
if url is not None:
|
||||
process_args += [url]
|
||||
elif os.name == "posix":
|
||||
process_args = [
|
||||
"notify-send",
|
||||
"--icon=utilities-terminal",
|
||||
"--expire-time=3000",
|
||||
title,
|
||||
message,
|
||||
]
|
||||
else:
|
||||
platform_not_supported_error()
|
||||
if sys.platform == "darwin":
|
||||
process_args = [
|
||||
"terminal-notifier",
|
||||
"-title",
|
||||
title,
|
||||
"-message",
|
||||
message,
|
||||
"-open",
|
||||
]
|
||||
if url is not None:
|
||||
process_args += [url]
|
||||
elif os.name == "posix":
|
||||
process_args = [
|
||||
"notify-send",
|
||||
"--icon=utilities-terminal",
|
||||
"--expire-time=3000",
|
||||
title,
|
||||
message,
|
||||
]
|
||||
else:
|
||||
platform_not_supported_error()
|
||||
|
||||
subprocess.run(process_args, check=True)
|
||||
subprocess.run(process_args, check=True)
|
||||
|
||||
|
||||
def set_clipboard(text: str) -> None:
|
||||
# TODO: somehow merge program selection with the logic in `zsh/functions.zsh`
|
||||
if sys.platform == "darwin":
|
||||
process_args = ["pbcopy"]
|
||||
elif os.name == "posix":
|
||||
process_args = ["xsel", "--clipboard", "--input"]
|
||||
# process_args = ["xclip", "-in", "-selection", "clipboard"]
|
||||
else:
|
||||
platform_not_supported_error()
|
||||
# TODO: somehow merge program selection with the logic in `zsh/functions.zsh`
|
||||
if sys.platform == "darwin":
|
||||
process_args = ["pbcopy"]
|
||||
elif os.name == "posix":
|
||||
process_args = ["xsel", "--clipboard", "--input"]
|
||||
# process_args = ["xclip", "-in", "-selection", "clipboard"]
|
||||
else:
|
||||
platform_not_supported_error()
|
||||
|
||||
subprocess.run(process_args, input=text.encode(), check=True)
|
||||
subprocess.run(process_args, input=text.encode(), check=True)
|
||||
|
|
|
@ -9,58 +9,58 @@ from typing import Any, IO
|
|||
|
||||
|
||||
def read_bool(buf: IO[bytes]) -> bool:
|
||||
return buf.read(1)[0] == 1
|
||||
return buf.read(1)[0] == 1
|
||||
|
||||
|
||||
def read_number(buf: IO[bytes]) -> float:
|
||||
return struct.unpack("<d", buf.read(8))[0]
|
||||
return struct.unpack("<d", buf.read(8))[0]
|
||||
|
||||
|
||||
def _read_length(buf: IO[bytes]) -> int:
|
||||
return struct.unpack("<I", buf.read(4))[0]
|
||||
return struct.unpack("<I", buf.read(4))[0]
|
||||
|
||||
|
||||
def read_string(buf: IO[bytes]) -> str:
|
||||
is_empty = read_bool(buf)
|
||||
if is_empty:
|
||||
return ""
|
||||
len_ = buf.read(1)[0]
|
||||
if len_ == 0xFF:
|
||||
len_ = _read_length(buf)
|
||||
return buf.read(len_).decode("utf8")
|
||||
is_empty = read_bool(buf)
|
||||
if is_empty:
|
||||
return ""
|
||||
len_ = buf.read(1)[0]
|
||||
if len_ == 0xFF:
|
||||
len_ = _read_length(buf)
|
||||
return buf.read(len_).decode("utf8")
|
||||
|
||||
|
||||
def read_dictionary(buf: IO[bytes]) -> dict[str, Any]:
|
||||
len_ = _read_length(buf)
|
||||
value: dict[str, Any] = {}
|
||||
for _ in range(len_):
|
||||
key = read_string(buf)
|
||||
value[key] = read(buf)
|
||||
return value
|
||||
len_ = _read_length(buf)
|
||||
value: dict[str, Any] = {}
|
||||
for _ in range(len_):
|
||||
key = read_string(buf)
|
||||
value[key] = read(buf)
|
||||
return value
|
||||
|
||||
|
||||
def read_list(buf: IO[bytes]) -> list[Any]:
|
||||
len_ = _read_length(buf)
|
||||
value: list[Any] = []
|
||||
for _ in range(len_):
|
||||
read_string(buf)
|
||||
value.append(read(buf))
|
||||
return value
|
||||
len_ = _read_length(buf)
|
||||
value: list[Any] = []
|
||||
for _ in range(len_):
|
||||
read_string(buf)
|
||||
value.append(read(buf))
|
||||
return value
|
||||
|
||||
|
||||
def read(buf: IO[bytes]) -> Any:
|
||||
type_, _any_type_flag = buf.read(2)
|
||||
if type_ == 0:
|
||||
return None
|
||||
elif type_ == 1:
|
||||
return read_bool(buf)
|
||||
elif type_ == 2:
|
||||
return read_number(buf)
|
||||
elif type_ == 3:
|
||||
return read_string(buf)
|
||||
elif type_ == 4:
|
||||
return read_list(buf)
|
||||
elif type_ == 5:
|
||||
return read_dictionary(buf)
|
||||
else:
|
||||
raise Exception("unknown property tree type 0x{:02x}".format(type_))
|
||||
type_, _any_type_flag = buf.read(2)
|
||||
if type_ == 0:
|
||||
return None
|
||||
elif type_ == 1:
|
||||
return read_bool(buf)
|
||||
elif type_ == 2:
|
||||
return read_number(buf)
|
||||
elif type_ == 3:
|
||||
return read_string(buf)
|
||||
elif type_ == 4:
|
||||
return read_list(buf)
|
||||
elif type_ == 5:
|
||||
return read_dictionary(buf)
|
||||
else:
|
||||
raise Exception("unknown property tree type 0x{:02x}".format(type_))
|
||||
|
|
|
@ -3,29 +3,29 @@ from fractions import Fraction
|
|||
|
||||
|
||||
def factors(n):
|
||||
result = set()
|
||||
for i in range(1, int(sqrt(n)) + 1):
|
||||
if n % i == 0:
|
||||
result.add(i)
|
||||
result.add(n // i)
|
||||
return result
|
||||
result = set()
|
||||
for i in range(1, int(sqrt(n)) + 1):
|
||||
if n % i == 0:
|
||||
result.add(i)
|
||||
result.add(n // i)
|
||||
return result
|
||||
|
||||
|
||||
def solve_quadratic(a, b, c):
|
||||
if a == 0:
|
||||
raise Exception("not a quadratic equation")
|
||||
if a == 0:
|
||||
raise Exception("not a quadratic equation")
|
||||
else:
|
||||
d = b ** 2 - 4 * a * c
|
||||
print("D = " + str(d))
|
||||
if d < 0:
|
||||
print("no solutions")
|
||||
elif d > 0:
|
||||
sd = sqrt(d)
|
||||
print("sqrt(D) = " + str(sd))
|
||||
print("x1 = " + str((-b + sd) / (2 * a)))
|
||||
print("x2 = " + str((-b - sd) / (2 * a)))
|
||||
else:
|
||||
d = b ** 2 - 4 * a * c
|
||||
print("D = " + str(d))
|
||||
if d < 0:
|
||||
print("no solutions")
|
||||
elif d > 0:
|
||||
sd = sqrt(d)
|
||||
print("sqrt(D) = " + str(sd))
|
||||
print("x1 = " + str((-b + sd) / (2 * a)))
|
||||
print("x2 = " + str((-b - sd) / (2 * a)))
|
||||
else:
|
||||
print("x = " + str(-b / (2 * a)))
|
||||
print("x = " + str(-b / (2 * a)))
|
||||
|
||||
|
||||
print("loaded Python calculator")
|
||||
|
|
|
@ -5,18 +5,18 @@ COLORS = [ansi.code_to_chars(30 + color_index) for color_index in range(0, 8)]
|
|||
|
||||
|
||||
def colored(string, *colors):
|
||||
return "".join(colors + (string, Style.RESET_ALL))
|
||||
return "".join(colors + (string, Style.RESET_ALL))
|
||||
|
||||
|
||||
def bright_colored(string, *colors):
|
||||
return "".join(colors + (Style.BRIGHT, string, Style.RESET_ALL))
|
||||
return "".join(colors + (Style.BRIGHT, string, Style.RESET_ALL))
|
||||
|
||||
|
||||
def colorize_percent(percent, warning, critical, inverse=False):
|
||||
COLORS = [Fore.GREEN, Fore.YELLOW, Fore.RED]
|
||||
COLORS = [Fore.GREEN, Fore.YELLOW, Fore.RED]
|
||||
|
||||
color_index = 0 if percent < warning else 1 if percent < critical else 2
|
||||
if inverse:
|
||||
color_index = 2 - color_index
|
||||
color_index = 0 if percent < warning else 1 if percent < critical else 2
|
||||
if inverse:
|
||||
color_index = 2 - color_index
|
||||
|
||||
return colored("%.2f%%" % percent, COLORS[color_index])
|
||||
return colored("%.2f%%" % percent, COLORS[color_index])
|
||||
|
|
|
@ -1,34 +1,34 @@
|
|||
def humanize_timedelta(timedelta):
|
||||
result = []
|
||||
result = []
|
||||
|
||||
days = timedelta.days
|
||||
mm, ss = divmod(timedelta.seconds, 60)
|
||||
hh, mm = divmod(mm, 60)
|
||||
days = timedelta.days
|
||||
mm, ss = divmod(timedelta.seconds, 60)
|
||||
hh, mm = divmod(mm, 60)
|
||||
|
||||
def plural(n):
|
||||
return n, "s" if abs(n) != 1 else ""
|
||||
def plural(n):
|
||||
return n, "s" if abs(n) != 1 else ""
|
||||
|
||||
if days > 0:
|
||||
result.append("%d day%s" % plural(days))
|
||||
if hh > 0 or result:
|
||||
result.append("%d hour%s" % plural(hh))
|
||||
if mm > 0 or result:
|
||||
result.append("%d min%s" % plural(mm))
|
||||
if len(result) <= 1:
|
||||
result.append("%d sec%s" % plural(ss))
|
||||
if days > 0:
|
||||
result.append("%d day%s" % plural(days))
|
||||
if hh > 0 or result:
|
||||
result.append("%d hour%s" % plural(hh))
|
||||
if mm > 0 or result:
|
||||
result.append("%d min%s" % plural(mm))
|
||||
if len(result) <= 1:
|
||||
result.append("%d sec%s" % plural(ss))
|
||||
|
||||
return ", ".join(result)
|
||||
return ", ".join(result)
|
||||
|
||||
|
||||
def humanize_bytes(bytes):
|
||||
units = ["B", "kB", "MB", "GB"]
|
||||
units = ["B", "kB", "MB", "GB"]
|
||||
|
||||
factor = 1
|
||||
unit = ""
|
||||
for unit in units:
|
||||
next_factor = factor << 10
|
||||
if bytes < next_factor:
|
||||
break
|
||||
factor = next_factor
|
||||
factor = 1
|
||||
unit = ""
|
||||
for unit in units:
|
||||
next_factor = factor << 10
|
||||
if bytes < next_factor:
|
||||
break
|
||||
factor = next_factor
|
||||
|
||||
return "%.2f %s" % (float(bytes) / factor, unit)
|
||||
return "%.2f %s" % (float(bytes) / factor, unit)
|
||||
|
|
|
@ -13,24 +13,24 @@ logo_line_widths = [len(re.sub(r"{\d}", "", line)) for line in logo_lines]
|
|||
logo_width = max(logo_line_widths)
|
||||
|
||||
for line_index in range(0, max(len(logo_lines), len(info_lines))):
|
||||
line = ""
|
||||
line = ""
|
||||
|
||||
logo_line_width = 0
|
||||
logo_line_width = 0
|
||||
|
||||
if line_index < len(logo_lines):
|
||||
logo_line = logo_lines[line_index]
|
||||
logo_line_width = logo_line_widths[line_index]
|
||||
if line_index < len(logo_lines):
|
||||
logo_line = logo_lines[line_index]
|
||||
logo_line_width = logo_line_widths[line_index]
|
||||
|
||||
line += Style.BRIGHT
|
||||
line += logo_line.format(*COLORS)
|
||||
line += Style.RESET_ALL
|
||||
line += Style.BRIGHT
|
||||
line += logo_line.format(*COLORS)
|
||||
line += Style.RESET_ALL
|
||||
|
||||
line += " " * (logo_width - logo_line_width + 3)
|
||||
line += " " * (logo_width - logo_line_width + 3)
|
||||
|
||||
if line_index < len(info_lines):
|
||||
info_line = info_lines[line_index]
|
||||
line += info_line
|
||||
if line_index < len(info_lines):
|
||||
info_line = info_lines[line_index]
|
||||
line += info_line
|
||||
|
||||
print(line)
|
||||
print(line)
|
||||
|
||||
print("")
|
||||
|
|
|
@ -11,202 +11,200 @@ from humanize import humanize_bytes, humanize_timedelta
|
|||
|
||||
|
||||
def get_system_info():
|
||||
info_lines = []
|
||||
info_lines = []
|
||||
|
||||
def info(name, value, *format_args):
|
||||
line = bright_colored(name + ":", Fore.YELLOW) + " " + value
|
||||
if format_args:
|
||||
line = line % format_args
|
||||
info_lines.append(line)
|
||||
def info(name, value, *format_args):
|
||||
line = bright_colored(name + ":", Fore.YELLOW) + " " + value
|
||||
if format_args:
|
||||
line = line % format_args
|
||||
info_lines.append(line)
|
||||
|
||||
username = getuser()
|
||||
hostname = _get_hostname()
|
||||
username = getuser()
|
||||
hostname = _get_hostname()
|
||||
|
||||
info_lines.append(
|
||||
bright_colored(username, Fore.BLUE) + "@" + bright_colored(hostname, Fore.RED)
|
||||
)
|
||||
info_lines.append("")
|
||||
info_lines.append(bright_colored(username, Fore.BLUE) + "@" + bright_colored(hostname, Fore.RED))
|
||||
info_lines.append("")
|
||||
|
||||
distro_id, distro_name, distro_version, distro_codename = _get_distro_info()
|
||||
info("OS", " ".join([distro_name, distro_version, distro_codename]))
|
||||
distro_id, distro_name, distro_version, distro_codename = _get_distro_info()
|
||||
info("OS", " ".join([distro_name, distro_version, distro_codename]))
|
||||
|
||||
logo_path = os.path.join(os.path.dirname(__file__), "logos", distro_id)
|
||||
with open(logo_path) as logo_file:
|
||||
logo_lines = logo_file.read().splitlines()
|
||||
logo_path = os.path.join(os.path.dirname(__file__), "logos", distro_id)
|
||||
with open(logo_path) as logo_file:
|
||||
logo_lines = logo_file.read().splitlines()
|
||||
|
||||
info("Kernel", "%s %s", platform.system(), platform.release())
|
||||
info("Kernel", "%s %s", platform.system(), platform.release())
|
||||
|
||||
info("Uptime", humanize_timedelta(_get_uptime()))
|
||||
info("Uptime", humanize_timedelta(_get_uptime()))
|
||||
|
||||
users_info = _get_users()
|
||||
if users_info:
|
||||
info("Users", users_info)
|
||||
users_info = _get_users()
|
||||
if users_info:
|
||||
info("Users", users_info)
|
||||
|
||||
shell = _get_shell()
|
||||
if shell is not None:
|
||||
info("Shell", shell)
|
||||
shell = _get_shell()
|
||||
if shell is not None:
|
||||
info("Shell", shell)
|
||||
|
||||
info_lines.append("")
|
||||
info_lines.append("")
|
||||
|
||||
cpu_usage_info = _get_cpu_usage()
|
||||
if cpu_usage_info is not None:
|
||||
info("CPU Usage", "%s", cpu_usage_info)
|
||||
info("Memory", "%s / %s (%s)", *_get_memory())
|
||||
cpu_usage_info = _get_cpu_usage()
|
||||
if cpu_usage_info is not None:
|
||||
info("CPU Usage", "%s", cpu_usage_info)
|
||||
info("Memory", "%s / %s (%s)", *_get_memory())
|
||||
|
||||
for disk_info in _get_disks():
|
||||
info("Disk (%s)", "%s / %s (%s)", *disk_info)
|
||||
for disk_info in _get_disks():
|
||||
info("Disk (%s)", "%s / %s (%s)", *disk_info)
|
||||
|
||||
battery_info = _get_battery()
|
||||
if battery_info is not None:
|
||||
info("Battery", "%s (%s)", *battery_info)
|
||||
battery_info = _get_battery()
|
||||
if battery_info is not None:
|
||||
info("Battery", "%s (%s)", *battery_info)
|
||||
|
||||
info_lines.append("")
|
||||
info_lines.append("")
|
||||
|
||||
for local_ip_address in _get_local_ipv4_addresses():
|
||||
info("Local IPv4 Address (%s)", "%s", *local_ip_address)
|
||||
for local_ip_address in _get_local_ipv4_addresses():
|
||||
info("Local IPv4 Address (%s)", "%s", *local_ip_address)
|
||||
|
||||
return logo_lines, info_lines
|
||||
return logo_lines, info_lines
|
||||
|
||||
|
||||
def _get_hostname():
|
||||
hostname = socket.gethostname()
|
||||
return hostname
|
||||
hostname = socket.gethostname()
|
||||
return hostname
|
||||
|
||||
|
||||
def _get_uptime():
|
||||
return datetime.now() - datetime.fromtimestamp(psutil.boot_time())
|
||||
return datetime.now() - datetime.fromtimestamp(psutil.boot_time())
|
||||
|
||||
|
||||
def _get_users():
|
||||
users = {}
|
||||
users = {}
|
||||
|
||||
for user in psutil.users():
|
||||
name = user.name
|
||||
terminal = user.terminal
|
||||
if name in users:
|
||||
users[name].append(terminal)
|
||||
else:
|
||||
users[name] = [terminal]
|
||||
for user in psutil.users():
|
||||
name = user.name
|
||||
terminal = user.terminal
|
||||
if name in users:
|
||||
users[name].append(terminal)
|
||||
else:
|
||||
users[name] = [terminal]
|
||||
|
||||
result = []
|
||||
result = []
|
||||
|
||||
for name in users:
|
||||
terminals = users[name]
|
||||
for name in users:
|
||||
terminals = users[name]
|
||||
|
||||
colored_name = bright_colored(name, Fore.BLUE)
|
||||
colored_terminals = [colored(str(term), Style.DIM, Fore.WHITE) for term in terminals]
|
||||
colored_name = bright_colored(name, Fore.BLUE)
|
||||
colored_terminals = [colored(str(term), Style.DIM, Fore.WHITE) for term in terminals]
|
||||
|
||||
terminals_str = ", ".join(colored_terminals)
|
||||
if len(colored_terminals) > 1:
|
||||
terminals_str = "(%s)" % terminals_str
|
||||
result.append(colored_name + "@" + terminals_str)
|
||||
terminals_str = ", ".join(colored_terminals)
|
||||
if len(colored_terminals) > 1:
|
||||
terminals_str = "(%s)" % terminals_str
|
||||
result.append(colored_name + "@" + terminals_str)
|
||||
|
||||
return ", ".join(result)
|
||||
return ", ".join(result)
|
||||
|
||||
|
||||
def _get_shell():
|
||||
return os.environ.get("SHELL")
|
||||
return os.environ.get("SHELL")
|
||||
|
||||
|
||||
def _get_cpu_usage():
|
||||
try:
|
||||
percent = psutil.cpu_percent()
|
||||
except Exception as e:
|
||||
print("Error in _get_cpu_usage:", e)
|
||||
return None
|
||||
try:
|
||||
percent = psutil.cpu_percent()
|
||||
except Exception as e:
|
||||
print("Error in _get_cpu_usage:", e)
|
||||
return None
|
||||
|
||||
return colorize_percent(percent, warning=60, critical=80)
|
||||
return colorize_percent(percent, warning=60, critical=80)
|
||||
|
||||
|
||||
def _get_memory():
|
||||
memory = psutil.virtual_memory()
|
||||
return (
|
||||
humanize_bytes(memory.used),
|
||||
humanize_bytes(memory.total),
|
||||
colorize_percent(memory.percent, warning=60, critical=80),
|
||||
)
|
||||
memory = psutil.virtual_memory()
|
||||
return (
|
||||
humanize_bytes(memory.used),
|
||||
humanize_bytes(memory.total),
|
||||
colorize_percent(memory.percent, warning=60, critical=80),
|
||||
)
|
||||
|
||||
|
||||
def _get_disks():
|
||||
result = []
|
||||
result = []
|
||||
|
||||
for disk in psutil.disk_partitions(all=False):
|
||||
if psutil.WINDOWS and ("cdrom" in disk.opts or disk.fstype == ""):
|
||||
# skip cd-rom drives with no disk in it on Windows; they may raise
|
||||
# ENOENT, pop-up a Windows GUI error for a non-ready partition or
|
||||
# just hang
|
||||
continue
|
||||
for disk in psutil.disk_partitions(all=False):
|
||||
if psutil.WINDOWS and ("cdrom" in disk.opts or disk.fstype == ""):
|
||||
# skip cd-rom drives with no disk in it on Windows; they may raise
|
||||
# ENOENT, pop-up a Windows GUI error for a non-ready partition or
|
||||
# just hang
|
||||
continue
|
||||
|
||||
usage = psutil.disk_usage(disk.mountpoint)
|
||||
result.append((
|
||||
disk.mountpoint,
|
||||
humanize_bytes(usage.used),
|
||||
humanize_bytes(usage.total),
|
||||
colorize_percent(usage.percent, warning=70, critical=85),
|
||||
))
|
||||
usage = psutil.disk_usage(disk.mountpoint)
|
||||
result.append((
|
||||
disk.mountpoint,
|
||||
humanize_bytes(usage.used),
|
||||
humanize_bytes(usage.total),
|
||||
colorize_percent(usage.percent, warning=70, critical=85),
|
||||
))
|
||||
|
||||
return result
|
||||
return result
|
||||
|
||||
|
||||
def _get_battery():
|
||||
if not hasattr(psutil, "sensors_battery"):
|
||||
return None
|
||||
if not hasattr(psutil, "sensors_battery"):
|
||||
return None
|
||||
|
||||
try:
|
||||
battery = psutil.sensors_battery()
|
||||
except Exception as e:
|
||||
print("Error in _get_battery:", e)
|
||||
return None
|
||||
try:
|
||||
battery = psutil.sensors_battery()
|
||||
except Exception as e:
|
||||
print("Error in _get_battery:", e)
|
||||
return None
|
||||
|
||||
if battery is None:
|
||||
return None
|
||||
if battery is None:
|
||||
return None
|
||||
|
||||
percent = battery.percent
|
||||
if battery.power_plugged:
|
||||
status = "charging" if percent < 100 else "fully charged"
|
||||
else:
|
||||
status = "%s left" % humanize_timedelta(timedelta(seconds=battery.secsleft))
|
||||
return colorize_percent(percent, critical=10, warning=20, inverse=True), status
|
||||
percent = battery.percent
|
||||
if battery.power_plugged:
|
||||
status = "charging" if percent < 100 else "fully charged"
|
||||
else:
|
||||
status = "%s left" % humanize_timedelta(timedelta(seconds=battery.secsleft))
|
||||
return colorize_percent(percent, critical=10, warning=20, inverse=True), status
|
||||
|
||||
|
||||
def _get_local_ipv4_addresses():
|
||||
result = []
|
||||
result = []
|
||||
|
||||
for interface, addresses in psutil.net_if_addrs().items():
|
||||
for address in addresses:
|
||||
if address.family != socket.AF_INET:
|
||||
# allow only IPv4 addresses (skip IPv6 and MAC, for example)
|
||||
continue
|
||||
if interface.startswith("lo"):
|
||||
# skip loopback interfaces
|
||||
continue
|
||||
for interface, addresses in psutil.net_if_addrs().items():
|
||||
for address in addresses:
|
||||
if address.family != socket.AF_INET:
|
||||
# allow only IPv4 addresses (skip IPv6 and MAC, for example)
|
||||
continue
|
||||
if interface.startswith("lo"):
|
||||
# skip loopback interfaces
|
||||
continue
|
||||
|
||||
result.append((interface, address.address))
|
||||
result.append((interface, address.address))
|
||||
|
||||
return result
|
||||
return result
|
||||
|
||||
|
||||
def _get_distro_info():
|
||||
if psutil.WINDOWS:
|
||||
return "windows", platform.system(), platform.release(), ""
|
||||
elif psutil.OSX:
|
||||
import plistlib
|
||||
if psutil.WINDOWS:
|
||||
return "windows", platform.system(), platform.release(), ""
|
||||
elif psutil.OSX:
|
||||
import plistlib
|
||||
|
||||
with open("/System/Library/CoreServices/SystemVersion.plist", "rb") as f:
|
||||
sw_vers = plistlib.load(f)
|
||||
return "mac", sw_vers["ProductName"], sw_vers["ProductVersion"], ""
|
||||
elif _is_android():
|
||||
from subprocess import check_output
|
||||
with open("/System/Library/CoreServices/SystemVersion.plist", "rb") as f:
|
||||
sw_vers = plistlib.load(f)
|
||||
return "mac", sw_vers["ProductName"], sw_vers["ProductVersion"], ""
|
||||
elif _is_android():
|
||||
from subprocess import check_output
|
||||
|
||||
android_version = check_output(["getprop", "ro.build.version.release"])
|
||||
return "android", "Android", android_version.decode().strip(), ""
|
||||
elif psutil.LINUX:
|
||||
import distro
|
||||
android_version = check_output(["getprop", "ro.build.version.release"])
|
||||
return "android", "Android", android_version.decode().strip(), ""
|
||||
elif psutil.LINUX:
|
||||
import distro
|
||||
|
||||
return distro.id(), distro.name(), distro.version(), distro.codename()
|
||||
return distro.id(), distro.name(), distro.version(), distro.codename()
|
||||
|
||||
raise NotImplementedError("unsupported OS")
|
||||
raise NotImplementedError("unsupported OS")
|
||||
|
||||
|
||||
def _is_android():
|
||||
return os.path.isdir("/system/app") and os.path.isdir("/system/priv-app")
|
||||
return os.path.isdir("/system/app") and os.path.isdir("/system/priv-app")
|
||||
|
|
Loading…
Reference in New Issue