Skip to content

Commit 5bdc346

Browse files
committed
more console updates
1 parent 4bc8bcf commit 5bdc346

6 files changed

Lines changed: 371 additions & 355 deletions

File tree

src/Classes/NoteStore.class.ps1

Lines changed: 126 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,126 @@ enum PSNoteKind {
33
Script
44
}
55

6-
enum PSNoteMenuItems{
7-
Main
8-
Catalogs
9-
Tags
10-
Favorites
11-
NoteList
12-
NoteActions
13-
Settings
14-
Help
15-
Quit
16-
Welcome
17-
AllCatalogs
18-
Preview
19-
Search
6+
class PSNoteMenuItem {
7+
8+
[string] $Name
9+
[string] $Key
10+
[char] $Alt
11+
[string] $Label
12+
13+
hidden PSNoteMenuItem(
14+
[string] $name,
15+
[string] $key,
16+
[char] $alt,
17+
[string] $label
18+
) {
19+
$this.Name = $name
20+
$this.Key = $key
21+
$this.Alt = $alt
22+
$this.Label = $label
23+
}
24+
25+
hidden PSNoteMenuItem(
26+
[string] $name,
27+
[string] $label
28+
) {
29+
$this.Name = $name
30+
$this.Key = $null
31+
$this.Alt = $null
32+
$this.Label = $label
33+
}
34+
35+
# --- Static "enum-like" instances ---
36+
static [PSNoteMenuItem] $Main = [PSNoteMenuItem]::new('Main', '^M', 'M', 'Main')
37+
static [PSNoteMenuItem] $Catalogs = [PSNoteMenuItem]::new('Catalogs', '^G', 'G', 'Catalogs')
38+
static [PSNoteMenuItem] $Tags = [PSNoteMenuItem]::new('Tags', '^T', 'T', 'Tags')
39+
static [PSNoteMenuItem] $Favorites = [PSNoteMenuItem]::new('Favorites', '^F', 'F', 'Favorites')
40+
static [PSNoteMenuItem] $Settings = [PSNoteMenuItem]::new('Settings', '^O', 'O', 'Settings')
41+
static [PSNoteMenuItem] $Search = [PSNoteMenuItem]::new('Search', '^W', 'W', 'Search')
42+
static [PSNoteMenuItem] $Help = [PSNoteMenuItem]::new('Help', '^H', 'H', 'Help')
43+
static [PSNoteMenuItem] $Back = [PSNoteMenuItem]::new('Back', '^B', 'B', 'Back')
44+
static [PSNoteMenuItem] $Quit = [PSNoteMenuItem]::new('Quit', '^Q', 'Q', 'Quit')
45+
static [PSNoteMenuItem] $Welcome = [PSNoteMenuItem]::new('Welcome', '^W', 'W', 'Welcome')
46+
static [PSNoteMenuItem] $AllCatalogs = [PSNoteMenuItem]::new('AllCatalogs', '^A', 'A', 'All')
47+
static [PSNoteMenuItem] $Preview = [PSNoteMenuItem]::new('Preview', '^P', 'P', 'Preview')
48+
static [PSNoteMenuItem] $NewNote = [PSNoteMenuItem]::new('NewNote', '^N', 'N', 'New')
49+
static [PSNoteMenuItem] $NoteList = [PSNoteMenuItem]::new('NoteList', 'NoteList')
50+
static [PSNoteMenuItem] $NoteActions = [PSNoteMenuItem]::new('NoteActions', 'NoteActions')
51+
static [PSNoteMenuItem] $Previous = [PSNoteMenuItem]::new('Previous', '^Y', 'Y', 'Previous')
52+
static [PSNoteMenuItem] $Next = [PSNoteMenuItem]::new('Next', '^U', 'U', 'Next')
53+
static [PSNoteMenuItem] $Copy = [PSNoteMenuItem]::new('Copy', '^C', 'C', 'Copy')
54+
static [PSNoteMenuItem] $Execute = [PSNoteMenuItem]::new('Execute', '^E', 'E', 'Execute')
55+
static [PSNoteMenuItem] $ToggleFav = [PSNoteMenuItem]::new('ToggleFav', '^T', 'T', 'Toggle Favorite')
56+
57+
static [PSNoteMenuItem[]] GetAll() {
58+
return @(
59+
[PSNoteMenuItem]::Main
60+
[PSNoteMenuItem]::Catalogs
61+
[PSNoteMenuItem]::Tags
62+
[PSNoteMenuItem]::Favorites
63+
[PSNoteMenuItem]::Settings
64+
[PSNoteMenuItem]::Search
65+
[PSNoteMenuItem]::Help
66+
[PSNoteMenuItem]::Back
67+
[PSNoteMenuItem]::Quit
68+
[PSNoteMenuItem]::Welcome
69+
[PSNoteMenuItem]::AllCatalogs
70+
[PSNoteMenuItem]::Preview
71+
[PSNoteMenuItem]::NewNote
72+
[PSNoteMenuItem]::Copy
73+
[PSNoteMenuItem]::Execute
74+
[PSNoteMenuItem]::ToggleFav
75+
)
76+
}
77+
78+
static [PSNoteMenuItem[]] GetMain() {
79+
return @(
80+
# Top Row
81+
[PSNoteMenuItem]::Main
82+
[PSNoteMenuItem]::NewNote
83+
[PSNoteMenuItem]::AllCatalogs
84+
[PSNoteMenuItem]::Search
85+
[PSNoteMenuItem]::Help
86+
# Bottom Row
87+
[PSNoteMenuItem]::Quit
88+
[PSNoteMenuItem]::Favorites
89+
[PSNoteMenuItem]::Catalogs
90+
[PSNoteMenuItem]::Back
91+
[PSNoteMenuItem]::Settings
92+
)
93+
}
94+
95+
static [PSNoteMenuItem[]] GetNoteActions() {
96+
return @(
97+
# Top Row
98+
[PSNoteMenuItem]::Copy
99+
[PSNoteMenuItem]::Execute
100+
[PSNoteMenuItem]::ToggleFav
101+
[PSNoteMenuItem]::Back
102+
)
103+
}
104+
105+
static [PSNoteMenuItem] FromKey([string]$key) {
106+
$from = [PSNoteMenuItem]::GetAll() |
107+
Where-Object { $_.Key -eq $key -and -not [string]::IsNullOrEmpty($_.Key) } | Select-Object -First 1
108+
if (-not $from) {
109+
$from = [PSNoteMenuItem]::GetAll() |
110+
Where-Object { $_.Alt -eq $key.TrimStart('^') -and -not [string]::IsNullOrEmpty($_.Alt) } | Select-Object -First 1
111+
}
112+
return $from
113+
}
114+
115+
static [PSNoteMenuItem] FromString([string]$name) {
116+
return [PSNoteMenuItem]::GetAll() |
117+
Where-Object { $_.Name -eq $name } | Select-Object -First 1
118+
}
119+
120+
[string] ToString() {
121+
return $this.Name
122+
}
20123
}
21124

125+
22126
# Create the PSNote class
23127
class PSNote {
24128
[string]$Note
@@ -602,7 +706,7 @@ class NoteConfigStore {
602706

603707
[string] $Path
604708
[int] $Version
605-
[PSNoteMenuItems] $Main
709+
[PSNoteMenuItem] $Main
606710
[bool] $ExitOnCopy
607711
[ConsoleColor] $ForegroundColor
608712
[ConsoleColor] $BackgroundColor
@@ -625,7 +729,7 @@ class NoteConfigStore {
625729

626730
[void] SetDefaults() {
627731
$this.Version = [NoteConfigStore]::CurrentVersion
628-
$this.Main = [PSNoteMenuItems]::Welcome
732+
$this.Main = [PSNoteMenuItem]::Welcome
629733
$this.ExitOnCopy = $true
630734
$this.ForegroundColor = [ConsoleColor]::Black
631735
$this.BackgroundColor = [ConsoleColor]::Gray
@@ -640,7 +744,7 @@ class NoteConfigStore {
640744

641745
$data = $json | ConvertFrom-Json -ErrorAction Stop
642746
if (-not [string]::IsNullOrWhiteSpace([string]$data.Main)) {
643-
$tryFg = [PSNoteMenuItems]::Welcome
747+
$tryFg = [PSNoteMenuItem]::Welcome
644748
if ([Enum]::TryParse([string]$data.Main, [ref]$tryFg)) {
645749
$this.Main = $tryFg
646750
}
@@ -683,8 +787,8 @@ class NoteConfigStore {
683787
class NoteConsoleState {
684788
static [int] $CurrentVersion = 1
685789

686-
[PSNoteMenuItems] $Mode
687-
[PSNoteMenuItems[]] $ReturnMode
790+
[PSNoteMenuItem] $Mode
791+
[PSNoteMenuItem[]] $ReturnMode
688792
[string] $ScopeLabel
689793
[System.Collections.Generic.List[PSNote]] $ScopeNotes
690794
[System.Collections.Generic.List[PSNote]] $LastList
@@ -702,14 +806,14 @@ class NoteConsoleState {
702806
$this.SetDefaults()
703807
$this.ScopeNotes = @($Store.Notes)
704808
$this.Settings = $Store.Config
705-
$tryMode = [PSNoteMenuItems]::Welcome
706-
if ([Enum]::TryParse([string]$Store.Config.Main, [ref]$tryMode)) {
809+
$tryMode = [PSNoteMenuItem]::FromString($Store.Config.Main)
810+
if ($tryMode) {
707811
$this.Mode = $tryMode
708812
}
709813
}
710814

711815
[void] SetDefaults() {
712-
$this.Mode = [PSNoteMenuItems]::Welcome
816+
$this.Mode = [PSNoteMenuItem]::Welcome
713817
$this.ReturnMode = @()
714818
$this.ScopeLabel = 'All'
715819
$this.ScopeNotes = @()

src/Private/Console/Invoke-PSNotesFooterCombo.ps1

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
function Write-PSNotesFooter {
2+
[CmdletBinding()]
3+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '')]
4+
param(
5+
[Parameter(Mandatory)]
6+
$State,
7+
[Parameter(Mandatory = $false)]
8+
$Menu = $null
9+
)
10+
11+
$Items = [PSNoteMenuItem]::GetMain()
12+
13+
[int] $Rows = 2
14+
$width = $Host.UI.RawUI.WindowSize.Width
15+
if ( $width -lt 78) { $width = 78 }
16+
$perRow = [Math]::Ceiling($Items.Count / $Rows)
17+
18+
$footerTop = (Get-PSNotesViewportRow -FromBottom ($Rows)) - 1 # top row of footer block
19+
20+
if($Menu) {
21+
[Console]::SetCursorPosition(0, $footerTop - 2)
22+
Write-Host $Menu -ForegroundColor Yellow
23+
}
24+
25+
# (optional) clear footer area first
26+
for ($i = 0; $i -lt $Rows-1; $i++) {
27+
[Console]::SetCursorPosition(0, $footerTop + $i)
28+
Write-Host (' ' * $width) -NoNewline
29+
}
30+
31+
for ($r = 0; $r -lt $Rows; $r++) {
32+
$rowItems = $Items | Select-Object -Skip ($r * $perRow) -First $perRow
33+
if (-not $rowItems -or $rowItems.Count -eq 0) { continue }
34+
35+
$colWidth = [Math]::Floor($width / $rowItems.Count)
36+
if ($colWidth -lt 12) { $colWidth = 12 }
37+
38+
[Console]::SetCursorPosition(0, $footerTop + $r)
39+
foreach ($it in $rowItems) {
40+
$key = [string]$it.Key
41+
$label = [string]$it.Label
42+
43+
# Build column, but render in two parts:
44+
# [ key ] label.... (all on footer bg, except key block)
45+
$colInnerMax = $colWidth
46+
47+
# Key chunk (ensure it fits)
48+
$keyChunk = " $key"
49+
if ($keyChunk.Length -gt 4) { $keyChunk = $keyChunk.Substring(0, 4) }
50+
$keyChunk = $keyChunk.PadRight(4)
51+
52+
# Remaining space for " label"
53+
$remaining = $colInnerMax - 4
54+
if ($remaining -lt 1) { $remaining = 1 }
55+
56+
$labelChunk = (" " + $label)
57+
if ($labelChunk.Length -gt $remaining) {
58+
$labelChunk = $labelChunk.Substring(0, $remaining)
59+
}
60+
$labelChunk = $labelChunk.PadRight($remaining)
61+
62+
# Render
63+
Write-Host $keyChunk -NoNewline -ForegroundColor $state.Settings.BackgroundColor -BackgroundColor $state.Settings.ForegroundColor
64+
Write-Host $labelChunk -NoNewline -ForegroundColor $state.Settings.ForegroundColor -BackgroundColor $state.Settings.BackgroundColor
65+
}
66+
67+
# Finish line, and make sure the whole line is footer background
68+
Write-Host "" -ForegroundColor $state.Settings.ForegroundColor -BackgroundColor $state.Settings.BackgroundColor
69+
}
70+
71+
# Reset colors after footer
72+
Write-Host "" -NoNewline
73+
}
74+
75+
function Invoke-PSNotesFooterCombo {
76+
<#
77+
Maps '^X' tokens to your footer actions.
78+
Returns $true if handled (meaning caller should continue loop), else $false.
79+
#>
80+
[CmdletBinding()]
81+
param(
82+
[Parameter(Mandatory)] $State,
83+
[Parameter(Mandatory)] [string] $Combo
84+
)
85+
86+
switch ($Combo) {
87+
'^M' { $State.Mode = $State.Settings.Main; return $true }
88+
'^A' { $State.Mode = [PSNoteMenuItem]::AllCatalogs; return $true }
89+
'^S' { $State.Mode = [PSNoteMenuItem]::Search; return $true }
90+
'^G' { $State.Mode = [PSNoteMenuItem]::Catalogs; return $true }
91+
'^T' { $State.Mode = [PSNoteMenuItem]::Tags; return $true }
92+
'^H' { $State.Mode = [PSNoteMenuItem]::Help; return $true }
93+
'^F' { $State.Mode = [PSNoteMenuItem]::Favorites; return $true }
94+
'^O' { $State.Mode = [PSNoteMenuItem]::Settings; return $true }
95+
'^Q' { $State.Mode = [PSNoteMenuItem]::Quit; return $true }
96+
'^C' { $State.Mode = [PSNoteMenuItem]::Quit; return $true }
97+
'^N' { $State.Mode = [PSNoteMenuItem]::NewNote; return $true }
98+
default { return $false }
99+
}
100+
}

0 commit comments

Comments
 (0)