From b2f4e57538d958c4042172eae81d300292fb512d Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 21 Jul 2024 10:51:09 -0500 Subject: [PATCH] maybe wezterm --- .gitconfig | 2 ++ config.ini | 5 ++++ nvim/after/ftplugin/lua.lua | 1 + prefs/host.py | 18 ++++++++++---- prefs/installer.py | 29 +++++++++++++---------- prefs/linker.py | 19 ++++++++------- prefs/resource.py | 16 +++++++++---- prefs/run.py | 18 +++++++------- prefs/utils.py | 15 ++++++++++++ wezterm/config.lua | 47 +++++++++++++++++++++++++++++++++++++ 10 files changed, 131 insertions(+), 39 deletions(-) create mode 100644 nvim/after/ftplugin/lua.lua create mode 100644 prefs/utils.py create mode 100644 wezterm/config.lua diff --git a/.gitconfig b/.gitconfig index 6a1bcba..3a34c7a 100644 --- a/.gitconfig +++ b/.gitconfig @@ -3,3 +3,5 @@ email = jordanorelli@gmail.com [pull] rebase = true +[rebase] + instructionFormat = (%an <%ae>) %s diff --git a/config.ini b/config.ini index e01de11..6f33b43 100644 --- a/config.ini +++ b/config.ini @@ -45,6 +45,11 @@ when: host.has('kitty') and host.is_linux files: desktop/kitty.desktop > .local/share/applications/kitty.desktop +[home wezterm] +when: host.has('WezTerm.app') +files: + wezterm/config.lua > .wezterm.lua + [link-files nix-config] when: host.has('nix') target_root: ~/.config diff --git a/nvim/after/ftplugin/lua.lua b/nvim/after/ftplugin/lua.lua new file mode 100644 index 0000000..0b3214b --- /dev/null +++ b/nvim/after/ftplugin/lua.lua @@ -0,0 +1 @@ +print("You are in lua") diff --git a/prefs/host.py b/prefs/host.py index 2a8304d..65915d0 100644 --- a/prefs/host.py +++ b/prefs/host.py @@ -11,38 +11,40 @@ import pathlib import shutil from functools import cached_property + class Host: """ hacking the python module system a little to make the module look like a singleton object so that it can have properties """ + @cached_property def is_wsl(self): """ true if we're running Linux on WSL """ - return 'WSL2' in platform.platform() + return "WSL2" in platform.platform() @cached_property def is_linux(self): """ true if we're on linux (including WSL), false otherwise """ - return platform.system() == 'Linux' + return platform.system() == "Linux" @cached_property def is_windows(self): """ true if we're on Windows (and running Python from Windows) """ - return platform.system() == 'Windows' + return platform.system() == "Windows" @cached_property def is_darwin(self): """ true if we're on MacOS """ - return platform.system() == 'Darwin' + return platform.system() == "Darwin" @cached_property def is_admin(self): @@ -69,10 +71,16 @@ class Host: """ return pathlib.Path.home() - def has(self, cmd): + def has(self, cmd: str): """ whether or not the machine has the specified command """ + if self.is_darwin and cmd.endswith(".app"): + app_dir = pathlib.Path("/Applications") + app_path = app_dir / cmd + if app_path.exists(): + return True return shutil.which(cmd) is not None + sys.modules[__name__] = Host() diff --git a/prefs/installer.py b/prefs/installer.py index a5f2f55..34e0876 100644 --- a/prefs/installer.py +++ b/prefs/installer.py @@ -1,27 +1,23 @@ -import configparser -import json -import pathlib -import sys +from configparser import ConfigParser, SectionProxy from functools import cached_property -from . import host -from . import sections -from . import targets from .options import Options from .resource import Resource + class Installer: """ manages the installation of preferences files """ + def __init__(self): self.options = Options.from_cli_args() @cached_property - def config(self): + def config(self) -> ConfigParser: path = self.options.config_path - with open(path, 'r', encoding='utf-8') as config_file: - parser = configparser.ConfigParser() + with open(path, "r", encoding="utf-8") as config_file: + parser = ConfigParser() # make keys case-sensitive parser.optionxform = str parser.read_file(config_file) @@ -37,7 +33,14 @@ class Installer: else: print(f"SKIP [{name}]") - def when(self, section): - if clause := section.get('when', None): - return eval(clause) + def when(self, section: SectionProxy) -> bool: + if clause := section.get("when", None): + from . import host + + given_locals = { + "host": host, + } + + v = eval(clause, None, given_locals) + return bool(v) return True diff --git a/prefs/linker.py b/prefs/linker.py index 866f1b5..4a512c7 100644 --- a/prefs/linker.py +++ b/prefs/linker.py @@ -4,10 +4,12 @@ import os from . import host + class Linker: """ Linker links files from soome source to some target """ + def __init__(self, source_root, target_root): self.source_root = source_root self.target_root = target_root @@ -23,8 +25,6 @@ class Linker: parent_dir = target_path.parent parent_dir.mkdir(parents=True) - exists = os.path.exists(str(target_path)) - is_link = os.path.islink(str(target_path)) if target_path.exists(): if target_path.is_symlink(): if target_path.resolve() == source_path: @@ -38,7 +38,9 @@ class Linker: print(f"rm {target_path} (dir)") shutil.rmtree(target_path) else: - print(f"skip {source_path}: unable to handle target at {target_path}: is not a symlink, file, or directory") + print( + f"skip {source_path}: unable to handle target at {target_path}: is not a symlink, file, or directory" + ) return else: if target_path.is_symlink(): @@ -51,14 +53,15 @@ class Linker: print(f"ERROR creating link: {e}") print(f"link {target_path} -> {source_path}") + class LinkFiles: - resource_name = 'link-files' + resource_name = "link-files" def __init__(self, label, section): self.label = label - self.target_root = pathlib.Path(section['target_root']).expanduser() + self.target_root = pathlib.Path(section["target_root"]).expanduser() self.source_root = host.dotfiles_root - self.parse_files(section.get('files', '')) + self.parse_files(section.get("files", "")) def parse_files(self, text): self.files = [] @@ -73,8 +76,8 @@ class LinkFiles: def __repr__(self): if self.label: - return f'' - return '' + return f"" + return "" def run(self): linker = Linker(host.dotfiles_root, self.target_root) diff --git a/prefs/resource.py b/prefs/resource.py index 216f15e..e29b61a 100644 --- a/prefs/resource.py +++ b/prefs/resource.py @@ -1,14 +1,22 @@ +from configparser import SectionProxy from .home import Home from .linker import LinkFiles from .cargo import CargoInstall from .run import Run from . import git + class Resource: - resource_types = [Home, LinkFiles, CargoInstall, Run, git.Repo] + resource_types: list[type] = [ + Home, + LinkFiles, + CargoInstall, + Run, + git.Repo, + ] @classmethod - def from_name(cls, name): + def from_name(cls, name: str) -> type: """ from_name is to be implemented by resource classes """ @@ -18,8 +26,8 @@ class Resource: raise ValueError(f"No section type has name {name}") @classmethod - def from_section(cls, name, section): - parts = name.split(' ') + def from_section(cls, name: str, section: SectionProxy): + parts = name.split(" ") name = parts[0] try: label = parts[1] diff --git a/prefs/run.py b/prefs/run.py index 6c53464..55690d1 100644 --- a/prefs/run.py +++ b/prefs/run.py @@ -1,16 +1,15 @@ import subprocess -import os -import shutil from . import host + class Run: - resource_name = 'run' + resource_name = "run" def __init__(self, label, section): self.label = label or None - self.cmd = section.get('cmd', '').split() - self.cwd = section.get('cwd', host.dotfiles_root) - self.shell = section.get('shell', False) + self.cmd = section.get("cmd", "").split() + self.cwd = section.get("cwd", host.dotfiles_root) + self.shell = section.get("shell", False) def run(self): kwargs = { @@ -20,6 +19,7 @@ class Run: "shell": self.shell, } with subprocess.Popen(self.cmd, **kwargs) as proc: - line = proc.stdout.read() - if line: - print(line) + if proc.stdout is not None: + line = proc.stdout.read() + if line: + print(line) diff --git a/prefs/utils.py b/prefs/utils.py new file mode 100644 index 0000000..7ac65d1 --- /dev/null +++ b/prefs/utils.py @@ -0,0 +1,15 @@ +from typing import Sequence, Tuple +from pathlib import Path + + +def parse_files(text: str) -> Sequence[Tuple[Path, Path]]: + files = [] + lines = [s for s in text.splitlines() if s] + for line in lines: + parts = line.split(">", 2) + if len(parts) == 1: + pair = (Path(parts[0].strip()), Path(parts[0].strip())) + else: + pair = (Path(parts[0].strip()), Path(parts[1].strip())) + files.append(pair) + return files diff --git a/wezterm/config.lua b/wezterm/config.lua new file mode 100644 index 0000000..34c34f6 --- /dev/null +++ b/wezterm/config.lua @@ -0,0 +1,47 @@ +local wezterm = require 'wezterm' + +local config = wezterm.config_builder() + +-- JellyBeans is not recognized +-- config.color_scheme = 'JellyBeans' +config.adjust_window_size_when_changing_font_size = false + +-- Set the default starting directory when opening the terminal +-- config.default_cwd + +-- The scroll bar is disabled by default but sometimes when you have really +-- long output I like it. +config.enable_scroll_bar = true + +-- whether or not the tab bar is shown +config.enable_tab_bar = true + +-- hide the tab bar in windows that have only one tab +config.hide_tab_bar_if_only_one_tab = true + +config.font_size = 14 + +config.launch_menu = { + { + args = { 'lazydocker' }, + } +} + +config.keys = { + { + key = 'Enter', + mods = 'CMD', + action = wezterm.action.ToggleFullScreen, + }, +} + +config.native_macos_fullscreen_mode = true + +config.scrollback_lines = 10000 + +config.tab_bar_at_bottom = true + +-- config.initial_cols = 90 +-- config.initial_rows = 24 + +return config