Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion fxcss/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,62 @@ def _command_row(name, summary, width):
return [f" {shown}{' ' * (width - len(name))}{lines[0].lstrip()}"] + lines[1:]


def _installed_theme_name(manifest):
"""A short name for an installed theme: the repository or folder, not its ref."""
source = manifest.get("source") or {}
if source.get("name"):
return source["name"]
if source.get("path"):
return Path(source["path"]).name or source["path"]
return manifest.get("theme") or "a theme"


def _machine_context():
"""This machine at a glance: the OS, its Firefox and what is themed.

Read from files only, so bare `fxcss` stays instant and never starts
Firefox. The profile and theme are left out when no single profile can be
named, the same case `install` would stop and ask about.
"""
from . import install, style
parts = [style.platform_label(sys.platform)]
try:
binary = core.find_firefox(None)
except SystemExit:
binary = None
if not binary or not Path(binary).exists():
return parts + [f"{style.muted('Firefox')} {style.bad('not found')}",
f"{style.muted('set')} {style.cmd('FIREFOX_BIN')} "
f"{style.muted('to its path')}"]
name, version = core.build_identity(binary)
parts.append(f"{style.muted(name)} {version}" if version else style.muted(name))

profiles = install.discover_profiles()
picks = profiles if len(profiles) == 1 else [p for p in profiles if p["default"]]
if len(picks) != 1:
return parts
parts.append(style.clip(picks[0]["name"], 32))
manifest = install.read_manifest(picks[0]["path"])
if manifest:
theme = style.clip(_installed_theme_name(manifest), 32)
parts.append(f"{style.strong(theme)} {style.muted('installed')}")
elif profile_state(picks[0]) == "unmanaged":
parts.append(style.muted("has its own chrome/"))
else:
parts.append(style.muted("no theme yet"))
return parts


def landing_text():
"""What bare `fxcss` prints: where to start, by what someone came to do."""
from . import style
examples = [(name, args) for _, q, _, subset in GROUPS if q for name, args in subset]
width = max(len(f"fxcss {n} {a}".rstrip()) for n, a in examples) + 2
out = [f" {style.brand('fxcss')} {TAGLINE}"]
try:
out += style.dotted(_machine_context(), indent=9)
except Exception: # a courtesy line must never stop the welcome screen
pass
for title, question, _, subset in GROUPS:
if not question:
continue
Expand Down Expand Up @@ -2744,7 +2794,7 @@ def cmd_doctor(args):
listed = any(b["path"] == firefox for b in builds)
for line in style.rows([
("fxcss", __version__),
("firefox", f"{info['version']} on {style.platform_name(info['os'])}"
("firefox", f"{info['version']} on {style.platform_label(info['os'])}"
f" {style.muted('build ' + str(info['buildID']))}"),
# The builds table below names the binary in use; say it here
# only when that table will not.
Expand Down
22 changes: 22 additions & 0 deletions fxcss/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"""

import base64
import configparser
import hashlib
import json
import os
Expand Down Expand Up @@ -2618,6 +2619,27 @@ def record(label, binary):
for label in CHANNEL_ORDER if label in found]


def application_ini(binary):
"""The [App] section of a build's application.ini, or None if unreadable."""
binary = Path(binary)
for ini in (binary.parent / "application.ini",
binary.parent.parent / "Resources" / "application.ini"):
if ini.is_file():
config = configparser.ConfigParser(interpolation=None)
try:
config.read(ini, encoding="utf-8")
except configparser.Error:
return None
return dict(config["App"]) if config.has_section("App") else {}
return None


def build_identity(binary):
"""A build's (product name, version), never by running it: name may be a fork's."""
app = application_ini(binary) or {}
return app.get("name") or "Firefox", app.get("version")


def firefox_version(binary):
"""The build's version string, or None if it will not say."""
try:
Expand Down
31 changes: 30 additions & 1 deletion fxcss/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def _utf8():
"ok", "!", "x", "->", "-", "*", "...", "-")
POINTER, UPDOWN, ARROWS = ">", "up/down", "arrows"

# Beside an operating system's name, never instead of it. Each is the symbol
# for that system's special key: Command, the Windows key and Meta.
OS_MARKS = {"macOS": "⌘", "Windows": "⊞", "Linux": "◆"} if _utf8() else {}


def set_colour(on):
"""Force colour on or off for the rest of the run (None: detect again)."""
Expand Down Expand Up @@ -349,6 +353,22 @@ def _visible(text):
return re.sub(r"\033\[[0-9;]*m", "", text)


def dotted(items, indent=2, width=None, on=None):
"""Styled items joined by a dim dot, breaking between items at the edge."""
width = width or term_width()
separator = f" {muted(DOT, on=on)} "
lines = [[]]
used = indent
for item in items:
cells = len(_visible(item))
if lines[-1] and used + 3 + cells >= width:
lines.append([])
used = indent
used += cells + (3 if lines[-1] else 0)
lines[-1].append(item)
return [" " * indent + separator.join(line) for line in lines if line]


def title(command_name, context=None, on=None):
"""The first line of a long-running command: the mark, the verb, where."""
line = f" {brand('fxcss', on=on)} {heading(command_name, on=on)}"
Expand All @@ -358,8 +378,17 @@ def title(command_name, context=None, on=None):


def platform_name(os_name):
"""A reader's name for an OS, from what Firefox or sys.platform calls it."""
return {"Darwin": "macOS", "Linux": "Linux", "WINNT": "Windows",
"Windows_NT": "Windows"}.get(os_name, os_name)
"Windows_NT": "Windows", "darwin": "macOS", "linux": "Linux",
"win32": "Windows"}.get(os_name, os_name)


def platform_label(os_name, on=None):
"""`⌘ macOS`: the name, after its symbol in the brand colour when it has one."""
name = platform_name(os_name)
mark = OS_MARKS.get(name)
return f"{brand(mark, on=on)} {name}" if mark else name


def agree(count, many, one):
Expand Down
14 changes: 3 additions & 11 deletions fxcss/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"""

import bisect
import configparser
import gzip
import json
import os
Expand Down Expand Up @@ -186,17 +185,10 @@ def get(self, version):

def build_version(binary):
"""The version string of a Firefox build, read without running it."""
binary = Path(binary)
for ini in (binary.parent / "application.ini",
binary.parent.parent / "Resources" / "application.ini"):
if ini.is_file():
config = configparser.ConfigParser()
try:
config.read(ini, encoding="utf-8")
return config.get("App", "Version", fallback=None)
except configparser.Error:
break
from . import core
app = core.application_ini(binary)
if app is not None:
return app.get("version")
return core.firefox_version(binary)


Expand Down
Loading
Loading