Exemplo n.º 1
0
def change_shell():
    if current_shell()[0] != "zsh":
        try:
            subprocess_call(f"chsh -s $(which zsh) {current_user()}",
                            shell=True)
        except KeyboardInterrupt:
            printer("Canceled by user", foreground=FG.WARNING)
Exemplo n.º 2
0
    def main(self, arguments, *, reload=False, message=False):

        tools_requirements("zsh", "vim", "git")

        create_zshrc_not_exists(f". $HOME/.{package.info['pkg_name']}/init",
                                self.zsh_rc)

        snakypy_path_create(self.config_root)

        create_config(config_content, self.config_file)

        snakypy_file_create(set_zshpower_content, self.init_file, force=True)

        if arguments["--omz"]:
            omz_install(self.omz_root)
            omz_install_plugins(self.omz_root, self.plugins)
            create_zshrc(zshrc_content, self.zsh_rc)
            change_theme_in_zshrc(self.zsh_rc, f"{package.info['pkg_name']}")
            add_plugins_zshrc(self.zsh_rc)
            snakypy_file_create(set_zshpower_content,
                                self.theme_file,
                                force=True)

        install_fonts(self.HOME)

        change_shell()

        printer("Done!", foreground=FG.FINISH) if message else None

        if not arguments["--omz"] and not get_line_source(self.zsh_rc):
            printer(instruction_not_omz, foreground=FG.YELLOW)

        reload_zsh() if reload else None
Exemplo n.º 3
0
def install_fonts(home, force=False):
    url = "https://github.com/snakypy/snakypy-static"
    base_url = f"blob/master/zshpower/fonts/fonts.zip?raw=true"
    font_name = "DejaVu Sans Mono Nerd Font"
    fonts_dir = join(home, f".fonts")
    snakypy_path_create(fonts_dir)
    curl_output = join(home, "zshpower__font.zip")

    if platform.startswith("linux"):
        try:
            if (not isfile(
                    join(fonts_dir, "DejaVu Sans Mono Nerd Font Complete.ttf"))
                    or force):
                printer(
                    f'Please wait, downloading the "{font_name}" font and'
                    "installing...",
                    foreground=FG.QUESTION,
                )
                cmd_line = f"curl -L {join(url, base_url)} -o {curl_output}"
                cmd_snakypy(cmd_line, verbose=True)

                with ZipFile(curl_output, "r") as zip_ref:
                    zip_ref.extractall(fonts_dir)
                    os_remove(curl_output)
                    printer("Done!", foreground=FG.FINISH)
                return True
            return
        except Exception as err:
            raise Exception(f'Error downloading font "{font_name}"', err)
    return
Exemplo n.º 4
0
def checking_init(home):
    """Function that ends commands that depend on the created repository, but
    the repository was not created."""
    if not exists(join(home, f".{package.info['pkg_name']}")):
        printer(
            f'Command "{package.info["pkg_name"]} init" has not been started.'
            "Aborted",
            foreground=FG.WARNING,
        )
        exit(1)
    return True
Exemplo n.º 5
0
def omz_install_plugins(omz_root, plugins):
    try:
        url_master = "https://github.com/zsh-users"
        for plugin in plugins:
            path = join(omz_root, f"custom/plugins/{plugin}")
            clone = f"git clone {url_master}/{plugin}.git {path}"
            if not isdir(path):
                printer(f"Install plugins {plugin}...", foreground=FG.QUESTION)
                cmd_snakypy(clone, verbose=True)
                printer(f"Plugin {plugin} task finished!",
                        foreground=FG.FINISH)
    except Exception:
        raise Exception(f"There was an error installing the plugin")
Exemplo n.º 6
0
 def main():
     print("\n")
     printer("Offered by:".center(50), foreground=FG.GREEN)
     billboard(package.info["organization_name"],
               justify="center",
               foreground=FG.YELLOW)
     printer(f"copyright (c) since 2020\n".center(100), foreground=FG.GREEN)
     credence(
         package.info["name"],
         __version__,
         package.info["home_page"],
         package.info,
         foreground=FG.CYAN,
     )
Exemplo n.º 7
0
def omz_install(omz_root):
    omz_github = "https://github.com/ohmyzsh/ohmyzsh.git"
    cmd_line = f"git clone {omz_github} {omz_root}"
    try:
        if not exists(omz_root):
            printer("Install Oh My ZSH...", foreground=FG.QUESTION)
            cmd_snakypy(cmd_line, verbose=True)
            printer(
                "Oh My ZSH installation process finished.",
                foreground=FG.FINISH,
            )

    except Exception:
        raise Exception("Error downloading Oh My ZSH. Aborted!")
Exemplo n.º 8
0
    def main(self):
        checking_init(self.HOME)

        if read_zshrc_omz(self.zsh_rc):
            if read_zshrc_omz(self.zsh_rc)[0] == "zshpower":
                printer("Already activated. Nothing to do.",
                        foreground=FG.GREEN)
                exit(0)
            change_theme_in_zshrc(self.zsh_rc, "zshpower")
            printer("Activation process finish.", foreground=FG.FINISH)
            reload_zsh()
            return True
        return printer("You are not using Oh My ZSH to run this command.",
                       foreground=FG.WARNING)
Exemplo n.º 9
0
    def main(self, arguments, *, theme_name="robbyrussell"):
        checking_init(self.HOME)

        if read_zshrc_omz(self.zsh_rc):
            if not read_zshrc_omz(self.zsh_rc)[0] == "zshpower":
                printer("Already disabled. Nothing to do.",
                        foreground=FG.GREEN)
                exit(0)
            if not arguments["--theme"]:
                change_theme_in_zshrc(self.zsh_rc, theme_name)
            else:
                change_theme_in_zshrc(self.zsh_rc, arguments["--theme"])
            printer("Deactivation process finish.", foreground=FG.FINISH)
            reload_zsh()
            return True
        return printer("You are not using Oh My ZSH to run this command.",
                       foreground=FG.WARNING)
Exemplo n.º 10
0
    def main(self):
        checking_init(self.HOME)

        if not read_zshrc_omz(self.zsh_rc):
            title = f"Do you want to uninstall {package.info['name']}?"
            options = ["Yes", "No"]
            reply = pick(title, options, colorful=True, index=True)
            if reply is None or reply[0] == 1:
                printer("Whew! Thanks! :)", foreground=FG.GREEN)
                exit(0)
            rm_init_file_package(self.init_file)
            rm_source_zshrc(self.zsh_rc)
        else:
            title = f"What did you want to uninstall?"
            options = [
                f"{package.info['name']}",
                f"{package.info['name']} and Oh My ZSH",
                "Cancel",
            ]
            reply = pick(title, options, colorful=True, index=True)

            if reply is None or reply[0] == 2:
                printer("Whew! Thanks! :)", foreground=FG.GREEN)
                exit(0)

            with contextlib_suppress(Exception):
                os_remove(self.theme_file)

            rm_init_file_package(self.init_file)

            change_theme_in_zshrc(self.zsh_rc, "robbyrussell")

            if reply[0] == 1:
                shutil_rmtree(self.omz_root, ignore_errors=True)
                with contextlib_suppress(Exception):
                    shutil_copyfile(
                        self.zsh_rc,
                        f"{self.zsh_rc}-D{datetime.today().isoformat()}")
                with contextlib_suppress(Exception):
                    os_remove(self.zsh_rc)

            snakypy_file_create("", f"{self.HOME}/.zshrc", force=True)

        reload_zsh()
        printer("Uninstall process finished.", foreground=FG.FINISH)
Exemplo n.º 11
0
def read_zshrc(zshrc):
    try:
        with open(zshrc) as f:
            return f.read()
    except FileNotFoundError as fnf_err:
        printer(f"File not found {fnf_err}", foreground=FG.ERROR)