package console import ( "errors" "fmt" "image" "image/color" "golang.org/x/term" "git.maze.io/go/harmon/config" "git.maze.io/go/harmon/display" "git.maze.io/go/harmon/internal/pixel" ) func init() { display.Register("console", New) } type Display struct { p *textImage w, h int } func New(config *config.Display) (display.Display, error) { if !term.IsTerminal(0) { return nil, errors.New("console: not a terminal") } w, h, err := term.GetSize(0) if err != nil { return nil, err } return &Display{ p: newTextImage(image.Rect(0, 0, w, h<<1)), w: w, h: h, }, nil } func (d *Display) String() string { return fmt.Sprintf("%dx%d console", d.w, d.h) } func (d *Display) Columns() int { return d.w } func (d *Display) Rows() int { return d.h } func (d *Display) Size() image.Point { return image.Pt(d.w, d.h) } func (d *Display) Refresh() error { fmt.Print("\x1b[?25l\x1b[2J") // hide the cursor and clear screen for i, l := 0, d.w*d.h; i < l; i += d.p.Stride { fmt.Println(string(d.p.Pix[i : i+d.p.Stride])) } return nil } func (d *Display) Bounds() image.Rectangle { return d.p.Rect } func (d *Display) ColorModel() color.Model { return d.p.ColorModel() } func (d *Display) At(x, y int) color.Color { return d.p.At(x, y) } func (d *Display) Set(x, y int, c color.Color) { d.p.Set(x, y, c) } func (d *Display) Halt() error { _, err := fmt.Print("\x1b[?25h\x1b[2J") // show the cursor and clear screen return err } type textImage struct { // Pix holds the image's pixels, as character values. The pixel at // (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*1]. Pix []rune // Stride is the Pix stride (in bytes) between vertically adjacent pixels. Stride int // Rect is the image's bounds. Rect image.Rectangle } func newTextImage(r image.Rectangle) *textImage { if r.Dy()%2 == 1 { r.Max.Y++ } p := make([]rune, r.Dx()*r.Dy()/2) for i := range p { p[i] = ' ' } return &textImage{ Pix: p, Stride: r.Dx(), Rect: r, } } func (p *textImage) ColorModel() color.Model { return pixel.MonochromeModel } func (p *textImage) PixOffset(x, y int) int { return (((y - p.Rect.Min.Y) >> 1) * p.Stride) + (x - p.Rect.Min.X) } func (p *textImage) At(x, y int) color.Color { if !(image.Point{x, y}.In(p.Rect)) { return pixel.Off } i := p.PixOffset(x, y) r := p.Pix[i] if y%2 == 1 { if r == '▀' || r == '█' { return pixel.On } } else { if r == '▄' || r == '█' { return pixel.On } } return pixel.Off } func (p *textImage) Set(x, y int, c color.Color) { if !(image.Point{x, y}.In(p.Rect)) { return } i := p.PixOffset(x, y) r := p.Pix[i] if pixel.IsOn(c) { if y%2 == 1 { if r == '▀' { p.Pix[i] = '█' } else { p.Pix[i] = '▄' } } else { if r == ' ' { p.Pix[i] = '▄' } } } else { if y%2 == 1 { if r == '█' { p.Pix[i] = '▀' } else { p.Pix[i] = ' ' } } else { if r == '█' { p.Pix[i] = '▄' } else { p.Pix[i] = ' ' } } } } var quadrants = []rune(" ▄▀█") /* func (p *textImage) PixOffset(x, y int) int { return (y-p.Rect.Min.Y)*p.Stride + (x - p.Rect.Min.X) } func (p *textImage) At(x, y int) color.Color { if !(image.Point{x, y}.In(p.Rect)) { return color.Gray{} } i := p.PixOffset(x, y) return fromGrayScale(p.Pix[i]) } func (p *textImage) Set(x, y int, c color.Color) { if !(image.Point{x, y}.In(p.Rect)) { return } i := p.PixOffset(x, y) p.Pix[i] = toGrayScale(c) } var grayScaleRamp = []rune(" ░░▒▓▓█") func toGrayScale(c color.Color) rune { g := color.GrayModel.Convert(c).(color.Gray) return grayScaleRamp[g.Y>>3] } func fromGrayScale(r rune) color.Gray { if i := strings.IndexRune(string(grayScaleRamp), r); i > -1 { return color.Gray{Y: uint8(i << 3)} } return color.Gray{} } */