github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/cmd/ethereum/repl/console_colors_windows.go (about)

     1  // Copyright (c) 2013-2014, Jeffrey Wilcke. All rights reserved.
     2  //
     3  // This library is free software; you can redistribute it and/or
     4  // modify it under the terms of the GNU General Public
     5  // License as published by the Free Software Foundation; either
     6  // version 2.1 of the License, or (at your option) any later version.
     7  //
     8  // This library is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    11  // General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU General Public License
    14  // along with this library; if not, write to the Free Software
    15  // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    16  // MA 02110-1301  USA
    17  
    18  /* Inspired by https://github.com/xuyu/logging/blob/master/colorful_win.go */
    19  
    20  package ethrepl
    21  
    22  import (
    23  	"syscall"
    24  	"unsafe"
    25  )
    26  
    27  type color uint16
    28  
    29  const (
    30  	green  = color(0x0002)
    31  	red    = color(0x0004)
    32  	yellow = color(0x000E)
    33  )
    34  
    35  const (
    36  	mask = uint16(yellow | green | red)
    37  )
    38  
    39  var (
    40  	kernel32                       = syscall.NewLazyDLL("kernel32.dll")
    41  	procGetStdHandle               = kernel32.NewProc("GetStdHandle")
    42  	procSetConsoleTextAttribute    = kernel32.NewProc("SetConsoleTextAttribute")
    43  	procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
    44  	hStdout                        uintptr
    45  	initScreenInfo                 *consoleScreenBufferInfo
    46  )
    47  
    48  func setConsoleTextAttribute(hConsoleOutput uintptr, wAttributes uint16) bool {
    49  	ret, _, _ := procSetConsoleTextAttribute.Call(hConsoleOutput, uintptr(wAttributes))
    50  	return ret != 0
    51  }
    52  
    53  type coord struct {
    54  	X, Y int16
    55  }
    56  
    57  type smallRect struct {
    58  	Left, Top, Right, Bottom int16
    59  }
    60  
    61  type consoleScreenBufferInfo struct {
    62  	DwSize              coord
    63  	DwCursorPosition    coord
    64  	WAttributes         uint16
    65  	SrWindow            smallRect
    66  	DwMaximumWindowSize coord
    67  }
    68  
    69  func getConsoleScreenBufferInfo(hConsoleOutput uintptr) *consoleScreenBufferInfo {
    70  	var csbi consoleScreenBufferInfo
    71  	ret, _, _ := procGetConsoleScreenBufferInfo.Call(hConsoleOutput, uintptr(unsafe.Pointer(&csbi)))
    72  	if ret == 0 {
    73  		return nil
    74  	}
    75  	return &csbi
    76  }
    77  
    78  const (
    79  	stdOutputHandle = uint32(-11 & 0xFFFFFFFF)
    80  )
    81  
    82  func init() {
    83  	hStdout, _, _ = procGetStdHandle.Call(uintptr(stdOutputHandle))
    84  	initScreenInfo = getConsoleScreenBufferInfo(hStdout)
    85  }
    86  
    87  func resetColorful() {
    88  	if initScreenInfo == nil {
    89  		return
    90  	}
    91  	setConsoleTextAttribute(hStdout, initScreenInfo.WAttributes)
    92  }
    93  
    94  func changeColor(c color) {
    95  	attr := uint16(0) & ^mask | uint16(c)
    96  	setConsoleTextAttribute(hStdout, attr)
    97  }