github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/tboxc/print_stuff.go (about)

     1  package tboxc
     2  
     3  // Terminal Box Comfort
     4  
     5  import "github.com/pbberlin/termbox-go"
     6  import "fmt"
     7  
     8  type Rect struct {
     9  	X1, Y1, X2, Y2 int
    10  }
    11  
    12  func PriRuneAt(x, y int, r rune) {
    13  	termbox.SetCell(x, y, r, fgc, brushBG)
    14  }
    15  
    16  func PriLineAt(x, y int, format string, args ...interface{}) {
    17  	s := fmt.Sprintf(format, args...)
    18  	for _, rne := range s {
    19  		termbox.SetCell(x, y, rne, fgc, brushBG)
    20  		x++
    21  	}
    22  }
    23  
    24  func PriColoredLineAt(x, y int, fg termbox.Attribute, format string,
    25  	args ...interface{}) {
    26  	s := fmt.Sprintf(format, args...)
    27  	for _, rne := range s {
    28  		termbox.SetCell(x, y, rne, fg, brushBG)
    29  		x++
    30  	}
    31  }
    32  
    33  // ultimately: throw away direct PriParAt
    34  func PriToRect(r Rect, format string, args ...interface{}) {
    35  	PriParAt(r.X1, r.Y1, r.X2-r.X1, r.Y2-r.Y1, fgc, format, args...)
    36  }
    37  
    38  // priParAt - printParagraphAt prints a formatted string at given coords.
    39  // It starts a new line, each time param width is surpassed.
    40  // Forthcoming lines are indented.
    41  // priParAt() blanks out previous printings to the extent of width x height.
    42  // Previous prints, that were overflowing height remain.
    43  func PriParAt(x, y, width, height int, fg termbox.Attribute, format string,
    44  	args ...interface{}) {
    45  
    46  	for i := x; i <= x+width; i++ {
    47  		for j := y; j <= y+height; j++ {
    48  			// termbox.SetCell(i, j, ' ', fg, termbox.ColorGreen)
    49  			termbox.SetCell(i, j, ' ', fg, brushBG)
    50  		}
    51  	}
    52  
    53  	xInit := x + 1
    54  	indent := 1
    55  	s := fmt.Sprintf(format, args...)
    56  	for _, rne := range s {
    57  		if rne != '\n' {
    58  			termbox.SetCell(x, y, rne, fg, brushBG)
    59  		}
    60  		x++
    61  		if x > xInit+width-1 || rne == '\n' {
    62  			y++
    63  			x = xInit + indent - 1
    64  		}
    65  	}
    66  
    67  }
    68  
    69  // i.e. demoPrintCombinedEdges(x1+1, 2)
    70  func demoPrintCombinedEdges(x, y int) {
    71  	PriRuneAt(x+0, y, 0x251C+0*8) // vert, branch right
    72  	PriRuneAt(x+1, y, 0x251C+1*8) // vert, branch left
    73  	PriRuneAt(x+2, y, 0x251C+2*8) // horz, branch down
    74  	PriRuneAt(x+3, y, 0x251C+3*8) // horz, branch top
    75  }
    76  
    77  func PainHorzLine(x, y1, y2 int) {
    78  	for i := y1; i <= y2; i++ {
    79  		PriRuneAt(i, x, 0x2500)
    80  	}
    81  }
    82  
    83  func PainFatVertLine(y, x1, x2 int) {
    84  	for i := x1; i <= x2; i++ {
    85  		PriRuneAt(y, i, 0x2588)
    86  	}
    87  }
    88  
    89  // blank with current brush
    90  func BlankOut(r Rect) {
    91  	for i := r.X1; i <= r.X2; i++ {
    92  		for j := r.Y1; j <= r.Y2; j++ {
    93  			PriRuneAt(i, j, rune(32))
    94  		}
    95  	}
    96  }
    97  
    98  // painRect blanks out the area.
    99  // painRect returns the *inner* rect coords
   100  func PainRect(x1, y1, x2, y2, attachedTo int) (xi1, yi1, xi2, yi2 int) {
   101  
   102  	BlankOut(Rect{x1, y1, x2, y2})
   103  
   104  	// edges
   105  	PriRuneAt(x1, y1, 0x250C+0*4) // left top
   106  	PriRuneAt(x2, y1, 0x250C+1*4) // right top
   107  	PriRuneAt(x1, y2, 0x250C+2*4) // bottom left
   108  	PriRuneAt(x2, y2, 0x250C+3*4) // br
   109  
   110  	switch attachedTo {
   111  	case 1: // attach to left
   112  		PriRuneAt(x1, y1, 0x251C+2*8)
   113  		PriRuneAt(x1, y2, 0x251C+3*8)
   114  	case 2: // attach to top
   115  		PriRuneAt(x1, y1, 0x251C+0*8)
   116  		PriRuneAt(x2, y1, 0x251C+1*8)
   117  	}
   118  
   119  	// horizontal lines (inner, because edges are set)
   120  	for i := x1 + 1; i < x2; i++ {
   121  		PriRuneAt(i, y1, 0x2500)
   122  		PriRuneAt(i, y2, 0x2500)
   123  	}
   124  
   125  	// vertical lines (inner, because edges are set)
   126  	for i := y1 + 1; i < y2; i++ {
   127  		PriRuneAt(x1, i, 0x2502)
   128  		PriRuneAt(x2, i, 0x2502)
   129  	}
   130  
   131  	return x1 + 1, y1 + 1, x2 - 1, y2 - 1
   132  
   133  }