Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvements, preference functions, and display improvements #120

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

# Don't check in the Output dir
Output/

Terminal-Icons/Data/*.xml
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,28 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.12.0] Unreleased

### Fixed

- Module load time is improved due to pre-processing theme files

### Added

- Add `Get-TerminalIconsPreference` function to retrieve current module preferences
- Add `Set-TerminalIconsPreference` function to set module preferences
- Add ability to format file/folder date times in difference formats and display in either local or UTC time
- Add ability to display file sizes in KB, MB, or GB
- Add ability to display file/folder name in normal, bold, italic, or underlined font
- Add icon and color for wellknown file `jenkinsfile`.
- Add icon and color for c++ files (.cxx and .c++)
- Add icon and color for wellknown file `makefile`.
- Add icon and color for svelte files (.svelte).
- Add icon and color for generic database files (.db)
- Add icon and color for Scala Build Tool files (.sbt)

### Changed

- On MacOS and Linux, display the `UnixMode` property instead of `Mode`. Also display the `User`, and `Group` properties

## [0.11.0] 2023-07-05

### Added
Expand Down
4 changes: 2 additions & 2 deletions Terminal-Icons/Private/Add-Theme.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function Add-Theme {
$confirmMsg = "Are you sure you want to add file [$resolvedPath]?"
$operation = "Add $($Type.ToLower())"
if ($PSCmdlet.ShouldProcess($statusMsg, $confirmMsg, $operation) -or $Force.IsPresent) {
if (-not $script:userThemeData.Themes.$Type.ContainsKey($item.BaseName) -or $Force.IsPresent) {
if (-not $script:current.Themes.$Type.ContainsKey($item.BaseName) -or $Force.IsPresent) {

$theme = Import-PowerShellDataFile $item.FullName

Expand All @@ -70,7 +70,7 @@ function Add-Theme {
})
}

$script:userThemeData.Themes.$Type[$theme.Name] = $theme
$script:current.Themes.$Type[$theme.Name] = $theme
Save-Theme -Theme $theme -Type $Type
} else {
Write-Error "$Type theme [$($theme.Name)] already exists. Use the -Force switch to overwrite."
Expand Down
34 changes: 34 additions & 0 deletions Terminal-Icons/Private/Get-CurrentSettings.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function Get-CurrentSettings {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '')]
[OutputType([hashtable])]
param(
[Preferences]$Preferences = $script:prefs,

[string]$DefaultTheme = $script:defaultTheme
)

# Default settings
$current = @{
IconTheme = $DefaultTheme
ColorTheme = $DefaultTheme
RendorModeUnicode = Get-RendorModeUnicode 'Bold'
DateTimeFormat = [DateTimeFormat]::GENERAL_SHORT_TIME
DateTimeFormatString = 'g'
TimeZoneDisplay = [TimeZoneDisplay]::Local
FileSizeDisplay = [FileSizeDisplay]::Bytes
Themes = @{
Color = @{}
Icon = @{}
}
}

$current.IconTheme = $Preferences.IconTheme
$current.ColorTheme = $Preferences.ColorTheme
$current.RendorModeUnicode = Get-RendorModeUnicode $Preferences.RendorMode
$current.DataTimeFormat = $Preferences.DateTimeFormat
$current.DateTimeFormatString = Get-DateTimeFormatString $Preferences.DateTimeFormat
$current.TimeZoneDisplay = $Preferences.TimeZoneDisplay
$current.FileSizeDisplay = $Preferences.FileSizeDisplay

$current
}
17 changes: 17 additions & 0 deletions Terminal-Icons/Private/Get-DateTimeFormatString.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function Get-DateTimeFormatString {
[OutputType([String])]
param(
[DateTimeFormat]$Format
)

switch ($Format) {
([DateTimeFormat]::ISO8601) { 'u'; break }
([DateTimeFormat]::SORTABLE) { 's'; break }
([DateTimeFormat]::RFC1123) { 'r'; break }
([DateTimeFormat]::GENERAL_SHORT_TIME) { 'g'; break }
([DateTimeFormat]::GENERAL_LONG_TIME) { 'G'; break }
([DateTimeFormat]::FULL_SHORT_TIME) { 'f'; break }
([DateTimeFormat]::FULL_LONG_TIME) { 'F'; break }
default {''}
}
}
8 changes: 8 additions & 0 deletions Terminal-Icons/Private/Get-PreferencesFile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function Get-PreferencesFile {
[OutputType([String])]
param(
[string]$Filename = $script:prefsFile
)

[IO.Path]::Combine((Get-ThemeStoragePath), $Filename)
}
14 changes: 14 additions & 0 deletions Terminal-Icons/Private/Get-RendorModeUnicode.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function Get-RendorModeUnicode {
[OutputType([String])]
param(
[RendorMode]$RendorMode
)

switch ($RendorMode) {
([RendorMode]::Normal) { $null; break }
([RendorMode]::Bold) { $script:bold; break}
([RendorMode]::Italic) { $script:italic; break }
([RendorMode]::Underline) { $script:underline; break }
default { $null}
}
}
6 changes: 4 additions & 2 deletions Terminal-Icons/Private/Get-ThemeStoragePath.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ function Get-ThemeStoragePath {
param()

if ($IsLinux -or $IsMacOs) {
if (-not ($basePath = $env:XDG_CONFIG_HOME)) {
$basePath = $env:XDG_CONFIG_HOME
if (-not $basePath) {
$basePath = [IO.Path]::Combine($HOME, '.local', 'share')
}
} else {
if (-not ($basePath = $env:APPDATA)) {
$basePath = $env:APPDATA
if (-not $basePath) {
$basePath = [Environment]::GetFolderPath('ApplicationData')
}
}
Expand Down
23 changes: 14 additions & 9 deletions Terminal-Icons/Private/Import-Preferences.ps1
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
function Import-Preferences {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
[OutputType([hashtable])]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '')]
[OutputType([Preferences])]
[cmdletbinding()]
param(
[parameter(ValueFromPipeline)]
[string]$Path = (Join-Path (Get-ThemeStoragePath) 'prefs.xml'),

[string]$DefaultThemeName = $script:defaultTheme
[string]$Path = (Get-PreferencesFile)
)

begin {
$defaultPrefs = @{
CurrentColorTheme = $DefaultThemeName
CurrentIconTheme = $DefaultThemeName
}
$defaultPrefs = [Preferences]::new()
}

process {
if (Test-Path $Path) {
try {
Import-Clixml -Path $Path -ErrorAction Stop
$prefs = Import-Clixml -Path $Path -ErrorAction Stop
if (-not $prefs -is [Preferences]) {
# Preferences are in old format. Convert to new format.
$newPrefs = [Preferences]::new()
$newPrefs.ColorTheme = $prefs.CurrentColorTheme
$newPrefs.IconTheme = $prefs.CurrentIconTheme
$newPrefs
} else {
$prefs
}
} catch {
Write-Warning "Unable to parse [$Path]. Setting default preferences."
$defaultPrefs
Expand Down
13 changes: 13 additions & 0 deletions Terminal-Icons/Private/Import-XmlThemes.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function Import-XmlThemes {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '')]
param()

$script:iconThemes = Import-Clixml -Path $script:moduleRoot/Data/iconThemes.xml
$script:colorThemes = Import-Clixml -Path $script:moduleRoot/Data/colorThemes.xml
$script:colorSequences = Import-Clixml -Path $script:moduleRoot/Data/colorSequences.xml
$script:glyphs = Import-Clixml -Path $script:moduleRoot/Data/glyphs.xml
$script:current.Themes = @{
Color = $script:colorThemes
Icon = $script:iconThemes
}
}
35 changes: 28 additions & 7 deletions Terminal-Icons/Private/Resolve-Icon.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ function Resolve-Icon {
[Parameter(Mandatory, ValueFromPipeline)]
[IO.FileSystemInfo]$FileInfo,

[string]$IconTheme = $script:userThemeData.CurrentIconTheme,
[string]$IconTheme = $script:current.IconTheme,

[string]$ColorTheme = $script:userThemeData.CurrentColorTheme
[string]$ColorTheme = $script:current.ColorTheme
)

begin {
$icons = $script:userThemeData.Themes.Icon[$IconTheme]
$icons = $script:current.Themes.Icon[$IconTheme]
$colors = $script:colorSequences[$ColorTheme]
}

Expand Down Expand Up @@ -39,7 +39,7 @@ function Resolve-Icon {
if ($colors) {
$colorSeq = $colors.Types.($type)['junction']
} else {
$colorSet = $script:colorReset
$colorSeq = $script:colorReset
}
$displayInfo['Target'] = ' ' + $glyphs['nf-md-arrow_right_thick'] + ' ' + $FileInfo.Target
break
Expand All @@ -53,7 +53,7 @@ function Resolve-Icon {
if ($colors) {
$colorSeq = $colors.Types.($type)['symlink']
} else {
$colorSet = $script:colorReset
$colorSeq = $script:colorReset
}
$displayInfo['Target'] = ' ' + $glyphs['nf-md-arrow_right_thick'] + ' ' + $FileInfo.Target
break
Expand All @@ -63,7 +63,17 @@ function Resolve-Icon {
$iconName = $icons.Types.$type.WellKnown[$FileInfo.Name]
if (-not $iconName) {
if ($FileInfo.PSIsContainer) {
$iconName = $icons.Types.$type[$FileInfo.Name]
$iconName = $icons.Types.Directories[$FileInfo.Name]

# Try matching directory name to any regex listings in the theme as a last resort
if (-not $iconName -and $icons.Types.Directories.regex) {
foreach ($directoryRegex in $icons.Types.Directories.regex.GetEnumerator()) {
if ($FileInfo.Name -match $directoryRegex.Key) {
$iconName = $directoryRegex.Value
break
}
}
}
} elseif ($icons.Types.$type.ContainsKey($FileInfo.Extension)) {
$iconName = $icons.Types.$type[$FileInfo.Extension]
} else {
Expand All @@ -75,6 +85,7 @@ function Resolve-Icon {
$iconName = $icons.Types.$type[$fullExtension]
}
}

if (-not $iconName) {
$iconName = $icons.Types.$type['']
}
Expand All @@ -95,7 +106,17 @@ function Resolve-Icon {
$colorSeq = $colors.Types.$type.WellKnown[$FileInfo.Name]
if (-not $colorSeq) {
if ($FileInfo.PSIsContainer) {
$colorSeq = $colors.Types.$type[$FileInfo.Name]
$colorSeq = $colors.Types.Directories[$FileInfo.Name]

# Try matching directory name to any regex listings in the theme as a last resort
if (-not $colorSeq -and $colors.Types.Directories.regex) {
foreach ($directoryRegex in $colors.Types.Directories.regex.GetEnumerator()) {
if ($FileInfo.Name -match $directoryRegex.Key) {
$colorSeq = $directoryRegex.Value
break
}
}
}
} elseif ($colors.Types.$type.ContainsKey($FileInfo.Extension)) {
$colorSeq = $colors.Types.$type[$FileInfo.Extension]
} else {
Expand Down
5 changes: 3 additions & 2 deletions Terminal-Icons/Private/Save-Preferences.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
function Save-Preferences {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '')]
[cmdletbinding()]
param(
[parameter(Mandatory, ValueFromPipeline)]
[hashtable]$Preferences,
[Preferences]$Preferences,

[string]$Path = (Join-Path (Get-ThemeStoragePath) 'prefs.xml')
[string]$Path = (Get-PreferencesFile)
)

process {
Expand Down
12 changes: 6 additions & 6 deletions Terminal-Icons/Private/Set-Theme.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ function Set-Theme {
)

if ([string]::IsNullOrEmpty($Name)) {
$script:userThemeData."Current$($Type)Theme" = $null
$script:prefs."Current$($Type)Theme" = ''
$script:current."$($Type)Theme" = $null
$script:prefs."$($Type)Theme" = ''
Save-Preferences $script:prefs
} else {
if (-not $script:userThemeData.Themes.$Type.ContainsKey($Name)) {
if (-not $script:current.Themes.$Type.ContainsKey($Name)) {
Write-Error "$Type theme [$Name] not found."
} else {
$script:userThemeData."Current$($Type)Theme" = $Name
$script:prefs."Current$($Type)Theme" = $Name
Save-Theme -Theme $userThemeData.Themes.$Type[$Name] -Type $type
$script:current."$($Type)Theme" = $Name
$script:prefs."$($Type)Theme" = $Name
Save-Theme -Theme $script:current.Themes.$Type[$Name] -Type $type
Save-Preferences $script:prefs
}
}
Expand Down
18 changes: 15 additions & 3 deletions Terminal-Icons/Public/Format-TerminalIcons.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,31 @@ function Format-TerminalIcons {
Outputs a colorized string with an icon prepended.
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '')]
[OutputType([string])]
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[IO.FileSystemInfo]$FileInfo
[IO.FileSystemInfo]$FileInfo,

[Parameter(DontShow)]
[hashtable]$CurrentSettings = $script:current
)

begin {
# Load the theme files on first invocation
if (-not $script:themeFilesLoaded) {
Import-XmlThemes
$script:themeFilesLoaded = $true
}
}

process {
$displayInfo = Resolve-Icon $FileInfo
if ($displayInfo.Icon) {
"$($displayInfo.Color)$($displayInfo.Icon) $($FileInfo.Name)$($displayInfo.Target)$($script:colorReset)"
"$($displayInfo.Color)$($displayInfo.Icon) $($CurrentSettings.RendorModeUnicode)$($FileInfo.Name)$($displayInfo.Target)$($script:colorReset)"
} else {
"$($displayInfo.Color)$($FileInfo.Name)$($displayInfo.Target)$($script:colorReset)"
"$($displayInfo.Color)$($CurrentSettings.RendorModeUnicode)$($FileInfo.Name)$($displayInfo.Target)$($script:colorReset)"
}
}
}
35 changes: 35 additions & 0 deletions Terminal-Icons/Public/Format-TerminalIconsDate.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function Format-TerminalIconsDate {
<#
.SYNOPSIS
Format a date according to the user's preferences.
.DESCRIPTION
Format a date according to the user's preferences.
.PARAMETER DateTime
The date to format.
.EXAMPLE
Format-TerminalIconsDate -DateTime (Get-Date)
.INPUTS
System.DateTime
.OUTPUTS
System.String
#>
[OutputType([String])]
[CmdletBinding()]
param(
[datetime]$DateTime,

[Parameter(DontShow)]
[hashtable]$CurrentSettings = $script:current
)

if ($CurrentSettings.DateTimeFormat -eq [DateTimeFormat]::HUMANIZE) {
$isUtc = $CurrentSettings.TimeZoneDisplay -eq [TimeZoneDisplay]::UTC
$DateTime.Humanize($isUtc)
} else {
if ($CurrentSettings.TimeZoneDisplay -eq [TimeZoneDisplay]::UTC) {
$DateTime.ToUniversalTime().ToString($CurrentSettings.DateTimeFormatString)
} else {
$DateTime.ToString($CurrentSettings.DateTimeFormatString)
}
}
}
Loading
Loading