Skip to content

Commit

Permalink
Formatted all Go code
Browse files Browse the repository at this point in the history
Closes #97
  • Loading branch information
tombh committed Jul 11, 2018
1 parent 3c41974 commit 2fc0b5c
Show file tree
Hide file tree
Showing 20 changed files with 335 additions and 247 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ env:
- secure: "boYctu4EQI2og3YWpD54yo1LzibgBNrYN3exbfwxaNNbJuQscprAlOOG5Y1O7MBgzG9AP1DeON3X0al7g/IYMrsMsrSGhJLq9w2/ntwMiUIyKfTAP8rB5fASRSYxA5mqhpjXK4iIzqG2xiOr1SmCbpT9ew0AXP/HgxQprsppBbH+H4HyrP4cejIbfk8ajU/XvrDmhOY4s/IZIy+vfEfZH5xheJnG3iWFICwWUF5CIjjr6fQpq9ZbL2sDMvzruYPMAQ3iDfBrUr+ZhN4PPjrzvCRUNpoUoPsXCsFDPMcZpBjSHH5ZELPRBzHSdJignIjHaV0UtLtFApZl49lzq749r2Sno2ba4JCd0RIgMERjNluXynkJiHrL2tMR8HYhOmoeLMdX0zrguv+136+Jn04gQde3S+FI1/sN0/Xa14xD7SGMUT1MgFnwKgFXOFeUd3VpvbpFg6RWVgpCnaDd2/AKL+mQDEgL4yRg9q95a6Q5ub/c6nla8/E4asEvYGlJPj0OG/MKvJMi0gnKuP4+nil0jgGeKki3k1UxwvldlS+n7HUhQ7c4B9c61KS19i+z5SxBiXjilL+zGlAVVgRcalIoKQrTRDSRKnKZkR5Ant0CLjt44pncHLwlYXD/aPuLNihCa4AqUlpYPYru0pQawXNkVnk6E/cg2DAsp7oDcpcg540="

before_install:
- diff -u <(echo -n) <(gofmt -d ./)
- ./interfacer/contrib/setup_go.sh
- ./webext/contrib/setup_node.sh
- mkdir -p $REPO_ROOT && cp -rfp $TRAVIS_BUILD_DIR -T $REPO_ROOT
Expand Down
20 changes: 10 additions & 10 deletions interfacer/src/browsh/browsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"encoding/base64"
"flag"
"fmt"
"strconv"
"io/ioutil"
"os"
"os/exec"
"runtime"
"path/filepath"
"runtime"
"strconv"
"strings"

// TCell seems to be one of the best projects in any language for handling terminal
Expand Down Expand Up @@ -43,15 +43,15 @@ var (
isDebug = flag.Bool("debug", false, "Log to ./debug.log")
timeLimit = flag.Int("time-limit", 0, "Kill Browsh after the specified number of seconds")
// StartupURL is the URL of the first tab at boot
StartupURL = flag.String("startup-url", "https://google.com", "URL to launch at startup")
StartupURL = flag.String("startup-url", "https://google.com", "URL to launch at startup")
// IsHTTPServer needs to be exported for use in tests
IsHTTPServer = flag.Bool("http-server", false, "Run as an HTTP service")
IsHTTPServer = flag.Bool("http-server", false, "Run as an HTTP service")
// HTTPServerPort also needs to be exported for use in tests
HTTPServerPort = flag.String("http-server-port", "4333", "HTTP server address")
httpServerBind = flag.String("http-server-bind", "0.0.0.0", "HTTP server binding address")
HTTPServerPort = flag.String("http-server-port", "4333", "HTTP server address")
httpServerBind = flag.String("http-server-bind", "0.0.0.0", "HTTP server binding address")
// IsTesting is used in tests, so it needs to be exported
IsTesting = false
logfile string
IsTesting = false
logfile string
)

func setupLogging() {
Expand Down Expand Up @@ -153,7 +153,7 @@ func getConfigFolder() string {
func Shell(command string) string {
parts := strings.Fields(command)
head := parts[0]
parts = parts[1:len(parts)]
parts = parts[1:]
out, err := exec.Command(head, parts...).CombinedOutput()
if err != nil {
Log(fmt.Sprintf(
Expand Down Expand Up @@ -211,7 +211,7 @@ func ttyEntry() {
// MainEntry decides between running Browsh as a CLI app or as an HTTP web server
func MainEntry() {
flag.Parse()
if (*IsHTTPServer) {
if *IsHTTPServer {
HTTPServerStart()
} else {
ttyEntry()
Expand Down
4 changes: 2 additions & 2 deletions interfacer/src/browsh/cells.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
// of this frame for fast scrolling.
type cell struct {
character []rune
fgColour tcell.Color
bgColour tcell.Color
fgColour tcell.Color
bgColour tcell.Color
}

// Both updating a frame and scrolling a frame can happen at the same time, so we need
Expand Down
14 changes: 7 additions & 7 deletions interfacer/src/browsh/comms.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
package browsh

import (
"strings"
"encoding/json"
"fmt"
"net/http"
"encoding/json"
"strings"

"github.com/gorilla/websocket"
)

var (
upgrader = websocket.Upgrader{
upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
stdinChannel = make(chan string)
stdinChannel = make(chan string)
isConnectedToWebExtension = false
)

type incomingRawText struct {
RequestID string `json:"request_id"`
RawText string `json:"body"`
RawText string `json:"body"`
}

func startWebSocketServer() {
serverMux := http.NewServeMux()
serverMux.HandleFunc("/", webSocketServer)
if err := http.ListenAndServe(":" + *webSocketPort, serverMux); err != nil {
if err := http.ListenAndServe(":"+*webSocketPort, serverMux); err != nil {
Shutdown(err)
}
}

func sendMessageToWebExtension(message string) {
if (!isConnectedToWebExtension) {
if !isConnectedToWebExtension {
Log("Webextension not connected. Message not sent: " + message)
return
}
Expand Down
100 changes: 55 additions & 45 deletions interfacer/src/browsh/frame_builder.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package browsh

import (
"fmt"
"encoding/json"
"fmt"
"unicode"

"github.com/gdamore/tcell"
Expand All @@ -13,14 +13,14 @@ import (
type frame struct {
// Dimensions of the frame's real data. Can be less than the DOM dimensions because
// we cannot sync frames of unlimited size from the browser.
subWidth int
subWidth int
subHeight int
// If the frame is smaller than the DOM, then this is the frame's position
// within the overall DOM.
subLeft int
subTop int
subTop int
// The total DOM dimensions. These are measured in the same units of the frame
totalWidth int
totalWidth int
totalHeight int
// The current position of the scroll in the TTY. Should be synced with the real
// browser.
Expand All @@ -31,8 +31,8 @@ type frame struct {
// of a different size and shape will interact with data from another DOM.
isDOMSizeChanged bool
// Raw data used to build a single, usable frame
pixels map[int][2]tcell.Color
text map[int][]rune
pixels map[int][2]tcell.Color
text map[int][]rune
textColours map[int]tcell.Color
// The actual built frame, can be used to render cells to the TTY
cells *threadSafeCellsMap
Expand All @@ -41,26 +41,26 @@ type frame struct {
}

type jsonFrameBase struct {
TabID int `json:"id"`
SubWidth int `json:"sub_width"`
SubHeight int `json:"sub_height"`
SubLeft int `json:"sub_left"`
SubTop int `json:"sub_top"`
TotalWidth int `json:"total_width"`
TabID int `json:"id"`
SubWidth int `json:"sub_width"`
SubHeight int `json:"sub_height"`
SubLeft int `json:"sub_left"`
SubTop int `json:"sub_top"`
TotalWidth int `json:"total_width"`
TotalHeight int `json:"total_height"`
}

type incomingFrameText struct {
Meta jsonFrameBase `json:"meta"`
Text []string `json:"text"`
Colours []int32 `json:"colours"`
Meta jsonFrameBase `json:"meta"`
Text []string `json:"text"`
Colours []int32 `json:"colours"`
InputBoxes map[string]inputBox `json:"input_boxes"`
}

// TODO: Can these be sent as binary blobs?
type incomingFramePixels struct {
Meta jsonFrameBase `json:"meta"`
Colours []int32 `json:"colours"`
Meta jsonFrameBase `json:"meta"`
Colours []int32 `json:"colours"`
}

func (f *frame) domRowCount() int {
Expand All @@ -77,7 +77,7 @@ func parseJSONFrameText(jsonString string) {
if err := json.Unmarshal(jsonBytes, &incoming); err != nil {
Shutdown(err)
}
if (!isTabPresent(incoming.Meta.TabID)) {
if !isTabPresent(incoming.Meta.TabID) {
Log(fmt.Sprintf("Not building frame for non-existent tab ID: %d", incoming.Meta.TabID))
return
}
Expand All @@ -86,7 +86,9 @@ func parseJSONFrameText(jsonString string) {

func (f *frame) buildFrameText(incoming incomingFrameText) {
f.setup(incoming.Meta)
if (!f.isIncomingFrameTextValid(incoming)) { return }
if !f.isIncomingFrameTextValid(incoming) {
return
}
f.updateInputBoxes(incoming)
f.populateFrameText(incoming)
}
Expand All @@ -97,17 +99,21 @@ func parseJSONFramePixels(jsonString string) {
if err := json.Unmarshal(jsonBytes, &incoming); err != nil {
Shutdown(err)
}
if (!isTabPresent(incoming.Meta.TabID)) {
if !isTabPresent(incoming.Meta.TabID) {
Log(fmt.Sprintf("Not building frame for non-existent tab ID: %d", incoming.Meta.TabID))
return
}
if (len(Tabs[incoming.Meta.TabID].frame.text) == 0) { return }
if len(Tabs[incoming.Meta.TabID].frame.text) == 0 {
return
}
Tabs[incoming.Meta.TabID].frame.buildFramePixels(incoming)
}

func (f *frame) buildFramePixels(incoming incomingFramePixels) {
f.setup(incoming.Meta)
if (!f.isIncomingFramePixelsValid(incoming)) { return }
if !f.isIncomingFramePixelsValid(incoming) {
return
}
f.populateFramePixels(incoming)
}

Expand All @@ -132,7 +138,7 @@ func (f *frame) resetCells() {
}

func (f *frame) isIncomingFrameTextValid(incoming incomingFrameText) bool {
if (len(incoming.Text) == 0) {
if len(incoming.Text) == 0 {
Log("Not parsing zero-size text frame")
return false
}
Expand Down Expand Up @@ -166,29 +172,29 @@ func (f *frame) updateInputBoxes(incoming incomingFrameText) {
func (f *frame) populateFrameText(incoming incomingFrameText) {
var cellIndex, frameIndex, colourIndex int
if f.isDOMSizeChanged || f.text == nil {
f.text = make(map[int][]rune, (f.domRowCount()) * f.totalWidth)
f.textColours = make(map[int]tcell.Color, (f.domRowCount()) * f.totalWidth)
f.text = make(map[int][]rune, (f.domRowCount())*f.totalWidth)
f.textColours = make(map[int]tcell.Color, (f.domRowCount())*f.totalWidth)
}
for y := 0; y < f.subRowCount(); y++ {
for x := 0; x < f.subWidth; x++ {
cellIndex = f.getCellIndexFromSubCoords(x, y * 2)
cellIndex = f.getCellIndexFromSubCoords(x, y*2)
frameIndex = (y * f.subWidth) + x
colourIndex = frameIndex * 3
f.textColours[cellIndex] = tcell.NewRGBColor(
incoming.Colours[colourIndex + 0],
incoming.Colours[colourIndex + 1],
incoming.Colours[colourIndex + 2],
incoming.Colours[colourIndex+0],
incoming.Colours[colourIndex+1],
incoming.Colours[colourIndex+2],
)
f.text[cellIndex] = []rune(incoming.Text[frameIndex])
f.buildCell(f.subLeft + x, (f.subTop / 2) + y);
f.buildCell(f.subLeft+x, (f.subTop/2)+y)
}
}
}

func (f *frame) populateFramePixels(incoming incomingFramePixels) {
var cellIndex, frameIndexFg, frameIndexBg, pixelIndexFg, pixelIndexBg int
if f.isDOMSizeChanged || f.pixels == nil {
f.pixels = make(map[int][2]tcell.Color, f.totalHeight * f.totalWidth)
f.pixels = make(map[int][2]tcell.Color, f.totalHeight*f.totalWidth)
}
data := incoming.Colours
for y := 0; y < f.subHeight; y += 2 {
Expand All @@ -200,24 +206,24 @@ func (f *frame) populateFramePixels(incoming incomingFramePixels) {
pixelIndexFg = frameIndexFg * 3
pixels := [2]tcell.Color{
tcell.NewRGBColor(
data[pixelIndexBg + 0],
data[pixelIndexBg + 1],
data[pixelIndexBg + 2],
data[pixelIndexBg+0],
data[pixelIndexBg+1],
data[pixelIndexBg+2],
),
tcell.NewRGBColor(
data[pixelIndexFg + 0],
data[pixelIndexFg + 1],
data[pixelIndexFg + 2],
data[pixelIndexFg+0],
data[pixelIndexFg+1],
data[pixelIndexFg+2],
),
}
f.pixels[cellIndex] = pixels
f.buildCell(f.subLeft + x, (f.subTop + y) / 2);
f.buildCell(f.subLeft+x, (f.subTop+y)/2)
}
}
}

func (f *frame) isIncomingFramePixelsValid(incoming incomingFramePixels) bool {
if (len(incoming.Colours) == 0) {
if len(incoming.Colours) == 0 {
Log("Not parsing zero-size text frame")
return false
}
Expand All @@ -233,7 +239,7 @@ func (f *frame) buildCell(x int, y int) {
index := (y * f.totalWidth) + x
character, fgColour := f.getCharacterAt(index)
pixelFg, bgColour := f.getPixelColoursAt(index)
if (isCharacterTransparent(character)) {
if isCharacterTransparent(character) {
character = []rune("▄")
fgColour = pixelFg
}
Expand Down Expand Up @@ -266,13 +272,13 @@ func (f *frame) getPixelColoursAt(index int) (tcell.Color, tcell.Color) {
}

func isCharacterTransparent(character []rune) bool {
return string(character) == "" || unicode.IsSpace(character[0]);
return string(character) == "" || unicode.IsSpace(character[0])
}

func (f *frame) addCell(index int, fgColour, bgColour tcell.Color, character []rune) {
newCell := cell{
fgColour: fgColour,
bgColour: bgColour,
fgColour: fgColour,
bgColour: bgColour,
character: character,
}
f.cells.store(index, newCell)
Expand All @@ -289,8 +295,12 @@ func (f *frame) getCellIndexFromSubCoords(x, y int) int {

func (f *frame) limitScroll(height int) {
maxYScroll := f.domRowCount() - height
if (f.yScroll > maxYScroll) { f.yScroll = maxYScroll }
if (f.yScroll < 0) { f.yScroll = 0 }
if f.yScroll > maxYScroll {
f.yScroll = maxYScroll
}
if f.yScroll < 0 {
f.yScroll = 0
}
}

func (f *frame) maybeFocusInputBox(x, y int) {
Expand Down
3 changes: 1 addition & 2 deletions interfacer/src/browsh/frame_builder_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package browsh

import (
"testing"
"fmt"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -260,4 +260,3 @@ var _ = Describe("Frame struct", func() {
})
})
})

Loading

0 comments on commit 2fc0b5c

Please sign in to comment.