NQualityOfLife.Utils.Common.FormatAsString - Niilo007/RimWorld-NQoL GitHub Wiki
- AddCustomFormatterForType
- IFormatMode
- FormatMode
- DisplayMode
- DisplayModeFlags
- Utils.LogParamExtensions.OrNull
- Utils.LogParamExtensions.AsParam
/// <summary>Format the <paramref name="list"/> as a <see cref="string"/> using the provided <see cref="FormatMode"/> and <see cref="DisplayModeFlags"/></summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="formatMode">Controls how the <paramref name="list"/> is formatted, such as vertical or horizontal</param>
/// <param name="displayMode"></param>
/// <param name="specialFormatter">Format the items (<typeparamref name="T"/>) in the <paramref name="list"/> using this <see cref="Func{T, TResult}"/> instead</param>
/// <param name="specialSuffixMaker"></param>
/// <returns>A <see cref="string"/> representation of the <paramref name="list"/> and the contained items (<typeparamref name="T"/>)</returns>
/// <remarks>The <paramref name="specialFormatter"/> overrides default and custom formatters<para>Custom formatters for unsupported types (<typeparamref name="T"/>) can be added using <see cref="AddCustomFormatterForType{T}(Func{object?, DisplayModeFlags, string})"/></para></remarks>
public static string FormatAsString<T>(this IList<T>? list, FormatMode formatMode, DisplayModeFlags displayMode = DisplayModeFlags.Default, Func<T?, string>? specialFormatter = null, Func<T?, string>? specialSuffixMaker = null)
{
}
/// <summary>Format the <paramref name="dictionary"/> as a <see cref="string"/> using the provided <see cref="FormatMode"/> and <see cref="DisplayModeFlags"/></summary>
/// <typeparam name="K"></typeparam>
/// <typeparam name="V"></typeparam>
/// <param name="dictionary"></param>
/// <param name="formatMode"></param>
/// <param name="displayMode"></param>
/// <returns></returns>
/// <inheritdoc cref="FormatAsString{T}(IList{T}?, FormatMode, DisplayModeFlags, Func{T, string}?, Func{T, string}?)"/>
public static string FormatAsString<K, V>(this IDictionary<K, V>? dictionary, FormatMode formatMode, DisplayModeFlags displayMode = DisplayModeFlags.Default)
{
}
/// <inheritdoc cref="FormatAsString{T}(IList{T}?, FormatMode, DisplayModeFlags, Func{T, string}?, Func{T, string}?)"/>
[Eager]
public static string FormatAsString<T>(this IEnumerable<T>? values, FormatMode formatMode, DisplayModeFlags displayMode = DisplayModeFlags.Default, Func<T?, string>? specialFormatter = null, Func<T?, string>? specialSuffix = null)
{
}
Formats the given list into a string according to the provided parameters.
If the output format for the given type T
is not useful, you can add a custom formatter using AddCustomFormatterForType
//[HarmonyPatch(typeof(RimWorld.SurgeryOutcomeEffectDef), nameof(RimWorld.SurgeryOutcomeEffectDef.GetOutcome), MethodType.Normal)]
//internal class SurgerySuccessInfo_Patch_1 : HarmonyPatch
//{
// public static string report_Quality;
// [HarmonyPrefix]
// [JetBrains.Annotations.UsedImplicitly]
// internal static void Prefix(RecipeDef recipe, Pawn surgeon, Pawn patient, List<Thing> ingredients, BodyPartRecord part, Bill bill)
// {
// try
// {
// float quality = recipe.surgeryOutcomeEffect.GetQuality(recipe, surgeon, patient, ingredients, part, bill);
string MakeSpecialSuffix(SurgeryOutcomeComp comp)
{
if (!comp.Affects(recipe, surgeon, patient, part)) { return string.Concat(": ", "NA".Translate()); }
float affect = 1f;
comp.AffectQuality(recipe, surgeon, patient, ingredients, part, bill, ref affect);
float delta = affect - 1f;
if (delta < 0)
{
return string.Concat($": {Utils.Log.Colors.Color_False_Tag}-</color>", (Math.Abs(delta)).RatioToColoredPercentage(true, alwaysGoodOrBadAbove: -0.01f));
}
else
{
return string.Concat($": {Utils.Log.Colors.Color_True_Tag}+</color>", (Math.Abs(delta)).RatioToColoredPercentage(false, alwaysGoodOrBadAbove: -0.01f));
}
}
string SpecialFormatter(SurgeryOutcomeComp comp)
{
string stringAfterDot = comp.OrNull().SubstringAfter('.');
return stringAfterDot.OrNull(null)?.TranslateOr(stringAfterDot.SubstringAfter('_').OrNull(stringAfterDot) ?? null).OrNull(comp);
}
report_Quality = string.Concat(
$"\n", "NQoL_Surgery_Quality".Translate(), $"'{quality.RatioToColoredPercentage()}'\n", "NQoL_Surgery_Quality_Factors".Translate(),
$"\n{recipe.surgeryOutcomeEffect.comps.FormatAsString(
Common.FormatMode.VerticalWithLines,
specialFormatter: (comp) => SpecialFormatter(comp),
specialSuffixMaker: (comp) => MakeSpecialSuffix(comp)
)}"
);
// }
// catch (Exception e)
// {
// Utils.Log.Error(e, $"({recipe.AsParam()}, {surgeon.AsParam(nameof(surgeon))}, {patient.AsParam(nameof(patient))}, {ingredients.AsParam()}, //{part.AsParam()}, {bill.AsParam()})");
// report_Quality = e.ExceptionInfo();
// }
// }
//}