• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MorganKryze / ConsoleAppVisuals / 8114215569

01 Mar 2024 04:29PM UTC coverage: 85.767% (-9.3%) from 95.093%
8114215569

push

github

MorganKryze
🤖 moved to publish

865 of 1072 branches covered (80.69%)

Branch coverage included in aggregate %.

1708 of 1928 relevant lines covered (88.59%)

273.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.62
/src/ConsoleAppVisuals/elements/static_elements/inspectors/InteractiveList.cs
1
/*
2
    GNU GPL License 2024 MorganKryze(Yann Vidamment)
3
    For full license information, please visit: https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/LICENSE
4
*/
5
namespace ConsoleAppVisuals.Elements;
6

7
/// <summary>
8
/// This class is used to display a InteractiveList of all the elements in the window.
9
/// </summary>
10
public class InteractiveList : Element
11
{
12
    #region Fields: title, headers, lines, display array, rounded corners
13
    private List<List<string>> _lines;
14
    private string[] _displayArray;
15
    private bool _roundedCorners;
16
    private Placement _placement;
17
    #endregion
18

19
    #region Properties: get headers, get lines
20

21
    /// <summary>
22
    /// This property returns the title of the InteractiveList.
23
    /// </summary>
24
    public static string Title => "Interactive Element types available";
39✔
25

26
    /// <summary>
27
    /// This property returns the headers of the dashboard.
28
    /// </summary>
29
    public static List<string> Headers => new() { "Id", "Type", "Project" };
1,641✔
30
    private string GetCorners => _roundedCorners ? "╭╮╰╯" : "┌┐└┘";
234✔
31

32
    /// <summary>
33
    /// This property wether the corners of the InteractiveList are rounded.
34
    /// </summary>
35
    public bool RoundedCorners => _roundedCorners;
6✔
36

37
    /// <summary>
38
    /// This property returns the lines of the InteractiveList.
39
    /// </summary>
40
    public List<List<string>> Lines => _lines;
3✔
41

42
    /// <summary>
43
    /// This property returns the title of the InteractiveList.
44
    /// </summary>
45
    public override Placement Placement => _placement;
9✔
46

47
    /// <summary>
48
    /// This property returns the line to display the InteractiveList on.
49
    /// </summary>
50

51
    /// <summary>
52
    /// This property returns the height of the InteractiveList.
53
    /// </summary>
54
    public override int Height => _displayArray.Length;
3✔
55

56
    /// <summary>
57
    /// This property returns the width of the InteractiveList.
58
    /// </summary>
59
    public override int Width => _displayArray.Max(x => x.Length);
42✔
60

61
    /// <summary>
62
    /// This property returns the number of lines in the InteractiveList.
63
    /// </summary>
64
    public int Count => _lines.Count;
3✔
65

66
    #endregion
67

68
    #region Constructor
69
    /// <summary>
70
    /// This constructor creates a new instance of the WindowElementsInteractiveList class.
71
    /// </summary>
72
    /// <param name="placement">The placement of the InteractiveList.</param>
73
    /// <param name="roundedCorners">If true, the corners of the InteractiveList will be rounded.</param>
74
    public InteractiveList(Placement placement = Placement.TopCenter, bool roundedCorners = false)
33✔
75
    {
76
        _lines = UpdateLines();
33✔
77
        _placement = placement;
33✔
78
        _roundedCorners = roundedCorners;
33✔
79
        _displayArray = Array.Empty<string>();
33✔
80
        BuildDisplay();
33✔
81
    }
33✔
82
    #endregion
83

84
    #region Methods
85
    /// <summary>
86
    /// Toggles the rounded corners of the element.
87
    /// </summary>
88
    /// <remarks>Refer to the example project to understand how to implement it available at https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/ </remarks>
89
    public void SetRoundedCorners(bool rounded = true)
90
    {
91
        _roundedCorners = rounded;
6✔
92
        BuildDisplay();
6✔
93
    }
6✔
94

95
    /// <summary>
96
    /// This method updates the placement of the InteractiveList.
97
    /// </summary>
98
    /// <param name="placement">The new placement of the InteractiveList.</param>
99
    /// <remarks>
100
    /// For more information, refer to the following resources:
101
    /// <list type="bullet">
102
    /// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
103
    /// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
104
    /// </list>
105
    /// </remarks>
106
    public void UpdatePlacement(Placement placement)
107
    {
108
        _placement = placement;
×
109
        BuildDisplay();
×
110
    }
×
111

112
    private static List<List<string>> UpdateLines()
113
    {
114
        var elements = new List<List<string>>();
33✔
115
        var types = new List<Type>();
33✔
116
        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
4,686✔
117
        {
118
            if (
2,310✔
119
                assembly.FullName != null
2,310✔
120
                && !assembly.FullName.StartsWith("mscorlib")
2,310✔
121
                && !assembly.FullName.StartsWith("System")
2,310✔
122
                && !assembly.FullName.StartsWith("Microsoft")
2,310✔
123
            )
2,310✔
124
            {
125
                types.AddRange(
231✔
126
                    assembly
231✔
127
                        .GetTypes()
231✔
128
                        .Where(t =>
231✔
129
                            t.BaseType != null
34,584✔
130
                            && t.BaseType.IsGenericType
34,584✔
131
                            && t.BaseType.GetGenericTypeDefinition() == typeof(InteractiveElement<>)
34,584✔
132
                        )
231✔
133
                );
231✔
134
            }
135
        }
136
        var id = 0;
33✔
137
        foreach (var type in types)
528✔
138
        {
139
            elements.Add(
231!
140
                new List<string> { $"{id}", type.Name, type.Assembly.GetName().Name ?? "Unknown" }
231✔
141
            );
231✔
142
            id += 1;
231✔
143
        }
144
        return elements;
33✔
145
    }
146

147
    private void BuildDisplay()
148
    {
149
        var stringList = new List<string>();
39✔
150
        var localMax = new int[Headers.Count];
39✔
151
        for (int i = 0; i < Headers.Count; i++)
312✔
152
        {
153
            if (Headers[i]?.Length > localMax[i])
117!
154
            {
155
                localMax[i] = Headers[i]?.Length ?? 0;
117!
156
            }
157
        }
158

159
        for (int i = 0; i < _lines.Count; i++)
624✔
160
        {
161
            for (int j = 0; j < _lines[i].Count; j++)
2,184✔
162
            {
163
                if (_lines[i][j]?.ToString()?.Length > localMax[j])
819!
164
                {
165
                    localMax[j] = _lines[i][j]?.ToString()?.Length ?? 0;
78!
166
                }
167
            }
168
        }
169

170
        StringBuilder headerBuilder = new("│ ");
39✔
171
        for (int i = 0; i < Headers.Count; i++)
312✔
172
        {
173
            headerBuilder.Append(Headers[i]?.PadRight(localMax[i]) ?? "");
117!
174
            if (i != Headers.Count - 1)
117✔
175
            {
176
                headerBuilder.Append(" │ ");
78✔
177
            }
178
            else
179
            {
180
                headerBuilder.Append(" │");
39✔
181
            }
182
        }
183
        stringList.Add(headerBuilder.ToString());
39✔
184

185
        StringBuilder upperBorderBuilder = new(GetCorners[0].ToString());
39✔
186
        for (int i = 0; i < Headers.Count; i++)
312✔
187
        {
188
            upperBorderBuilder.Append(new string('─', localMax[i] + 2));
117✔
189
            upperBorderBuilder.Append((i != Headers.Count - 1) ? "┬" : GetCorners[1].ToString());
117✔
190
        }
191
        stringList.Insert(0, upperBorderBuilder.ToString());
39✔
192

193
        StringBuilder intermediateBorderBuilder = new("├");
39✔
194
        for (int i = 0; i < Headers.Count; i++)
312✔
195
        {
196
            intermediateBorderBuilder.Append(new string('─', localMax[i] + 2));
117✔
197
            intermediateBorderBuilder.Append((i != Headers.Count - 1) ? "┼" : "┤");
117✔
198
        }
199
        stringList.Add(intermediateBorderBuilder.ToString());
39✔
200

201
        for (int i = 0; i < _lines.Count; i++)
624✔
202
        {
203
            StringBuilder lineBuilder = new("│ ");
273✔
204
            for (int j = 0; j < _lines[i].Count; j++)
2,184✔
205
            {
206
                lineBuilder.Append(_lines[i][j]?.ToString()?.PadRight(localMax[j]) ?? "");
819!
207
                if (j != _lines[i].Count - 1)
819✔
208
                {
209
                    lineBuilder.Append(" │ ");
546✔
210
                }
211
                else
212
                {
213
                    lineBuilder.Append(" │");
273✔
214
                }
215
            }
216
            stringList.Add(lineBuilder.ToString());
273✔
217
        }
218

219
        StringBuilder lowerBorderBuilder = new(GetCorners[2].ToString());
39✔
220
        for (int i = 0; i < Headers.Count; i++)
312✔
221
        {
222
            lowerBorderBuilder.Append(new string('─', localMax[i] + 2));
117✔
223
            lowerBorderBuilder.Append((i != Headers.Count - 1) ? "┴" : GetCorners[3].ToString());
117✔
224
        }
225
        stringList.Add(lowerBorderBuilder.ToString());
39✔
226

227
        _displayArray = stringList.ToArray();
39✔
228
        BuildTitle();
39✔
229
    }
39✔
230

231
    private void BuildTitle()
232
    {
233
        var len = _displayArray![0].Length;
39✔
234
        var title = Title.ResizeString(len - 4);
39✔
235
        title = $"│ {title} │";
39✔
236
        var upperBorderBuilder = new StringBuilder(GetCorners[0].ToString());
39✔
237
        upperBorderBuilder.Append(new string('─', len - 2));
39✔
238
        upperBorderBuilder.Append(GetCorners[1].ToString());
39✔
239
        var display = _displayArray.ToList();
39✔
240
        display[0] = display[0]
39✔
241
            .Remove(0, 1)
39✔
242
            .Insert(0, "├")
39✔
243
            .Remove(display[1].Length - 1, 1)
39✔
244
            .Insert(display[1].Length - 1, "┤");
39✔
245
        display.Insert(0, title);
39✔
246
        display.Insert(0, upperBorderBuilder.ToString());
39✔
247
        _displayArray = display.ToArray();
39✔
248
    }
39✔
249

250
    /// <summary>
251
    /// This method displays the InteractiveList.
252
    /// </summary>
253
    [Visual]
254
    protected override void RenderElementActions()
255
    {
256
        _lines = UpdateLines();
257
        BuildDisplay();
258
        string[] array = new string[_displayArray!.Length];
259
        for (int j = 0; j < _displayArray.Length; j++)
260
        {
261
            array[j] = _displayArray[j];
262
            Core.WritePositionedString(array[j], _placement.ToTextAlignment(), false, Line + j);
263
        }
264
    }
265
    #endregion
266
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc