Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
netux committed Jan 29, 2019
0 parents commit 2cd998b
Show file tree
Hide file tree
Showing 10 changed files with 601 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode/
debug
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Console Pxls viewer

View a Pxls canvas on the command-line!

## Known bugs

- Windows has fewer, wronger colors
- this is due to the library that's used ([termbox-go](https://github.com/nsf/termbox-go/)) not supporting xterm 256 colors at the time of this writing.
- Mingw on Windows not rendering anything
- no idea why this happens
- Moving horizontally goes twice as fast than moving vertically

## Controls

* Drag your mouse to scroll through the canvas.
* Use the arrow keys to move faster.
* Ctrl+C exists.

## Arguments

`--host`: the hostname of the pxls server. Default is "pxls.space"

`--secure`: whether the connection should be secure (use https: and wss: protocols) or insecure (use http: and ws: protocols). Default is true.

---

## Screenshots

Powershell in Windows:
![Windows, Powershell](images/windows-powershell.gif)

Ubuntu with Xterm color palette (full of visual glitches, but pretty colors)
![Ubuntu, Terminal (Xterm color palette)](images/ubuntu-xterm.gif)
32 changes: 32 additions & 0 deletions color_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// +build !windows linux,darwin

package main

import (
"github.com/lucasb-eyer/go-colorful"
"github.com/nsf/termbox-go"
)

const termboxXtermOffset = 0x11

// modification of https://github.com/ichinaski/pxl/blob/master/color.go.

// reduceRGB reduces color values to the range [0, 15].
func reduceRGB(color colorful.Color) (uint16, uint16, uint16) {
r, g, b, _ := color.RGBA()

return uint16(r >> 8), uint16(g >> 8), uint16(b >> 8)
}

// termColor converts a 24-bit RGB color into a term256 compatible approximation.
func colorToTermboxAttr(color colorful.Color) termbox.Attribute {
r, g, b := reduceRGB(color)

var (
termR = (((r * 5) + 127) / 255) * 36
termG = (((g * 5) + 127) / 255) * 6
termB = (((b * 5) + 127) / 255)
)

return termbox.Attribute(termR + termG + termB + termboxXtermOffset)
}
35 changes: 35 additions & 0 deletions color_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// +build windows

package main

import (
"github.com/lucasb-eyer/go-colorful"
"github.com/nsf/termbox-go"
)

// TODO(netux): wait for Termbox to add support for Output256 on Windows.
var termboxPalette = map[colorful.Color]termbox.Attribute{
colorful.Color{R: 0, G: 0, B: 0}: termbox.ColorBlack,
colorful.Color{R: 1, G: 0, B: 0}: termbox.ColorRed,
colorful.Color{R: 0, G: 1, B: 0}: termbox.ColorGreen,
colorful.Color{R: 0, G: 1, B: 0}: termbox.ColorBlue,
colorful.Color{R: 1, G: 1, B: 0}: termbox.ColorYellow,
colorful.Color{R: 1, G: 0, B: 1}: termbox.ColorMagenta,
colorful.Color{R: 0, G: 1, B: 1}: termbox.ColorCyan,
colorful.Color{R: 1, G: 1, B: 1}: termbox.ColorWhite,
}

func colorToTermboxAttr(color colorful.Color) termbox.Attribute {
var closest colorful.Color
var by float64 = 2
for c := range termboxPalette {
d := color.DistanceLab(c)

if d < by {
closest = c
by = d
}
}

return termboxPalette[closest]
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module go-dev.netux.site/shell/pxls-viewer

require (
github.com/lucasb-eyer/go-colorful v0.0.0-20181028223441-12d3b2882a08
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d
golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/lucasb-eyer/go-colorful v0.0.0-20181028223441-12d3b2882a08 h1:5MnxBC15uMxFv5FY/J/8vzyaBiArCOkMdFT9Jsw78iY=
github.com/lucasb-eyer/go-colorful v0.0.0-20181028223441-12d3b2882a08/go.mod h1:NXg0ArsFk0Y01623LgUqoqcouGDB+PwCCQlrwrG6xJ4=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d h1:x3S6kxmy49zXVVyhcnrFqxvNVCBPb2KZ9hV2RBdS840=
github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3 h1:ulvT7fqt0yHWzpJwI57MezWnYDVpCAYBVuYst/L+fAY=
golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Binary file added images/ubuntu-xterm.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/windows-powershell.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 2cd998b

Please sign in to comment.