none
Fixed Width Font for Code RRS feed

  • Question

  • Visual Studio uses Fixed width font, so why can't these forums mimic what they will see. I just posted to the forum, my code with comments in the regex parse were no longer alinged and the final result shown in the comment did not line up due to the proportional font. GRRR Brent didn't you get new people to help?

    string fromFile =
    @"Name   5        8
     
    ID      30      31
     
    Type    40      45
    
    ICD     15       29
    ";
    
    string pattern = @"
    ^        # Start looking at the beginning of the line
    (?!\s)      # If white space at beginning, move to next line
    (?<Text>[^\d]+) # Valid text found extract it up to the first number
    (?<Start>\d+)  # Get the starting index.
    (?:\s+)     # Match but don't capture the space between
    (?<Stop>\d+)   # Get the stopping index
    ";
    
    var result = Regex.Matches( fromFile, pattern, RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline )
              .OfType<Match>()
              .Select( mt => new {
                        Text = mt.Groups["Text"].Value,
                        Begin = int.Parse(mt.Groups["Start"].Value),
                        End  = int.Parse(mt.Groups["Stop"].Value)
                        } 
                  )
              .OrderBy(rule => rule.Begin)
              .Select(rule => new 
                        {
                        Text = rule.Text.Substring(0, Math.Min(rule.End - rule.Begin, rule.Text.Length)),
                        Start = rule.Begin,
                        End  = rule.End,
                        })
    
              .Aggregate( new StringBuilder(),
                    ( sb, rule ) =>
                    {
                      var runningLength = sb.ToString().Count();
                      var padSpan = (rule.Start - 1);
    
                      var padding = Math.Abs(runningLength - padSpan);
    
                      return sb.Append(
                        rule.Text.PadLeft( padding + rule.Text.Length )
                        );
                    }
              )
              .ToString()        
              ;
    
    
        Console.WriteLine( "12345678901234567890123456789012345678901234567890" );
        Console.WriteLine( result );
    /* Output
    12345678901234567890123456789012345678901234567890
      Nam    ICD      I     Type     
    */
    
    
    


    William Wegerson (www.OmegaCoder.Com)
    Wednesday, July 13, 2011 7:30 PM

Answers

  • Yes, there is an indenting problem, I'll log a bug.

    If I go to http://social.technet.microsoft.com/Forums/en-US/tnsandbox/thread/04190e1d-ff10-4c77-a47a-778c1beb75cc and copy the 2nd post into notepad, the first comment line is indented as 8 spaces as expected. Then I copied and pasted into VS and it formatted the     ' This should be indented 8 spaces.  line only four indented right under the If line above it.  So even VS on a copy past adjusts the whitespace, reducing it by four or sticking the comment indented inline with the previous line starting point. 

    But you're right, we are dropping obvious spacing we should preserve. This is probably a bug in our colorization code we may be able to fix. I noticed in the sandbox thread your inserted code snippets aren't colorized, why is that, did you not chose a language, preview insert path to inserting it?


    Community Forums Program Manager
    Thursday, July 14, 2011 5:46 PM
    Answerer

All replies

  • I've been annoyed by this for some time. The "Insert Code Block" feature uses a <pre> (preformatted) tag to enclose code. Documentation indicates this should use a fixed width font and preserve white space, but I find it does neither in the forums. Some 2 months ago I stopped using the "Insert Code Feature" and instead use the "Edit HTML Source" button. Entering code in HTML is awful, but I coded a VBScript program to convert any text file into HTML, specifying a fixed width font and preserving all white space. The program I use is linked on this page:

    http://www.rlmueller.net/ConvertCodeToHTML.htm

    I always compose my code in a text file, run this script to create a *.htm file, then paste the contents in the "Edit HTML Source" dialog. However, I recently discovered a drawback. The reply will no longer be counted as a reply with code. I have lots of replies already with code, so I don't care much, but this is a drawback. I'm thinking of revising the VBScript program to add a short section in <pre> tags so the reply will be flagged as having code. I have many replies in the last two months with extensive code examples that are not counted toward the new "reply with code achievements".

     


    Richard Mueller - MVP Directory Services
    Wednesday, July 13, 2011 11:02 PM
    Moderator
  • I'll take a look and see if there is something we can do to improve this. We may have a flaw in our css or the code that marks up the code.
    Community Forums Program Manager
    Thursday, July 14, 2011 5:45 AM
    Answerer
  • Here is a sandbox thread where I experimented with code blocks and HTML:

    http://social.technet.microsoft.com/Forums/en-US/tnsandbox/thread/04190e1d-ff10-4c77-a47a-778c1beb75cc

    The code blocks get worse if you later edit the reply, as happened in the last message in the thread. Indenting gets further reduced.

     


    Richard Mueller - MVP Directory Services
    Thursday, July 14, 2011 10:45 AM
    Moderator
  • Yes, there is an indenting problem, I'll log a bug.

    If I go to http://social.technet.microsoft.com/Forums/en-US/tnsandbox/thread/04190e1d-ff10-4c77-a47a-778c1beb75cc and copy the 2nd post into notepad, the first comment line is indented as 8 spaces as expected. Then I copied and pasted into VS and it formatted the     ' This should be indented 8 spaces.  line only four indented right under the If line above it.  So even VS on a copy past adjusts the whitespace, reducing it by four or sticking the comment indented inline with the previous line starting point. 

    But you're right, we are dropping obvious spacing we should preserve. This is probably a bug in our colorization code we may be able to fix. I noticed in the sandbox thread your inserted code snippets aren't colorized, why is that, did you not chose a language, preview insert path to inserting it?


    Community Forums Program Manager
    Thursday, July 14, 2011 5:46 PM
    Answerer
  • I wondered how other people did that. I never selected a language before. Nice.

     


    Richard Mueller - MVP Directory Services
    Thursday, July 14, 2011 9:32 PM
    Moderator
  • Yeah, but the spacing is still a bug...we'll look into that.
    Community Forums Program Manager
    Friday, July 15, 2011 1:12 AM
    Answerer
  • Yeah, but the spacing is still a bug...we'll look into that.
    Community Forums Program Manager

    9 years since last  post and is putting text in that need a fixed width font s this still a problem ?

    I tried to put some text it is not code (output text from a console window) but still requires fix width font so columns align correctly how about an entry in language type "log" that would do this? 

    I tried insert html button and using PRE tags but something is overriding the normal fixed width font implied by PRE tags. Other than using some text-to-html generator is the a way to do this that quick?

    I am testing with  Chrome and Firefox browsers, so is a compatibility issue with older browsers.

    Below is insert code example  - I see the text is not aligned as it is proportional font

    Required OS version : B1
    Image version       : B2
    Subsystem version   : B3

    Below is PRE tag example

    Required OS version : B1
    Image version       : B2
    Subsystem version   : B3
    

    Before I submit post it looks aligned but final result does not match
    PRE tags are inheriting style from this css link - perhaps you could drop the override or explain why it is forcing proportional font.


    • Edited by Scott_R_ Thursday, April 9, 2020 11:57 AM
    Thursday, April 9, 2020 11:41 AM