mirror of
https://github.com/anjoy8/Blog.Core.git
synced 2024-09-20 23:48:27 +08:00
添加以表格方式输出控制台
This commit is contained in:
parent
10fd4fbf80
commit
c0b6a75c79
|
@ -64,7 +64,8 @@ namespace Blog.Core
|
|||
services.AddSwaggerSetup();
|
||||
services.AddJobSetup();
|
||||
services.AddHttpContextSetup();
|
||||
services.AddAppConfigSetup(Env);
|
||||
//services.AddAppConfigSetup(Env);
|
||||
services.AddAppTableConfigSetup(Env);//表格打印配置
|
||||
services.AddHttpApi();
|
||||
services.AddRedisInitMqSetup();
|
||||
|
||||
|
|
|
@ -1,15 +1,25 @@
|
|||
using System;
|
||||
|
||||
namespace Blog.Core.Common.Helper
|
||||
namespace Blog.Core.Common
|
||||
{
|
||||
public static class ConsoleHelper
|
||||
{
|
||||
private static readonly object _objLock = new();
|
||||
|
||||
/// <summary>
|
||||
/// 在控制台输出
|
||||
/// </summary>
|
||||
/// <param name="str">文本</param>
|
||||
/// <param name="color">前颜色</param>
|
||||
public static void WriteColorLine(string str, ConsoleColor color)
|
||||
{
|
||||
ConsoleColor currentForeColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = color;
|
||||
Console.WriteLine(str);
|
||||
Console.ForegroundColor = currentForeColor;
|
||||
lock (_objLock)
|
||||
{
|
||||
ConsoleColor currentForeColor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = color;
|
||||
Console.WriteLine(str);
|
||||
Console.ForegroundColor = currentForeColor;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -17,38 +27,27 @@ namespace Blog.Core.Common.Helper
|
|||
/// </summary>
|
||||
/// <param name="str">待打印的字符串</param>
|
||||
/// <param name="color">想要打印的颜色</param>
|
||||
public static void WriteErrorLine(this string str, ConsoleColor color = ConsoleColor.Red)
|
||||
{
|
||||
WriteColorLine(str, color);
|
||||
}
|
||||
public static void WriteErrorLine(this string str, ConsoleColor color = ConsoleColor.Red)=> WriteColorLine(str, color);
|
||||
|
||||
/// <summary>
|
||||
/// 打印警告信息
|
||||
/// </summary>
|
||||
/// <param name="str">待打印的字符串</param>
|
||||
/// <param name="color">想要打印的颜色</param>
|
||||
public static void WriteWarningLine(this string str, ConsoleColor color = ConsoleColor.Yellow)
|
||||
{
|
||||
WriteColorLine(str, color);
|
||||
}
|
||||
public static void WriteWarningLine(this string str, ConsoleColor color = ConsoleColor.Yellow)=> WriteColorLine(str, color);
|
||||
|
||||
/// <summary>
|
||||
/// 打印正常信息
|
||||
/// </summary>
|
||||
/// <param name="str">待打印的字符串</param>
|
||||
/// <param name="color">想要打印的颜色</param>
|
||||
public static void WriteInfoLine(this string str, ConsoleColor color = ConsoleColor.White)
|
||||
{
|
||||
WriteColorLine(str, color);
|
||||
}
|
||||
public static void WriteInfoLine(this string str, ConsoleColor color = ConsoleColor.White)=> WriteColorLine(str, color);
|
||||
|
||||
/// <summary>
|
||||
/// 打印成功的信息
|
||||
/// </summary>
|
||||
/// <param name="str">待打印的字符串</param>
|
||||
/// <param name="color">想要打印的颜色</param>
|
||||
public static void WriteSuccessLine(this string str, ConsoleColor color = ConsoleColor.Green)
|
||||
{
|
||||
WriteColorLine(str, color);
|
||||
}
|
||||
|
||||
public static void WriteSuccessLine(this string str, ConsoleColor color = ConsoleColor.Green)=> WriteColorLine(str, color);
|
||||
}
|
||||
}
|
39
Blog.Core.Common/Helper/Console/Table/ColumnShowFormat.cs
Normal file
39
Blog.Core.Common/Helper/Console/Table/ColumnShowFormat.cs
Normal file
|
@ -0,0 +1,39 @@
|
|||
namespace System
|
||||
{
|
||||
/// <summary>
|
||||
/// 列显示格式信息
|
||||
/// </summary>
|
||||
public class ColumnShowFormat
|
||||
{
|
||||
public ColumnShowFormat(int index, int strLength, Alignment alignment)
|
||||
{
|
||||
Index = index;
|
||||
StrLength = strLength;
|
||||
Alignment = alignment;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 索引,第几列数据
|
||||
/// </summary>
|
||||
public int Index { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 对其方式
|
||||
/// </summary>
|
||||
public Alignment Alignment { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 一列字符串长度
|
||||
/// </summary>
|
||||
public int StrLength { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 对其方式
|
||||
/// </summary>
|
||||
public enum Alignment
|
||||
{
|
||||
Left,
|
||||
Right
|
||||
}
|
||||
}
|
330
Blog.Core.Common/Helper/Console/Table/ConsoleTable.cs
Normal file
330
Blog.Core.Common/Helper/Console/Table/ConsoleTable.cs
Normal file
|
@ -0,0 +1,330 @@
|
|||
using Blog.Core.Common;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace System
|
||||
{
|
||||
public class ConsoleTable
|
||||
{
|
||||
#region 属性
|
||||
|
||||
/// <summary>
|
||||
/// 表格头部字符串
|
||||
/// </summary>
|
||||
public string TitleString { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 表格的列
|
||||
/// </summary>
|
||||
public IList<string> Columns
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_columns == null) _columns = new List<string>();
|
||||
return _columns;
|
||||
}
|
||||
set
|
||||
{
|
||||
_columns = value;
|
||||
_finalColumnWides = new List<int>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 行
|
||||
/// </summary>
|
||||
public List<string[]> Rows { get; set; } = new List<string[]>();
|
||||
|
||||
/// <summary>
|
||||
/// 列宽
|
||||
/// </summary>
|
||||
public List<int> ColumnWides { get; set; } = new List<int>();
|
||||
|
||||
/// <summary>
|
||||
/// 空白字符数量
|
||||
/// </summary>
|
||||
public int ColumnBlankNum { get; set; } = 4;
|
||||
|
||||
/// <summary>
|
||||
/// 对其方式
|
||||
/// </summary>
|
||||
public Alignment Alignment { get; set; } = Alignment.Left;
|
||||
|
||||
/// <summary>
|
||||
/// 是否显示行数
|
||||
/// </summary>
|
||||
public bool EnableCount { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// 表格显示样式
|
||||
/// 每次设置样子后就会重置 StyleInfo
|
||||
/// </summary>
|
||||
public TableStyle TableStyle
|
||||
{
|
||||
get
|
||||
{
|
||||
return _tableStyle;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (_tableStyle == value) return;
|
||||
_tableStyle = value;
|
||||
_formatInfo = null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion 属性
|
||||
|
||||
#region 私有信息
|
||||
private IList<string> _columns;
|
||||
private TableStyle _tableStyle;
|
||||
private StyleInfo _formatInfo;
|
||||
private List<ColumnShowFormat> _columnShowFormats = new List<ColumnShowFormat>();
|
||||
private List<int> _finalColumnWides = new List<int>();
|
||||
|
||||
/// <summary>
|
||||
/// 通过 Format 获得到表格显示样式
|
||||
/// </summary>
|
||||
private StyleInfo FormatInfo
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_formatInfo == null)
|
||||
_formatInfo = _tableStyle.GetFormatInfo();//得到样式信息
|
||||
return _formatInfo;
|
||||
}
|
||||
set
|
||||
{
|
||||
_formatInfo = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 每一列的宽度
|
||||
/// </summary>
|
||||
private List<int> FinalColumnWides
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_finalColumnWides is null || _finalColumnWides.Count < 1)
|
||||
{
|
||||
// 得到每一列最大的宽度
|
||||
List<int> _columnWides = Columns.GetColumnWides(Rows);
|
||||
// 替换用户输入长度
|
||||
ColumnWides ??= new List<int>();
|
||||
for (int i = 0; i < ColumnWides.Count; i++) _columnWides[i] = ColumnWides[i];
|
||||
_finalColumnWides = _columnWides;
|
||||
}
|
||||
return _finalColumnWides;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 每一列显示的基本信息
|
||||
/// </summary>
|
||||
private List<ColumnShowFormat> ColumnShowFormats
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_columnShowFormats.Count == 0)
|
||||
{
|
||||
for (int i = 0; i < Columns.Count; i++) _columnShowFormats.Add(new ColumnShowFormat(i, FinalColumnWides[i], Alignment));
|
||||
}
|
||||
return _columnShowFormats;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion 私有信息
|
||||
|
||||
#region 配置数据
|
||||
|
||||
/// <summary>
|
||||
/// 添加列
|
||||
/// </summary>
|
||||
/// <param name="columnName">列明</param>
|
||||
/// <param name="columnWide">列的宽</param>
|
||||
/// <returns></returns>
|
||||
public ConsoleTable AddColumn(string columnName, int columnWide = 0)
|
||||
{
|
||||
Columns.Add(columnName);
|
||||
columnWide = columnWide == 0 ? columnName.Length : columnWide;
|
||||
_finalColumnWides.Add(columnWide);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加行
|
||||
/// </summary>
|
||||
/// <param name="values">该行数据</param>
|
||||
/// <returns></returns>
|
||||
public ConsoleTable AddRow(params string[] values)
|
||||
{
|
||||
_ = values ?? throw new ArgumentNullException(nameof(values));
|
||||
|
||||
Rows.Add(values);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载 List 对象的数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="values"></param>
|
||||
/// <returns></returns>
|
||||
public static ConsoleTable From<T>(IEnumerable<T> values)
|
||||
{
|
||||
ConsoleTable table = new();
|
||||
|
||||
List<string> columns = GetColumns<T>().Where(c => !string.IsNullOrWhiteSpace(c)).ToList();
|
||||
columns.ForEach(c =>
|
||||
{
|
||||
table.AddColumn(c);
|
||||
});
|
||||
|
||||
values.ToList().ForEach(value =>
|
||||
{
|
||||
table.AddRow(columns.Select(c => GetColumnValue(value, c)).ToArray());
|
||||
});
|
||||
|
||||
return table;
|
||||
}
|
||||
|
||||
#endregion 配置数据
|
||||
|
||||
/// <summary>
|
||||
/// 获取表格字符串
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override string ToString()
|
||||
{
|
||||
StringBuilder builder = new();
|
||||
|
||||
builder.AppendLine(GetHeader());
|
||||
builder.AppendLine(GetExistData());
|
||||
builder.AppendLine(GetEnd());
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 绘制表格
|
||||
/// </summary>
|
||||
/// <param name="format">样式</param>
|
||||
/// <param name="color">title颜色</param>
|
||||
public void Writer(ConsoleColor color = ConsoleColor.White)
|
||||
{
|
||||
ConsoleHelper.WriteColorLine(GetHeader(), color);
|
||||
ConsoleHelper.WriteInfoLine(GetExistData());
|
||||
ConsoleHelper.WriteColorLine(GetEnd(), color);
|
||||
}
|
||||
|
||||
#region 帮助方法
|
||||
|
||||
/// <summary>
|
||||
/// 获取完成头
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetHeader()
|
||||
{
|
||||
// 创建顶部和底部分隔线
|
||||
string top_DownDividerdivider = FinalColumnWides.GetTopAndDwon(FormatInfo.AngleStr, ColumnBlankNum);
|
||||
// 创建分隔线
|
||||
string divider = FinalColumnWides.GetDivider(FormatInfo.AngleStr, ColumnBlankNum);
|
||||
// 获取标题字符串
|
||||
string tilte = FinalColumnWides.GetTitleStr(TitleString, ColumnBlankNum, FormatInfo.DelimiterStr);
|
||||
// 得到头部字符串
|
||||
string headers = ColumnShowFormats.FillFormatTostring(Columns.ToArray(), FormatInfo.DelimiterStr, ColumnBlankNum);
|
||||
|
||||
//绘制表格头
|
||||
StringBuilder top = new();
|
||||
if (FormatInfo.IsShowTop_Down_DataBorder) top.AppendLine(top_DownDividerdivider);
|
||||
if (!string.IsNullOrWhiteSpace(tilte))
|
||||
{
|
||||
top.AppendLine(tilte);
|
||||
top.AppendLine(divider);
|
||||
}
|
||||
top.AppendLine(headers);
|
||||
top.AppendLine(divider);
|
||||
return top.ToString().Trim();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取现有数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetExistData()
|
||||
{
|
||||
// 创建分隔线
|
||||
string divider = FinalColumnWides.GetDivider(FormatInfo.AngleStr, ColumnBlankNum);
|
||||
// 得到每行数据的字符串
|
||||
List<string> rowStrs = Rows.Select(row => ColumnShowFormats.FillFormatTostring(row, FormatInfo.DelimiterStr, ColumnBlankNum)).ToList();
|
||||
StringBuilder data = new();
|
||||
for (int i = 0; i < rowStrs.Count; i++)
|
||||
{
|
||||
if (FormatInfo.IsShowTop_Down_DataBorder && i != 0) data.AppendLine(divider);
|
||||
data.AppendLine(rowStrs[i]);
|
||||
}
|
||||
return data.ToString().Trim();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取新行数据
|
||||
/// </summary>
|
||||
/// <param name="row"></param>
|
||||
/// <returns></returns>
|
||||
public string GetNewRow(string[] row)
|
||||
{
|
||||
if (row is null) return "";
|
||||
|
||||
Rows.Add(row);
|
||||
//内容
|
||||
StringBuilder data = new();
|
||||
if (Rows.Count > 1) data.AppendLine(FinalColumnWides.GetDivider(FormatInfo.AngleStr, ColumnBlankNum));
|
||||
data.AppendLine(ColumnShowFormats.FillFormatTostring(row, FormatInfo.DelimiterStr, ColumnBlankNum));
|
||||
return data.ToString().Trim();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取底
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public string GetEnd()
|
||||
{
|
||||
StringBuilder down = new();
|
||||
if (FormatInfo.IsShowTop_Down_DataBorder) down.AppendLine(FinalColumnWides.GetTopAndDwon(FormatInfo.AngleStr, ColumnBlankNum));
|
||||
if (EnableCount) down.AppendLine($" Count: {Rows.Count}");
|
||||
return down.ToString().Trim();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取列名
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
private static IEnumerable<string> GetColumns<T>()
|
||||
{
|
||||
return typeof(T).GetProperties().Select(x => x.Name).ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取列值
|
||||
/// </summary>
|
||||
/// <typeparam name="T">类型</typeparam>
|
||||
/// <param name="obj">数据</param>
|
||||
/// <param name="column">列名</param>
|
||||
/// <returns></returns>
|
||||
private static string GetColumnValue<T>(T obj, string column)
|
||||
{
|
||||
if (obj == null) return null;
|
||||
|
||||
JObject o = obj as JObject ?? (JObject)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(obj));
|
||||
|
||||
return o.GetValue(column).ToString();
|
||||
}
|
||||
|
||||
#endregion 帮助方法
|
||||
}
|
||||
}
|
35
Blog.Core.Common/Helper/Console/Table/DrawTableInfo.cs
Normal file
35
Blog.Core.Common/Helper/Console/Table/DrawTableInfo.cs
Normal file
|
@ -0,0 +1,35 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace System
|
||||
{
|
||||
/// <summary>
|
||||
/// 绘制表格需要的信息
|
||||
/// </summary>
|
||||
public class DrawTableInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 顶部和底部字符串分隔线
|
||||
/// </summary>
|
||||
public string Top_DownDivider { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 分隔线
|
||||
/// </summary>
|
||||
public string Divider { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 标题
|
||||
/// </summary>
|
||||
public string Title { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 头部
|
||||
/// </summary>
|
||||
public string Header { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 数据
|
||||
/// </summary>
|
||||
public List<string> Data { get; set; }
|
||||
}
|
||||
}
|
151
Blog.Core.Common/Helper/Console/Table/TableExtension.cs
Normal file
151
Blog.Core.Common/Helper/Console/Table/TableExtension.cs
Normal file
|
@ -0,0 +1,151 @@
|
|||
using Blog.Core;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace System
|
||||
{
|
||||
public static class TableExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 按照现有数据计算每列最大宽度
|
||||
/// </summary>
|
||||
/// <param name="columns">列信息</param>
|
||||
/// <param name="rows">现有行数据</param>
|
||||
/// <returns>每一列显示宽度</returns>
|
||||
public static List<int> GetColumnWides(this IList<string> columns, IList<string[]> rows)
|
||||
{
|
||||
List<int> columnLengths = columns.Select((t, i) =>
|
||||
rows.Select(x => x[i])//得到所有行当前列的数据
|
||||
.Union(new[] { columns[i] })//连接当前列标题
|
||||
.Where(x => x != null)
|
||||
.Select(x => x.ObjToString().FullHalfLength())//得到该列每一行的字符串长度(计算中文占用两格)
|
||||
.Max())//到该列中长度最大的以列
|
||||
.ToList();
|
||||
return columnLengths;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将填充格式转成字符串
|
||||
/// 表头和数据行会用到
|
||||
/// </summary>
|
||||
/// <param name="format">一行的显示格式信息</param>
|
||||
/// <param name="objs">一行要显示的数据</param>
|
||||
/// <param name="delimiterStr">间隔符</param>
|
||||
/// <param name="columnBlankNum">每列留白数</param>
|
||||
/// <returns></returns>
|
||||
public static string FillFormatTostring(this List<ColumnShowFormat> format, string[] objs, string delimiterStr, int columnBlankNum)
|
||||
{
|
||||
string formatStr = string.Empty;
|
||||
format.ForEach(f =>
|
||||
{
|
||||
string ali = f.Alignment == Alignment.Right ? "" : "-";
|
||||
string val = objs[f.Index].ObjToString();
|
||||
if (val.Length > f.StrLength)
|
||||
{
|
||||
//val = val[0..f.StrLength];
|
||||
//val = val[0..(val.Length - val.GetChineseText().Length)];
|
||||
objs[f.Index] = "...";//标记超出长度
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(formatStr)) formatStr += $"{"".PadLeft(columnBlankNum, ' ')}";
|
||||
int alignmentStrLength = Math.Max(f.StrLength - objs[f.Index].ObjToString().GetChineseText().Length, 0);//对其填充空格数量
|
||||
formatStr += $"{delimiterStr}{"".PadLeft(columnBlankNum, ' ')}{{{f.Index},{ali}{alignmentStrLength}}}";
|
||||
});
|
||||
formatStr += $"{"".PadLeft(columnBlankNum, ' ')}{delimiterStr}";
|
||||
return string.Format(formatStr, objs);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取title 字符串
|
||||
/// </summary>
|
||||
/// <param name="columnWides">></param>
|
||||
/// <param name="titleStr">标题字符串信息</param>
|
||||
/// <param name="columnBlankNum">列两端留白数</param>
|
||||
/// <param name="delimiterStr">每列之间分割字符串</param>
|
||||
/// <returns></returns>
|
||||
public static string GetTitleStr(this List<int> columnWides, string titleStr, int columnBlankNum, string delimiterStr)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(titleStr)) return "";
|
||||
//一行的宽度
|
||||
int rowWide = columnWides.Sum() + columnWides.Count * 2 * columnBlankNum + columnWides.Count + 1;
|
||||
int blankNum = (rowWide - titleStr.FullHalfLength()) / 2 - 1;
|
||||
string tilte = $"{delimiterStr}{"".PadLeft(blankNum, ' ')}{titleStr}{"".PadLeft(blankNum, ' ')}{delimiterStr}";
|
||||
if (tilte.FullHalfLength() != rowWide) tilte = tilte.Replace($" {delimiterStr}", $" {delimiterStr}");
|
||||
return tilte;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取每行之间的分割行字符串
|
||||
/// </summary>
|
||||
/// <param name="columnWides">列宽信息</param>
|
||||
/// <param name="angleStr">每列之间分割字符串</param>
|
||||
/// <param name="columnBlankNum">列两端留白数</param>
|
||||
/// <returns></returns>
|
||||
public static string GetDivider(this List<int> columnWides, string angleStr, int columnBlankNum)
|
||||
{
|
||||
string divider = "";
|
||||
columnWides.ForEach(i =>
|
||||
{
|
||||
divider += $"{angleStr}{"".PadRight(i + columnBlankNum * 2, '-')}";
|
||||
});
|
||||
divider += angleStr;
|
||||
return divider;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取头部和底部字符串
|
||||
/// </summary>
|
||||
/// <param name="columnWides">列宽信息</param>
|
||||
/// <param name="angleStr">每列之间分割字符串</param>
|
||||
/// <param name="columnBlankNum">列两端留白数</param>
|
||||
/// <returns></returns>
|
||||
public static string GetTopAndDwon(this List<int> columnWides, string angleStr, int columnBlankNum)
|
||||
{
|
||||
string top_DownDividerdivider = "";
|
||||
columnWides.ForEach(i =>
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(top_DownDividerdivider)) top_DownDividerdivider += $"{angleStr}{"".PadRight(i + columnBlankNum * 2, '-')}";
|
||||
else top_DownDividerdivider += $"{"".PadRight(i + columnBlankNum * 2 + 1, '-')}";
|
||||
});
|
||||
top_DownDividerdivider += angleStr;
|
||||
return top_DownDividerdivider;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取表格显示样式
|
||||
/// </summary>
|
||||
/// <param name="format"></param>
|
||||
/// <returns></returns>
|
||||
public static StyleInfo GetFormatInfo(this TableStyle format)
|
||||
{
|
||||
return format switch
|
||||
{
|
||||
TableStyle.Default => new StyleInfo("|", true, "-"),
|
||||
TableStyle.MarkDown => new StyleInfo("|", false, "|"),
|
||||
TableStyle.Alternative => new StyleInfo("|", true, "+"),
|
||||
TableStyle.Minimal => new StyleInfo("", false, "-"),
|
||||
_ => new StyleInfo(),
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取文本长度,区分全角半角
|
||||
/// 全角算两个字符
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static int FullHalfLength(this string text)
|
||||
{
|
||||
return Regex.Replace(text, "[^\x00-\xff]", "**").Length;
|
||||
//可使用以下方法,不过要看在不同编码中字节数
|
||||
//return Encoding.Default.GetByteCount(text);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取中文文本
|
||||
/// </summary>
|
||||
/// <param name="text"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetChineseText(this string text) => Regex.Replace(text, "[\x00-\xff]", "");
|
||||
}
|
||||
}
|
57
Blog.Core.Common/Helper/Console/Table/TableStyle.cs
Normal file
57
Blog.Core.Common/Helper/Console/Table/TableStyle.cs
Normal file
|
@ -0,0 +1,57 @@
|
|||
namespace System
|
||||
{
|
||||
/// <summary>
|
||||
/// 表格显示样式
|
||||
/// </summary>
|
||||
public enum TableStyle
|
||||
{
|
||||
/// <summary>
|
||||
/// 默认格式的表格
|
||||
/// </summary>
|
||||
Default = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Markdwon格式的表格
|
||||
/// </summary>
|
||||
MarkDown = 1,
|
||||
|
||||
/// <summary>
|
||||
/// 交替格式的表格
|
||||
/// </summary>
|
||||
Alternative = 2,
|
||||
|
||||
/// <summary>
|
||||
/// 最简格式的表格
|
||||
/// </summary>
|
||||
Minimal = 3
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 表格显示样式信息
|
||||
/// 通过 Format 获取到的
|
||||
/// </summary>
|
||||
public class StyleInfo
|
||||
{
|
||||
public StyleInfo(string delimiterStr = "|", bool isShowTop_Down_DataBorder = true, string angleStr = "-")
|
||||
{
|
||||
DelimiterStr = delimiterStr;
|
||||
IsShowTop_Down_DataBorder = isShowTop_Down_DataBorder;
|
||||
AngleStr = angleStr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 每一列数据之间的间隔字符串
|
||||
/// </summary>
|
||||
public string DelimiterStr { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否显示顶部,底部,和每一行数据之间的横向边框
|
||||
/// </summary>
|
||||
public bool IsShowTop_Down_DataBorder { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 边角字符串
|
||||
/// </summary>
|
||||
public string AngleStr { get; set; }
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
using Autofac;
|
||||
using Blog.Core.Common.Extensions;
|
||||
using Blog.Core.Common.Helper;
|
||||
using Blog.Core.Common;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
using Blog.Core.Common.Helper;
|
||||
using Blog.Core.Common;
|
||||
using Blog.Core.EventBus.EventHandling;
|
||||
using Blog.Core.IServices;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using Blog.Core.Common.Helper;
|
||||
using Blog.Core.Common;
|
||||
using Blog.Core.Common.Helper;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Nacos.V2;
|
||||
using System;
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
using Blog.Core.Common;
|
||||
using Blog.Core.Common.Helper;
|
||||
using Blog.Core.Common.LogHelper;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Blog.Core.Extensions
|
||||
|
@ -213,5 +215,97 @@ namespace Blog.Core.Extensions
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
public static void AddAppTableConfigSetup(this IServiceCollection services, IHostEnvironment env)
|
||||
{
|
||||
if (services == null) throw new ArgumentNullException(nameof(services));
|
||||
|
||||
if (Appsettings.app(new string[] { "Startup", "AppConfigAlert", "Enabled" }).ObjToBool())
|
||||
{
|
||||
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||
Console.OutputEncoding = Encoding.GetEncoding("GB2312");
|
||||
}
|
||||
|
||||
#region 程序配置
|
||||
List<string[]> configInfos = new()
|
||||
{
|
||||
new string[] { "当前环境", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") },
|
||||
new string[] { "当前的授权方案", Permissions.IsUseIds4 ? "Ids4" : "JWT" },
|
||||
new string[] { "CORS跨域", Appsettings.app("Startup", "Cors", "EnableAllIPs") },
|
||||
new string[] { "RabbitMQ消息列队", Appsettings.app("RabbitMQ", "Enabled") },
|
||||
new string[] { "事件总线(必须开启消息列队)", Appsettings.app("EventBus", "Enabled") },
|
||||
new string[] { "redis消息队列", Appsettings.app("Startup", "RedisMq", "Enabled") },
|
||||
new string[] { "是否多库", Appsettings.app("MutiDBEnabled" ) },
|
||||
new string[] { "读写分离", Appsettings.app("CQRSEnabled") },
|
||||
};
|
||||
|
||||
new ConsoleTable()
|
||||
{
|
||||
TitleString = "Blog.Core 配置集",
|
||||
Columns = new string[] { "配置名称", "配置信息/是否启动" },
|
||||
Rows = configInfos,
|
||||
EnableCount = false,
|
||||
Alignment = Alignment.Left,
|
||||
ColumnBlankNum = 4,
|
||||
TableStyle = TableStyle.Alternative
|
||||
}.Writer(ConsoleColor.Blue);
|
||||
Console.WriteLine();
|
||||
#endregion 程序配置
|
||||
|
||||
#region AOP
|
||||
List<string[]> aopInfos = new()
|
||||
{
|
||||
new string[] { "Redis缓存AOP", Appsettings.app("AppSettings", "RedisCachingAOP", "Enabled") },
|
||||
new string[] { "内存缓存AOP", Appsettings.app("AppSettings", "MemoryCachingAOP", "Enabled") },
|
||||
new string[] { "服务日志AOP", Appsettings.app("AppSettings", "LogAOP", "Enabled" ) },
|
||||
new string[] { "事务AOP", Appsettings.app("AppSettings", "TranAOP", "Enabled" ) },
|
||||
new string[] { "Sql执行AOP", Appsettings.app("AppSettings", "SqlAOP", "OutToLogFile", "Enabled" ) },
|
||||
new string[] { "Sql执行AOP控制台输出", Appsettings.app("AppSettings", "SqlAOP", "OutToConsole", "Enabled" ) },
|
||||
};
|
||||
|
||||
new ConsoleTable
|
||||
{
|
||||
TitleString = "AOP",
|
||||
Columns = new string[] { "配置名称", "配置信息/是否启动" },
|
||||
Rows = aopInfos,
|
||||
EnableCount = false,
|
||||
Alignment = Alignment.Left,
|
||||
ColumnBlankNum = 7,
|
||||
TableStyle = TableStyle.Alternative
|
||||
}.Writer(ConsoleColor.Blue);
|
||||
Console.WriteLine();
|
||||
#endregion AOP
|
||||
|
||||
#region 中间件
|
||||
List<string[]> MiddlewareInfos = new()
|
||||
{
|
||||
new string[] { "请求纪录中间件", Appsettings.app("Middleware", "RecordAccessLogs", "Enabled") },
|
||||
new string[] { "IP记录中间件", Appsettings.app("Middleware", "IPLog", "Enabled" ) },
|
||||
new string[] { "请求响应日志中间件", Appsettings.app("Middleware", "RequestResponseLog", "Enabled" ) },
|
||||
new string[] { "SingnalR实时发送请求数据中间件", Appsettings.app("Middleware", "SignalR", "Enabled" ) },
|
||||
new string[] { "IP限流中间件", Appsettings.app("Middleware", "IpRateLimit", "Enabled") },
|
||||
new string[] { "性能分析中间件", Appsettings.app("Startup", "MiniProfiler", "Enabled") },
|
||||
new string[] { "Consul注册服务", Appsettings.app("Middleware", "Consul", "Enabled") },
|
||||
};
|
||||
|
||||
new ConsoleTable
|
||||
{
|
||||
TitleString = "中间件",
|
||||
Columns = new string[] { "配置名称", "配置信息/是否启动" },
|
||||
Rows = MiddlewareInfos,
|
||||
EnableCount = false,
|
||||
Alignment = Alignment.Left,
|
||||
ColumnBlankNum = 3,
|
||||
TableStyle = TableStyle.Alternative
|
||||
}.Writer(ConsoleColor.Blue);
|
||||
Console.WriteLine();
|
||||
#endregion 中间件
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
using Blog.Core.Common.Helper;
|
||||
using Blog.Core.Common;
|
||||
using Blog.Core.IServices;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
using Blog.Core.Common.Helper;
|
||||
using Blog.Core.Common;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
|
Loading…
Reference in New Issue
Block a user