Skip to content

Commit

Permalink
wsl: Get the distro icon from the package family name
Browse files Browse the repository at this point in the history
All the WSL distributions that was installed from the Microsoft Store
have a package family name that can be used to get the icon of the
distribution.

Signed-off-by: Matheus Castello <matheus@castello.eng.br>
  • Loading branch information
microhobby committed Jul 1, 2024
1 parent bba72b4 commit be8dfe5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
6 changes: 5 additions & 1 deletion tabby-core/src/components/profileIcon.component.pug
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
.icon(
[fastHtmlBind]='pngPath',
*ngIf='!isHTML && isPNG'
)
i.icon(
class='fa-fw {{icon}}',
[style.color]='color',
*ngIf='!isHTML'
*ngIf='!isHTML && !isPNG'
)
.icon(
[fastHtmlBind]='icon',
Expand Down
8 changes: 8 additions & 0 deletions tabby-core/src/components/profileIcon.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ export class ProfileIconComponent extends BaseComponent {
@Input() icon?: string
@Input() color?: string

get pngPath (): string {
return `<img src="${this.icon?.trim()}" width="16" height="16" />`
}

get isHTML (): boolean {
return this.icon?.startsWith('<') ?? false
}

get isPNG (): boolean {
return this.icon?.endsWith('.png') ?? false
}
}
51 changes: 49 additions & 2 deletions tabby-electron/src/shells/wsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { HostAppService, Platform, isWindowsBuild, WIN_BUILD_WSL_EXE_DISTRO_FLAG

import { ShellProvider, Shell } from 'tabby-local'

import { PowerShell } from 'node-powershell'

Check failure on line 9 in tabby-electron/src/shells/wsl.ts

View workflow job for this annotation

GitHub Actions / Lint

Cannot find module 'node-powershell' or its corresponding type declarations.

Check failure on line 9 in tabby-electron/src/shells/wsl.ts

View workflow job for this annotation

GitHub Actions / Lint

Unable to resolve path to module 'node-powershell'

/* eslint-disable block-scoped-var */

try {
Expand Down Expand Up @@ -49,6 +51,14 @@ export class WSLShellProvider extends ShellProvider {
return []
}

// make sure that this will not use the powershell profile
// that may take a long time to load
const pwsh = new PowerShell({
executableOptions: {
'-NoProfile': true,
},
})

const bashPath = `${process.env.windir}\\system32\\bash.exe`
const wslPath = `${process.env.windir}\\system32\\wsl.exe`

Expand All @@ -58,7 +68,24 @@ export class WSLShellProvider extends ShellProvider {

if (lxss?.DefaultDistribution) {
const defaultDistKey = wnr.getRegistryKey(wnr.HK.CU, lxssPath + '\\' + String(lxss.DefaultDistribution.value))
let _icon = wslIconMap.Linux
if (defaultDistKey?.DistributionName) {
// check if the register has PackageFamilyName
if (defaultDistKey.PackageFamilyName) {
// get the icon from the package family name
const packageFamilyName = (defaultDistKey.PackageFamilyName.value as string).split('_')[0]

if (packageFamilyName) {
const _ret = await pwsh.invoke(`Get-AppxPackage ${packageFamilyName} | ConvertTo-Json`)

if (!_ret.hadErrors && _ret.stdout?.toString() !== undefined && _ret.stdout.toString() !== '') {
const appx = JSON.parse(_ret.stdout.toString())
const installationLocation = appx.InstallLocation
_icon = `${installationLocation}\\Assets\\Square44x44Logo.targetsize-16.png`
}
}
}

const shell: Shell = {
id: 'wsl',
name: 'WSL / Default distro',
Expand All @@ -68,7 +95,7 @@ export class WSLShellProvider extends ShellProvider {
COLORTERM: 'truecolor',
},
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
icon: wslIconMap[defaultDistKey.DistributionName.value] ?? wslIconMap.Linux,
icon: wslIconMap[defaultDistKey.DistributionName.value] ?? _icon,
}
shells.push(shell)
}
Expand All @@ -90,11 +117,30 @@ export class WSLShellProvider extends ShellProvider {
return []
}
}

for (const child of wnr.listRegistrySubkeys(wnr.HK.CU, lxssPath) as string[]) {
const childKey = wnr.getRegistryKey(wnr.HK.CU, lxssPath + '\\' + child)
if (!childKey.DistributionName || !childKey.BasePath) {
continue
}

let _icon = wslIconMap.Linux
// check if the register has PackageFamilyName
if (childKey.PackageFamilyName) {
// get the icon from the package family name
const packageFamilyName = (childKey.PackageFamilyName.value as string).split('_')[0]

if (packageFamilyName) {
const _ret = await pwsh.invoke(`Get-AppxPackage ${packageFamilyName} | ConvertTo-Json`)

if (!_ret.hadErrors && _ret.stdout?.toString() !== undefined && _ret.stdout.toString() !== '') {
const appx = JSON.parse(_ret.stdout.toString())
const installationLocation = appx.InstallLocation
_icon = `${installationLocation}\\Assets\\Square44x44Logo.targetsize-16.png`
}
}
}

const wslVersion = (childKey.Flags?.value || 0) & 8 ? 2 : 1
const name = childKey.DistributionName.value
const fsBase = wslVersion === 2 ? `\\\\wsl$\\${name}` : childKey.BasePath.value as string + '\\rootfs'
Expand All @@ -110,11 +156,12 @@ export class WSLShellProvider extends ShellProvider {
COLORTERM: 'truecolor',
},
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
icon: wslIconMap[name] ?? wslIconMap.Linux,
icon: wslIconMap[name] ?? _icon,
}
shells.push(shell)
}

pwsh.dispose()
return shells
}
}

0 comments on commit be8dfe5

Please sign in to comment.