} // end GenerateView() protected override void ApplyViewToInputObject() { string val = RenderScriptValue(InputObject, m_view.Script, false); // TODO: What to do if it spans more than a line? Just truncate? Issue a // warning? Add "..."? Also, consider that the view definition might have been // generated. if (null == val) { WriteObject(String.Empty); } else { //int idx = val.IndexOf( '\n' ); // TODO: Now we pass 'false' for dontGroupMultipleResults, so I think this // won't ever get hit unless we are formatting a string that contains a // newline. Perhaps in that case we should escape it. int idx = CaStringUtil.ApparentIndexOf(val, '\n'); if (idx < 0) { WriteObject(val); } else { WriteObject(CaStringUtil.Truncate(val, idx, false)); } } } // end ApplyViewToInputObject()
internal static string FormatSingleLineDirect(PSObject obj, ScriptBlock script, PsContext ctx) { string val; using (FormatAltSingleLineCommand cmd = new FormatAltSingleLineCommand()) { val = cmd.RenderScriptValue(obj, script, false, ctx); } // TODO: What to do if it spans more than a line? Just truncate? Issue a // warning? Add "..."? Also, consider that the view definition might have been // generated. if (null == val) { return(String.Empty); } else { int idx = CaStringUtil.ApparentIndexOf(val, '\n'); if (idx < 0) { return(val); } else { return(CaStringUtil.Truncate(val, idx)); } } } // end FormatSingleLineDirect
} // end Truncate() /// <summary> /// Creates a new ColorString object if truncation is necessary. /// </summary> public static ColorString Truncate(ColorString cs, int maxApparentWidth, bool useEllipsis) { if (cs.Length <= maxApparentWidth) { return(cs); } // Would it be better to go through all the elements? return(CaStringUtil.Truncate(cs.ToString(true), maxApparentWidth, useEllipsis)); } // end Truncate()
internal static string FormatSingleLineDirect(PSObject obj, ScriptBlock script) { #if DEBUG sm_renderScriptCallDepth++; if (sm_renderScriptCallDepth > 10) { // Helps to catch runaway rendering /before/ gobbling tons of memory System.Diagnostics.Debugger.Break(); // Maybe I should just throw? } #endif var ctxVars = new List <PSVariable> { new PSVariable("_", obj), new PSVariable("PSItem", obj) }; var results = script.InvokeWithContext(null, ctxVars); #if DEBUG sm_renderScriptCallDepth--; #endif string val = null; if (results?.Count > 0) { val = ObjectsToMarkedUpString(results, "{0}", // <-- IMPORTANT: this prevents infinite recursion via Format-AltSingleLine null, false, 4).ToString(); } // TODO: What to do if it spans more than a line? Just truncate? Issue a // warning? Add "..."? Also, consider that the view definition might have been // generated. if (null == val) { return(String.Empty); } else { int idx = CaStringUtil.ApparentIndexOf(val, '\n'); if (idx < 0) { return(val); } else { return(CaStringUtil.Truncate(val, idx)); } } } // end FormatSingleLineDirect
protected static string PadAndAlign(string s, int width, ColumnAlignment alignment, TrimLocation trimLocation) { Util.Assert(ColumnAlignment.Default != alignment); int len = CaStringUtil.Length(s); int pad = width - len; if (0 == pad) { return(s); } if (pad < 0) { // Oh dear... too big to fit. return(CaStringUtil.Truncate(s, width, true, trimLocation)); } switch (alignment) { case ColumnAlignment.Left: return(s + new String(' ', pad)); case ColumnAlignment.Center: int leftpad = pad / 2; int rightpad = pad - leftpad; return(new String(' ', leftpad) + s + new String(' ', rightpad)); case ColumnAlignment.Right: return(new String(' ', pad) + s); default: throw new ArgumentException(Util.Sprintf("Invalid ColumnAlignment value: {0}", alignment), "alignment"); } } // end PadAndAlign()
private static ColorString _SummarizeModuleList(bool loadedMods, List <object> modObjList, int maxWidth) { ColorString cs = new ColorString(); if (0 == modObjList.Count) { cs.AppendPushPopFg(ConsoleColor.DarkGray, "(0 modules)"); } else { cs.Append(Util.Sprintf("{0} modules: ", modObjList.Count)); for (int i = 0; i < Math.Min(modObjList.Count, 3); i++) { if (i > 0) { cs.Append(", "); } DbgModuleInfo dmi = (DbgModuleInfo)modObjList[i]; if (loadedMods) { cs.Append(DbgProvider.ColorizeModuleName(dmi.Name)); } else { cs.Append(dmi.ImageName); } } if (modObjList.Count > 3) { cs.Append(", ..."); } } return(CaStringUtil.Truncate(cs.ToString(DbgProvider.HostSupportsColor), maxWidth)); } // end _SummarizeModuleList()
internal static string FormatSingleLineDirect(PSObject obj, ScriptBlock script, bool allowMultipleLines) { #if DEBUG sm_renderScriptCallDepth++; if (sm_renderScriptCallDepth > 10) { // Helps to catch runaway rendering /before/ gobbling tons of memory System.Diagnostics.Debugger.Break(); // Maybe I should just throw? } #endif var ctxVars = new List <PSVariable> { new PSVariable("_", obj), new PSVariable("PSItem", obj) }; var results = script.InvokeWithContext(null, ctxVars); #if DEBUG sm_renderScriptCallDepth--; #endif string val = null; if (results?.Count > 0) { val = ObjectsToMarkedUpString(results, "{0}", // <-- IMPORTANT: this prevents infinite recursion via Format-AltSingleLine null, false, 4).ToString(); } // TODO: What to do if it spans more than a line? Just truncate? Issue a // warning? Add "..."? Also, consider that the view definition might have been // generated. if (null == val) { return(String.Empty); } // Q. Why might an alleged single-line view generate multiple lines? // // A. Could be buggy. Could be a generated view, and somebody's ToString() // generates multiple lines. In short: it's not necessarily "weird", or // unusual. // // Q. Why would we want to allow multiple lines? Doesn't the name of this // class / method say "format SINGLE LINE"? // // A. Sometimes multi-line views can not only be accommodated, they are // desirable, such as for compatibility with the built-in Format-List // command. if (allowMultipleLines) { return(val); } int idx = CaStringUtil.ApparentIndexOf(val, '\n'); if (idx < 0) { return(val); } return(CaStringUtil.Truncate(val, idx)); } // end FormatSingleLineDirect