nickjj / dotfiles

Settings for various tools I use.
https://nickjanetakis.com/blog/the-tools-i-use
MIT License
950 stars 183 forks source link

Suggestion: use `LocalAppData` environment variable to get path to Windows Terminal #13

Closed QWp6t closed 3 months ago

QWp6t commented 3 years ago

First of all, thanks for all the effort you've put into your dotfiles. I've been using them for a little while now. I'm actually in the process of cherry-picking your latest stuff when I noticed a line in your set-theme script, figured I'd offer a suggestion.

Why?

This avoids several assumptions about the user's environment, particularly the location of the user's profile folder as well as the the root path for accessing Windows filesystem from WSL (i.e., / vs /mnt/)

How?

Windows has an environment variable called LocalAppData that you can use to get the current user's local application data path.

Terminal

Here's how you would do it in the terminal:

wslpath $(cmd.exe /c echo %LocalAppData% 2>/dev/null)
# => /c/Users/Me/AppData/Local
Explanation Screenshot below illustrates what's going on ![image](https://user-images.githubusercontent.com/2104321/133933830-6a9a1c0b-ac78-4587-940f-c88f7e4b0e68.png)

Python

I'm not very fluent in Python, which is why I didn't open a PR.

I think it would look something like this:

from subprocess import run, PIPE, DEVNULL

def get_windows_app_data_path():
    windows_path = run(['cmd.exe', '/c', 'echo', '%LocalAppData%'], stderr=DEVNULL, stdout=PIPE, universal_newlines=True).stdout.rstrip()
    return run(['wslpath', windows_path], stderr=DEVNULL, stdout=PIPE, universal_newlines=True).stdout.rstrip()

TERMINAL_CONFIG = f'{get_windows_app_data_path()}/Packages/Microsoft.WindowsTerminal_8wekyb3d8bbwe/LocalState/settings.json'  # noqa: E501

Anyway, just a suggestion. Have a good one. 👋🏼

nickjj commented 3 years ago

Hi,

Thanks. Your solution is definitely better for the reasons you specified.

Do you happen to know offhand if wslpath is available with WSL 1 and works the same as what you specified in that screenshot?

QWp6t commented 3 years ago

Looks like it was available with WSL1 after an update (1803), but perhaps if someone hasn't updated Windows 10 since 2017, they won't have it?

https://devblogs.microsoft.com/commandline/windows10v1803/#interoperability

nickjj commented 3 years ago

Sounds good, I'm ok with using your solution in that case.