This repository was archived by the owner on Feb 2, 2023. It is now read-only.
  
  
  - 
                Notifications
    You must be signed in to change notification settings 
- Fork 2.2k
Ascii art visual debugging for ASLayoutSpecs #751
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * Copyright (c) 2014-present, Facebook, Inc. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. An additional grant | ||
| * of patent rights can be found in the PATENTS file in the same directory. | ||
| * | ||
| */ | ||
|  | ||
| #import <Foundation/Foundation.h> | ||
|  | ||
| @protocol ASLayoutableAsciiArtProtocol <NSObject> | ||
| /** | ||
| * Returns an ascii-art representation of this object and its children. | ||
| * For example, an ASInsetSpec may return something like this: | ||
| * | ||
| * --ASInsetLayoutSpec-- | ||
| * | ASTextNode | | ||
| * --------------------- | ||
| */ | ||
| - (NSString *)asciiArtString; | ||
|  | ||
| /** | ||
| * returns the name of this object that will display in the ascii art. Usually this can | ||
| * simply be NSStringFromClass([self class]). | ||
| */ | ||
| - (NSString *)asciiArtName; | ||
|  | ||
| @end | ||
|  | ||
| /** | ||
| * A that takes a parent and its children and renders as ascii art box. | ||
| */ | ||
| @interface ASAsciiArtBoxCreator : NSObject | ||
|  | ||
| /** | ||
| * Renders an ascii art box with the children aligned horizontally | ||
| * Example: | ||
| * ------------ASStackLayoutSpec----------- | ||
| * | ASTextNode ASTextNode ASTextNode | | ||
| * ---------------------------------------- | ||
| */ | ||
| + (NSString *)horizontalBoxStringForChildren:(NSArray *)children parent:(NSString *)parent; | ||
|  | ||
| /** | ||
| * Renders an ascii art box with the children aligned vertically. | ||
| * Example: | ||
| * --ASStackLayoutSpec-- | ||
| * | ASTextNode | | ||
| * | ASTextNode | | ||
| * | ASTextNode | | ||
| * --------------------- | ||
| */ | ||
| + (NSString *)verticalBoxStringForChildren:(NSArray *)children parent:(NSString *)parent; | ||
|  | ||
| @end | ||
|  | ||
|  | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| /* | ||
| * Copyright (c) 2014-present, Facebook, Inc. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. An additional grant | ||
| * of patent rights can be found in the PATENTS file in the same directory. | ||
| * | ||
| */ | ||
|  | ||
| @import UIKit; | ||
| #import "ASAsciiArtBoxCreator.h" | ||
|  | ||
| static const NSUInteger kDebugBoxPadding = 2; | ||
|  | ||
| typedef NS_ENUM(NSUInteger, PIDebugBoxPaddingLocation) | ||
| { | ||
| PIDebugBoxPaddingLocationFront, | ||
| PIDebugBoxPaddingLocationEnd, | ||
| PIDebugBoxPaddingLocationBoth | ||
| }; | ||
|  | ||
| @interface NSString(PIDebugBox) | ||
|  | ||
| @end | ||
|  | ||
| @implementation NSString(PIDebugBox) | ||
|  | ||
| + (instancetype)debugbox_stringWithString:(NSString *)stringToRepeat repeatedCount:(NSUInteger)repeatCount | ||
| { | ||
| NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[stringToRepeat length] * repeatCount]; | ||
| for (NSUInteger index = 0; index < repeatCount; index++) { | ||
| [string appendString:stringToRepeat]; | ||
| } | ||
| return [string copy]; | ||
| } | ||
|  | ||
| - (NSString *)debugbox_stringByAddingPadding:(NSString *)padding count:(NSUInteger)count location:(PIDebugBoxPaddingLocation)location | ||
| { | ||
| NSString *paddingString = [NSString debugbox_stringWithString:padding repeatedCount:count]; | ||
| switch (location) { | ||
| case PIDebugBoxPaddingLocationFront: | ||
| return [NSString stringWithFormat:@"%@%@", paddingString, self]; | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just because it's an easy optimization & there can be large layouts to print from device debugging... consider using -stringByAppendingString:, as it's surprisingly much faster than the format initializer. | ||
| case PIDebugBoxPaddingLocationEnd: | ||
| return [NSString stringWithFormat:@"%@%@", self, paddingString]; | ||
| case PIDebugBoxPaddingLocationBoth: | ||
| return [NSString stringWithFormat:@"%@%@%@", paddingString, self, paddingString]; | ||
| } | ||
| return [self copy]; | ||
| } | ||
|  | ||
| @end | ||
|  | ||
| @implementation ASAsciiArtBoxCreator | ||
|  | ||
| + (NSString *)horizontalBoxStringForChildren:(NSArray *)children parent:(NSString *)parent | ||
| { | ||
| if ([children count] == 0) { | ||
| return parent; | ||
| } | ||
|  | ||
| NSMutableArray *childrenLines = [NSMutableArray array]; | ||
|  | ||
| // split the children into lines | ||
| NSUInteger lineCountPerChild = 0; | ||
| for (NSString *child in children) { | ||
| NSArray *lines = [child componentsSeparatedByString:@"\n"]; | ||
| lineCountPerChild = MAX(lineCountPerChild, [lines count]); | ||
| } | ||
|  | ||
| for (NSString *child in children) { | ||
| NSMutableArray *lines = [[child componentsSeparatedByString:@"\n"] mutableCopy]; | ||
| NSUInteger topPadding = ceilf((CGFloat)(lineCountPerChild - [lines count])/2.0); | ||
| NSUInteger bottomPadding = (lineCountPerChild - [lines count])/2.0; | ||
| NSUInteger lineLength = [lines[0] length]; | ||
|  | ||
| for (NSUInteger index = 0; index < topPadding; index++) { | ||
| [lines insertObject:[NSString debugbox_stringWithString:@" " repeatedCount:lineLength] atIndex:0]; | ||
| } | ||
| for (NSUInteger index = 0; index < bottomPadding; index++) { | ||
| [lines addObject:[NSString debugbox_stringWithString:@" " repeatedCount:lineLength]]; | ||
| } | ||
| [childrenLines addObject:lines]; | ||
| } | ||
|  | ||
| NSMutableArray *concatenatedLines = [NSMutableArray array]; | ||
| NSString *padding = [NSString debugbox_stringWithString:@" " repeatedCount:kDebugBoxPadding]; | ||
| for (NSUInteger index = 0; index < lineCountPerChild; index++) { | ||
| NSMutableString *line = [[NSMutableString alloc] init]; | ||
| [line appendFormat:@"|%@",padding]; | ||
| for (NSArray *childLines in childrenLines) { | ||
| [line appendFormat:@"%@%@", childLines[index], padding]; | ||
| } | ||
| [line appendString:@"|"]; | ||
| [concatenatedLines addObject:line]; | ||
| } | ||
|  | ||
| // surround the lines in a box | ||
| NSUInteger totalLineLength = [concatenatedLines[0] length]; | ||
| if (totalLineLength < [parent length]) { | ||
| NSUInteger difference = [parent length] + (2 * kDebugBoxPadding) - totalLineLength; | ||
| NSUInteger leftPadding = ceilf((CGFloat)difference/2.0); | ||
| NSUInteger rightPadding = difference/2; | ||
|  | ||
| NSString *leftString = [@"|" debugbox_stringByAddingPadding:@" " count:leftPadding location:PIDebugBoxPaddingLocationEnd]; | ||
| NSString *rightString = [@"|" debugbox_stringByAddingPadding:@" " count:rightPadding location:PIDebugBoxPaddingLocationFront]; | ||
|  | ||
| NSMutableArray *paddedLines = [NSMutableArray array]; | ||
| for (NSString *line in concatenatedLines) { | ||
| NSString *paddedLine = [line stringByReplacingOccurrencesOfString:@"|" withString:leftString options:NSCaseInsensitiveSearch range:NSMakeRange(0, 1)]; | ||
| paddedLine = [paddedLine stringByReplacingOccurrencesOfString:@"|" withString:rightString options:NSCaseInsensitiveSearch range:NSMakeRange([paddedLine length] - 1, 1)]; | ||
| [paddedLines addObject:paddedLine]; | ||
| } | ||
| concatenatedLines = paddedLines; | ||
| totalLineLength += difference; | ||
| } | ||
| concatenatedLines = [self appendTopAndBottomToBoxString:concatenatedLines parent:parent]; | ||
| return [concatenatedLines componentsJoinedByString:@"\n"]; | ||
|  | ||
| } | ||
|  | ||
| + (NSString *)verticalBoxStringForChildren:(NSArray *)children parent:(NSString *)parent | ||
| { | ||
| if ([children count] == 0) { | ||
| return parent; | ||
| } | ||
|  | ||
| NSMutableArray *childrenLines = [NSMutableArray array]; | ||
|  | ||
| NSUInteger maxChildLength = 0; | ||
| for (NSString *child in children) { | ||
| NSArray *lines = [child componentsSeparatedByString:@"\n"]; | ||
| maxChildLength = MAX(maxChildLength, [lines[0] length]); | ||
| } | ||
|  | ||
| NSUInteger rightPadding = 0; | ||
| NSUInteger leftPadding = 0; | ||
|  | ||
| if (maxChildLength < [parent length]) { | ||
| NSUInteger difference = [parent length] + (2 * kDebugBoxPadding) - maxChildLength; | ||
| leftPadding = ceilf((CGFloat)difference/2.0); | ||
| rightPadding = difference/2; | ||
| } | ||
|  | ||
| NSString *rightPaddingString = [NSString debugbox_stringWithString:@" " repeatedCount:rightPadding + kDebugBoxPadding]; | ||
| NSString *leftPaddingString = [NSString debugbox_stringWithString:@" " repeatedCount:leftPadding + kDebugBoxPadding]; | ||
|  | ||
| for (NSString *child in children) { | ||
| NSMutableArray *lines = [[child componentsSeparatedByString:@"\n"] mutableCopy]; | ||
|  | ||
| NSUInteger leftLinePadding = ceilf((CGFloat)(maxChildLength - [lines[0] length])/2.0); | ||
| NSUInteger rightLinePadding = (maxChildLength - [lines[0] length])/2.0; | ||
|  | ||
| for (NSString *line in lines) { | ||
| NSString *rightLinePaddingString = [NSString debugbox_stringWithString:@" " repeatedCount:rightLinePadding]; | ||
| rightLinePaddingString = [NSString stringWithFormat:@"%@%@|", rightLinePaddingString, rightPaddingString]; | ||
|  | ||
| NSString *leftLinePaddingString = [NSString debugbox_stringWithString:@" " repeatedCount:leftLinePadding]; | ||
| leftLinePaddingString = [NSString stringWithFormat:@"|%@%@", leftLinePaddingString, leftPaddingString]; | ||
|  | ||
| NSString *paddingLine = [NSString stringWithFormat:@"%@%@%@", leftLinePaddingString, line, rightLinePaddingString]; | ||
| [childrenLines addObject:paddingLine]; | ||
| } | ||
| } | ||
|  | ||
| childrenLines = [self appendTopAndBottomToBoxString:childrenLines parent:parent]; | ||
| return [childrenLines componentsJoinedByString:@"\n"]; | ||
| } | ||
|  | ||
| + (NSMutableArray *)appendTopAndBottomToBoxString:(NSMutableArray *)boxStrings parent:(NSString *)parent | ||
| { | ||
| NSUInteger totalLineLength = [boxStrings[0] length]; | ||
| [boxStrings addObject:[NSString debugbox_stringWithString:@"-" repeatedCount:totalLineLength]]; | ||
|  | ||
| NSUInteger leftPadding = ceilf(((CGFloat)(totalLineLength - [parent length]))/2.0); | ||
| NSUInteger rightPadding = (totalLineLength - [parent length])/2; | ||
|  | ||
| NSString *topLine = [parent debugbox_stringByAddingPadding:@"-" count:leftPadding location:PIDebugBoxPaddingLocationFront]; | ||
| topLine = [topLine debugbox_stringByAddingPadding:@"-" count:rightPadding location:PIDebugBoxPaddingLocationEnd]; | ||
| [boxStrings insertObject:topLine atIndex:0]; | ||
|  | ||
| return boxStrings; | ||
| } | ||
|  | ||
| @end | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to make sure I understand — this only prints out one box with the node's name in it? Or does it somehow print the hierarchy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should just print the node's name. If someone wanted to make ascii art of the node hierarchy they'd pass in self.subnodes as the children.