1 // 2 // Copyright (c) 2010-2018 Antmicro 3 // Copyright (c) 2011-2015 Realtime Embedded 4 // 5 // This file is licensed under the MIT License. 6 // Full license text is available in 'licenses/MIT.txt'. 7 // 8 using System; 9 using System.Linq; 10 using System.Collections.Generic; 11 12 namespace Antmicro.Renode.Utilities 13 { 14 public class Table 15 { Table()16 public Table() 17 { 18 content = new List<string[]>(); 19 } 20 Table(List<string[]> content)21 private Table(List<string[]> content) 22 { 23 this.content = content; 24 } 25 AddRow(params string[] elements)26 public Table AddRow(params string[] elements) 27 { 28 content.Add(elements); 29 return this; 30 } 31 AddRows(IEnumerable<TEntry> source, params Func<TEntry, string>[] selectors)32 public Table AddRows<TEntry>(IEnumerable<TEntry> source, params Func<TEntry, string>[] selectors) 33 { 34 foreach(var element in source) 35 { 36 content.Add(selectors.Select(x => x(element)).ToArray()); 37 } 38 return this; 39 } 40 ToArray()41 public string[,] ToArray() 42 { 43 var width = content.Max(x => x.Length); 44 var height = content.Count; 45 var result = new string[height, width]; 46 for(var i = 0; i < height; i++) 47 { 48 for(var j = 0; j < width; j++) 49 { 50 result[i, j] = content[i].Length > j ? content[i][j] : string.Empty; 51 } 52 } 53 return result; 54 } 55 56 private readonly List<string[]> content; 57 } 58 } 59 60