This repository was archived by the owner on Jun 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathGridViewDataSource.cs
More file actions
83 lines (67 loc) · 2.42 KB
/
Copy pathGridViewDataSource.cs
File metadata and controls
83 lines (67 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.Generic;
using NStack;
using Terminal.Gui;
namespace OutGridView.Cmdlet
{
internal sealed class GridViewDataSource : IListDataSource
{
public List<GridViewRow> GridViewRowList { get; set; }
public int Count => GridViewRowList.Count;
public GridViewDataSource(List<GridViewRow> itemList)
{
GridViewRowList = itemList;
}
public int Length { get; }
public void Render(ListView container, ConsoleDriver driver, bool selected, int item, int col, int line, int width, int start)
{
container.Move(col, line);
RenderUstr(driver, GridViewRowList[item].DisplayString, col, line, width);
}
public bool IsMarked(int item) => GridViewRowList[item].IsMarked;
public void SetMark(int item, bool value)
{
var oldValue = GridViewRowList[item].IsMarked;
GridViewRowList[item].IsMarked = value;
var args = new RowMarkedEventArgs()
{
Row = GridViewRowList[item],
OldValue = oldValue
};
MarkChanged?.Invoke(this, args);
}
public sealed class RowMarkedEventArgs : EventArgs
{
public GridViewRow Row { get; set; }
public bool OldValue { get; set; }
}
public event EventHandler<RowMarkedEventArgs> MarkChanged;
public IList ToList()
{
return GridViewRowList;
}
// A slightly adapted method from gui.cs: https://github.com/migueldeicaza/gui.cs/blob/fc1faba7452ccbdf49028ac49f0c9f0f42bbae91/Terminal.Gui/Views/ListView.cs#L433-L461
private static void RenderUstr(ConsoleDriver driver, ustring ustr, int col, int line, int width)
{
int used = 0;
int index = 0;
while (index < ustr.Length)
{
(var rune, var size) = Utf8.DecodeRune(ustr, index, index - ustr.Length);
var count = Rune.ColumnWidth(rune);
if (used + count > width) break;
driver.AddRune(rune);
used += count;
index += size;
}
while (used < width)
{
driver.AddRune(' ');
used++;
}
}
}
}