#
# Kate Adkins's personal xonshrc
#

import os
import sys
import socket

# Override this to debug the Xonshrc.
XONSHRC_VERBOSE = False

#
# Overrides for warnings
#
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)

def warn(*args, **kwargs):
    pass
import warnings
warnings.warn = warn


#
# Xonsh configuration
#


# Squish down long CWD paths,
$DYNAMIC_CWD_WIDTH="30%"
$DYNAMIC_CWD_ELISION_CHAR='…'

# Allow raw entry of paths; don't require cd.
$AUTO_CD=True

# Automatically push directories, so we can just popd back.
$AUTO_PUSHD=True

# Make it easy to type matching parens
$XONSH_AUTOPAIR=True

# Don't let deprecation warnings spam our console.
__import__('warnings').simplefilter('ignore', DeprecationWarning, 882)

#
# Helpers
#

def try_source(filename):
    """ Sources a given file iff that file exists. """
    from os import path
    from xonsh import aliases

    xshfiles_dir = path.dirname(path.realpath(__file__))
    full_file = path.join(xshfiles_dir, filename + ".xsh")

    if path.exists(full_file):
        if XONSHRC_VERBOSE:
            print(f"Sourcing {full_file}!")
        aliases.source_alias([full_file])
    elif XONSHRC_VERBOSE:
        print(f"Not sourcing {full_file}!")

#
# Environment
#
$GCC_COLOR = "auto"
$GCC_COLORS = "auto"
$EDITOR="nvim"
$VISUAL=$EDITOR


# Start off with some empty local variables; these will hopefully be populated
# by files we source.
PATH_ADDS = []

#
# Per-platform and per-machine environment.
#
try_source(socket.gethostname().split('.')[0])
try_source(sys.platform)

if not 'XONSH_HAS_NIXOS' in ${...}:
    $XONSH_HAS_NIXOS = False;

if not $XONSH_HAS_NIXOS:
    if (p"/etc/lsb_release".exists()) && !(grep -q "DISTRIB_ID=nixos" /etc/lsb-release).returncode == 0:
        if XONSHRC_VERBOSE:
            print("Found a NixOS machine; automatically loading the NixOS extensions.")
        try_source("includes/nixos")
    else:
        if XONSHRC_VERBOSE:
            print("This doesn't appear to be a NixOS machine.")
elif XONSHRC_VERBOSE:
    print("NixOS set up by host customization; no need to auto-load.")


# Note: lower in the list = higher priority
for component in PATH_ADDS:
  $PATH.insert(0, component)


# Aliases.
aliases['vim'] = "nvim"

aliases['en'] = "trans nl:en"
aliases['end'] = "trans -d nl:en"
aliases['nl'] = "trans en:nl"
aliases['nld'] = "trans -d en:nl"

# Keep thread-subproces on.
$THREAD_SUBPROCS=True

# Gulp.
try:
    import pygments.styles.solarized
    $XONSH_COLOR_STYLE='solarized-dark'

    if XONSHRC_VERBOSE:
        print("Loaded color scheme.")

except Exception as e:
    if XONSHRC_VERBOSE:
        print("Failed to load colors!")
        print(e)

if XONSHRC_VERBOSE:
    print("Final color scheme: " + $XONSH_COLOR_STYLE)

# Prompt config.
$STARSHIP_CONFIG = '~/.config/starship.toml'

# Load our Xonsh extensions.
xontrib load whole_word_jumping coreutils sh prompt_starship term_integration


