Skip to content

Commit ef79329

Browse files
committed
Cleanup
1 parent a3de9fb commit ef79329

File tree

7 files changed

+218
-29
lines changed

7 files changed

+218
-29
lines changed

Source/Assembly/Entities/Entities.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Management.Automation;
34
using System.Net;
45
using System.Text;
6+
using CodeOwls.PowerShell.Paths.Processors;
57

68
namespace PoshCode.Pansies
79
{
@@ -60,7 +62,7 @@ public static string Decode(string value)
6062
var output = new StringBuilder(stop);
6163

6264
int end = 0, start = 0;
63-
while((start = value.IndexOf('&', end)) != -1)
65+
while ((start = value.IndexOf('&', end)) != -1)
6466
{
6567
// if it's at the end, we're done here
6668
if (start == value.Length - 1)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace PoshCode
2+
{
3+
// Two rules for IPSMetadataSerializable:
4+
// There should be a parameterless constructor
5+
// FromPsMetadata needs to handle the output of ToPsMetadata
6+
public interface IPsMetadataSerializable
7+
{
8+
string ToPsMetadata();
9+
void FromPsMetadata(string metadata);
10+
}
11+
}

Source/Assembly/Position.cs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
namespace PoshCode.Pansies
2+
{
3+
public class Position : IPsMetadataSerializable
4+
{
5+
public bool Absolute { get; set; }
6+
7+
public int? Line { get; set; }
8+
9+
public int? Column { get; set; }
10+
11+
public Position() { }
12+
13+
public Position(int? line, int? column, bool absolute = false)
14+
{
15+
Line = line;
16+
Column = column;
17+
Absolute = absolute;
18+
}
19+
20+
public Position(string metadata)
21+
{
22+
this.FromPsMetadata(metadata);
23+
}
24+
25+
public override string ToString()
26+
{
27+
if (Line is null && Column is null)
28+
{
29+
return string.Empty;
30+
}
31+
32+
33+
if (Absolute)
34+
{
35+
// For absolute positioning, we can save by doing both
36+
// But if one is null, use these other absolute positioning commands
37+
if (Line is null)
38+
{
39+
return "\e" + $"[{Column}G";
40+
}
41+
else if (Column is null)
42+
{
43+
return "\e" + $"[{Line}d";
44+
}
45+
else
46+
{
47+
return "\e" + $"[{Line};{Column}H";
48+
}
49+
}
50+
else
51+
{
52+
if (!(Column is null))
53+
{
54+
if (Column > 0)
55+
{
56+
return "\e" + $"[{Column}C";
57+
}
58+
else if (Column < 0)
59+
{
60+
return "\e" + $"[{-Column}D";
61+
}
62+
}
63+
if (!(Line is null))
64+
{
65+
if (Line > 0)
66+
{
67+
return "\e" + $"[{Line}B";
68+
}
69+
else if (Line < 0)
70+
{
71+
return "\e" + $"[{-Line}A";
72+
}
73+
}
74+
}
75+
return string.Empty;
76+
}
77+
78+
public string ToPsMetadata()
79+
{
80+
return $"{Line};{Column}{(Absolute ? ";" : string.Empty)}";
81+
}
82+
83+
public void FromPsMetadata(string metadata)
84+
{
85+
var data = metadata.Split(';');
86+
if (data.Length == 3)
87+
{
88+
Absolute = data[2] == "1";
89+
}
90+
if (data.Length >= 2)
91+
{
92+
Line = data[0].Length > 0 ? (int?)int.Parse(data[0], System.Globalization.NumberFormatInfo.InvariantInfo) : null;
93+
Column = data[1].Length > 0 ? (int?)int.Parse(data[1], System.Globalization.NumberFormatInfo.InvariantInfo) : null;
94+
}
95+
}
96+
}
97+
}

Source/Assembly/Provider/Entities/Grapheme.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
using CodeOwls.PowerShell.Paths;
22
using CodeOwls.PowerShell.Provider.PathNodeProcessors;
33
using CodeOwls.PowerShell.Provider.PathNodes;
4-
using PoshCode.Pansies.Palettes;
4+
using PoshCode.Pansies.Provider;
55
using System;
6-
using System.Collections;
76
using System.Collections.Generic;
87
using System.IO;
9-
using System.Linq;
108
using System.Management.Automation.Provider;
119
using System.Text;
12-
using PoshCode.Pansies;
1310

14-
namespace PoshCode.Pansies.Provider
11+
namespace PoshCode.Pansies
1512
{
1613
public class Grapheme : PathNodeBase, IGetItemContent //, ISetItemContent
1714
{
1815
private string name;
1916
public string Value { get; set; }
2017

18+
/// <summary>
19+
/// supplies the name for the item at the current path value
20+
/// </summary>
21+
public override string Name
22+
{
23+
get { return name; }
24+
}
2125

2226
public Grapheme(KeyValuePair<string, string> item)
2327
{
@@ -56,12 +60,14 @@ public object GetContentReaderDynamicParameters(IProviderContext providerContext
5660
throw new NotImplementedException();
5761
}
5862

59-
/// <summary>
60-
/// supplies the name for the item at the current path value
61-
/// </summary>
62-
public override string Name
63+
64+
public IEnumerable<int> ToUTF32()
6365
{
64-
get { return name; }
66+
return Value.ToUTF32();
67+
}
68+
public string ToPsEscapedString()
69+
{
70+
return Value.ToPsEscapedString();
6571
}
6672
}
6773
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Text;
4+
5+
namespace PoshCode.Pansies
6+
{
7+
public static class StringExtensions
8+
{
9+
10+
public static IEnumerable<int> ToUTF32(this string value)
11+
{
12+
for (var i = 0; i < value.Length; i++)
13+
{
14+
if (char.IsHighSurrogate(value[i]))
15+
{
16+
if (value.Length < (i + 2) || !char.IsLowSurrogate(value[i + 1]))
17+
{
18+
throw new InvalidDataException();
19+
}
20+
yield return char.ConvertToUtf32(value[i], value[++i]);
21+
}
22+
else
23+
{
24+
yield return value[i];
25+
}
26+
}
27+
}
28+
public static string ToPsEscapedString(this string value)
29+
{
30+
var result = new StringBuilder();
31+
32+
for (var i = 0; i < value.Length; i++)
33+
{
34+
if (char.IsHighSurrogate(value[i]))
35+
{
36+
if (value.Length < (i + 2) || !char.IsLowSurrogate(value[i + 1]))
37+
{
38+
throw new InvalidDataException();
39+
}
40+
result.AppendFormat("`u{{{0:x6}}}", char.ConvertToUtf32(value[i], value[++i]));
41+
}
42+
else
43+
{
44+
if (value[i] == '\e')
45+
{
46+
result.Append("`e");
47+
}
48+
else if (value[i] == '`')
49+
{
50+
result.Append("``");
51+
}
52+
else if (value[i] == '$')
53+
{
54+
result.Append("`$");
55+
}
56+
else if (value[i] == '"')
57+
{
58+
result.Append("`\"");
59+
}
60+
else if (((int)value[i]) < 31 || ((int)value[i]) > 126)
61+
{
62+
result.AppendFormat("`u{{{0:x4}}}", (int)value[i]);
63+
}
64+
else
65+
{
66+
result.Append(value[i]);
67+
}
68+
}
69+
}
70+
return result.ToString();
71+
}
72+
}
73+
}

Source/Assembly/Provider/PansiesProvider.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace PoshCode.Pansies.Provider
1212
{
1313
[CmdletProvider("Pansies", ProviderCapabilities.None)]
14-
public class EntityProvider : CodeOwls.PowerShell.Provider.Provider
14+
public class PansiesProvider : CodeOwls.PowerShell.Provider.Provider
1515
{
1616
/// <summary>
1717
/// a required P2F override
@@ -25,12 +25,12 @@ protected override IPathResolver PathResolver
2525

2626
protected override IPathResolver PathResolver2(string path)
2727
{
28-
var name = path.Split([System.IO.Path.DirectorySeparatorChar, System.IO.Path.PathSeparator], StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
28+
var name = path.Split([System.IO.Path.DirectorySeparatorChar, ':', System.IO.Path.AltDirectorySeparatorChar], StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
2929
var drive = (from driveInfo in ProviderInfo.Drives
30-
where StringComparer.OrdinalIgnoreCase.Equals(driveInfo.Root.Trim('\\'), name)
31-
select driveInfo).FirstOrDefault() as Drive;
30+
where StringComparer.OrdinalIgnoreCase.Equals(driveInfo.Root.Trim(System.IO.Path.DirectorySeparatorChar, ':', System.IO.Path.AltDirectorySeparatorChar), name)
31+
select driveInfo).FirstOrDefault() as Drive;
3232

33-
return drive.PathResolver ?? new PansiesResolver(() => new PansiesProviderRoot());
33+
return drive.PathResolver ?? new PathNodeResolver(() => new PansiesProviderRoot());
3434
}
3535
/// <summary>
3636
/// overridden to supply a default drive when the provider is loaded
@@ -40,35 +40,35 @@ protected override Collection<PSDriveInfo> InitializeDefaultDrives()
4040
var drives = new Collection<PSDriveInfo>
4141
{
4242
new PansiesDrive (
43-
new PSDriveInfo( "Fg", ProviderInfo, "ForegroundColors:" + System.IO.Path.DirectorySeparatorChar, "Foreground Colors", null, "Fg:" ),
44-
new PansiesResolver(() => new RgbColorProviderRoot(RgbColorMode.Foreground))
43+
new PSDriveInfo( "fg", ProviderInfo, "fg:" + System.IO.Path.DirectorySeparatorChar, "Foreground Colors", null, "Fg:" ),
44+
new PathNodeResolver(() => new RgbColorProviderRoot(RgbColorMode.Foreground))
4545
),
4646
new PansiesDrive(
47-
new PSDriveInfo( "Bg", ProviderInfo, "BackgroundColors:" + System.IO.Path.DirectorySeparatorChar, "Background Colors", null, "Bg:" ),
48-
new PansiesResolver(() => new RgbColorProviderRoot(RgbColorMode.Background))
47+
new PSDriveInfo( "bg", ProviderInfo, "bg:" + System.IO.Path.DirectorySeparatorChar, "Background Colors", null, "Bg:" ),
48+
new PathNodeResolver(() => new RgbColorProviderRoot(RgbColorMode.Background))
4949
),
5050
new PansiesDrive(
51-
new PSDriveInfo( "Esc", ProviderInfo, "EscapeSequences:" + System.IO.Path.DirectorySeparatorChar, "Escape Sequences", null, "Esc:" ),
52-
new PansiesResolver(() => Entities.EscapeSequences.ToDriveRoot("Esc"))
51+
new PSDriveInfo( "esc", ProviderInfo, "esc:" + System.IO.Path.DirectorySeparatorChar, "Escape Sequences", null, "Esc:" ),
52+
new PathNodeResolver(() => Entities.EscapeSequences.ToDriveRoot("esc"))
5353
),
5454
new PansiesDrive(
55-
new PSDriveInfo( "Extra", ProviderInfo, "Strings:" + System.IO.Path.DirectorySeparatorChar, "Named Extended Strings", null, "Extra:" ),
56-
new PansiesResolver(() => Entities.ExtendedCharacters.ToDriveRoot("Extra"))
55+
new PSDriveInfo( "extra", ProviderInfo, "extra:" + System.IO.Path.DirectorySeparatorChar, "Named Extended Strings", null, "Extra:" ),
56+
new PathNodeResolver(() => Entities.ExtendedCharacters.ToDriveRoot("extra"))
5757
),
5858
};
5959
if (Entities.EnableNerdFonts) {
6060
drives.Add(
6161
new PansiesDrive(
62-
new PSDriveInfo( "NF", ProviderInfo, "NerdFontSymbols:" + System.IO.Path.DirectorySeparatorChar, "NerdFont Symbols", null, "NF:" ),
63-
new PansiesResolver(() => Entities.NerdFontSymbols.ToDriveRoot("Extra"))
62+
new PSDriveInfo("nf", ProviderInfo, "nf:" + System.IO.Path.DirectorySeparatorChar, "NerdFont Symbols", null, "NF:"),
63+
new PathNodeResolver(() => Entities.NerdFontSymbols.ToDriveRoot("nf"))
6464
)
6565
);
6666
}
6767
if (Entities.EnableEmoji) {
6868
drives.Add(
6969
new PansiesDrive(
70-
new PSDriveInfo( "Emoji", ProviderInfo, "Emoji:" + System.IO.Path.DirectorySeparatorChar, "Emoji 16", null, "Emoji:" ),
71-
new PansiesResolver(() => Entities.Emoji.ToDriveRoot("Extra"))
70+
new PSDriveInfo("emoji", ProviderInfo, "emoji:" + System.IO.Path.DirectorySeparatorChar, "Emoji 16", null, "Emoji:"),
71+
new PathNodeResolver(() => Entities.Emoji.ToDriveRoot("emoji"))
7272
)
7373
);
7474
};

Source/Assembly/Provider/PansiesResolver.cs renamed to Source/Assembly/Provider/PathNodeResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace PoshCode.Pansies.Provider
1212
{
13-
class PansiesResolver(Func<IPathNode> RootFactory) : PathResolverBase
13+
class PathNodeResolver(Func<IPathNode> RootFactory) : PathResolverBase
1414
{
1515
private readonly Func<IPathNode> rootFactory = RootFactory;
1616

0 commit comments

Comments
 (0)