-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 additions
and
77 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,35 +1,36 @@ | ||
package terminal | ||
|
||
import ( | ||
"image/color" | ||
"os" | ||
"syscall" | ||
"unsafe" | ||
) | ||
|
||
const defaultRatio float64 = 7.0 / 3.0 // The terminal's default cursor width/height ratio | ||
|
||
func terminalSize() (int, int, float64) { | ||
var size [4]uint16 | ||
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, | ||
uintptr(os.Stdout.Fd()), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&size)), | ||
0, 0, 0); err != 0 { | ||
panic(err) | ||
} | ||
rows, cols, width, height := size[0], size[1], size[2], size[3] | ||
|
||
whratio := defaultRatio | ||
if width > 0 && height > 0 { | ||
whratio = float64(height/rows) / float64(width/cols) | ||
} | ||
|
||
return int(cols), int(rows), whratio | ||
} | ||
|
||
func terminalColor(rgb color.RGBA) uint16 { | ||
r := (((rgb.R * 5) + 127) / 255) * 36 | ||
g := (((rgb.G * 5) + 127) / 255) * 6 | ||
b := ((rgb.B * 5) + 127) / 255 | ||
|
||
return uint16(r+g+b) + 16 + 1 // termbox default color offset | ||
} | ||
// | ||
//import ( | ||
// "image/color" | ||
// "os" | ||
// "syscall" | ||
// "unsafe" | ||
//) | ||
// | ||
//const defaultRatio float64 = 7.0 / 3.0 // The terminal's default cursor width/height ratio | ||
// | ||
//func terminalSize() (int, int, float64) { | ||
// var size [4]uint16 | ||
// if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, | ||
// uintptr(os.Stdout.Fd()), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&size)), | ||
// 0, 0, 0); err != 0 { | ||
// panic(err) | ||
// } | ||
// rows, cols, width, height := size[0], size[1], size[2], size[3] | ||
// | ||
// whratio := defaultRatio | ||
// if width > 0 && height > 0 { | ||
// whratio = float64(height/rows) / float64(width/cols) | ||
// } | ||
// | ||
// return int(cols), int(rows), whratio | ||
//} | ||
// | ||
//func terminalColor(rgb color.RGBA) uint16 { | ||
// r := (((rgb.R * 5) + 127) / 255) * 36 | ||
// g := (((rgb.G * 5) + 127) / 255) * 6 | ||
// b := ((rgb.B * 5) + 127) / 255 | ||
// | ||
// return uint16(r+g+b) + 16 + 1 // termbox default color offset | ||
//} |
This file contains 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 |
---|---|---|
@@ -1,40 +1,41 @@ | ||
package terminal | ||
|
||
import ( | ||
"fmt" | ||
"image/color" | ||
) | ||
|
||
var ( | ||
White = color.RGBA{R: 255, G: 255, B: 255, A: 255} | ||
Black = color.RGBA{A: 255} | ||
) | ||
|
||
// hexToRGBA convert hex string into color.RGBA | ||
func hexToRGBA(s string) color.RGBA { | ||
c := color.RGBA{ | ||
R: 0, | ||
G: 0, | ||
B: 0, | ||
A: 0xff, | ||
} | ||
|
||
var err error | ||
switch len(s) { | ||
case 7: | ||
_, err = fmt.Sscanf(s, "#%02x%02x%02x", &c.R, &c.G, &c.B) | ||
case 4: | ||
_, err = fmt.Sscanf(s, "#%1x%1x%1x", &c.R, &c.G, &c.B) | ||
// Double the hex digits: | ||
c.R *= 17 | ||
c.G *= 17 | ||
c.B *= 17 | ||
default: | ||
err = fmt.Errorf("invalid length, must be 7 or 4") | ||
} | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return c | ||
} | ||
// | ||
//import ( | ||
// "fmt" | ||
// "image/color" | ||
//) | ||
// | ||
//var ( | ||
// White = color.RGBA{R: 255, G: 255, B: 255, A: 255} | ||
// Black = color.RGBA{A: 255} | ||
//) | ||
// | ||
//// hexToRGBA convert hex string into color.RGBA | ||
//func hexToRGBA(s string) color.RGBA { | ||
// c := color.RGBA{ | ||
// R: 0, | ||
// G: 0, | ||
// B: 0, | ||
// A: 0xff, | ||
// } | ||
// | ||
// var err error | ||
// switch len(s) { | ||
// case 7: | ||
// _, err = fmt.Sscanf(s, "#%02x%02x%02x", &c.R, &c.G, &c.B) | ||
// case 4: | ||
// _, err = fmt.Sscanf(s, "#%1x%1x%1x", &c.R, &c.G, &c.B) | ||
// // Double the hex digits: | ||
// c.R *= 17 | ||
// c.G *= 17 | ||
// c.B *= 17 | ||
// default: | ||
// err = fmt.Errorf("invalid length, must be 7 or 4") | ||
// } | ||
// if err != nil { | ||
// panic(err) | ||
// } | ||
// | ||
// return c | ||
//} |
This file contains 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