dotfiles icon indicating copy to clipboard operation
dotfiles copied to clipboard

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

Open QWp6t opened this issue 4 years ago • 3 comments

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

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. 👋🏼

QWp6t avatar Sep 19 '21 16:09 QWp6t

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?

nickjj avatar Sep 19 '21 16:09 nickjj

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

QWp6t avatar Sep 19 '21 18:09 QWp6t

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

nickjj avatar Sep 19 '21 20:09 nickjj