Skip to content

Commit

Permalink
Added help text and updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ishuah committed Mar 8, 2018
1 parent 7c33459 commit 706f918
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,28 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/ishuah/bifrost)](https://goreportcard.com/report/github.com/ishuah/bifrost)

Bifrost is a tiny terminal emulator for serial port communication.

Note: Only Linux and OSX are currenly supported. Windows will be supported in subsequent releases.

## Installation
- Download the latest version from the releases page (https://github.com/ishuah/bifrost/releases)

On linux:
- Unzip and copy binary to `/usr/bin/`
```
unzip bifrost-<version>-linux-amd64.zip
cd bifrost-<version>-linux-amd64
sudo cp bifrost /usr/bin/
sudo chown root:root /usr/bin/bifrost
sudo chmod 755 /usr/bin/bifrost
```
On OSX:
- Unzip and copy binary to `/usr/local/bin/`
```
sudo mkdir -p /usr/local/bin
unzip bifrost-<version>-darwin-amd64.zip
cd bifrost-<version>-darwin-amd64
sudo cp bifrost /usr/local/bin/
```

- Run `bifrost -help` to confirm bifrost was installed correctly.
46 changes: 38 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,30 @@ import (
"github.com/tarm/serial"
)

const version = "v0.1.20"
const version = "v0.1.20-rc1"

var header = fmt.Sprintf("\nBifrost %s\n", version)
var helpText = fmt.Sprintf(`%s
Bifrost is a tiny terminal emulator for serial port communication.
Usage:
bifrost [flags]
Flags:
-port-path Name/path of the serial port
-baud The baud rate to use on the connection
-help This help message
`, header)

func welcomeMessage(portPath string, baud int) string {
return fmt.Sprintf(`%s
Options:
Port: %s
Baud rate: %d
Press Ctrl+\ to exit
`, header, portPath, baud)
}

func bufferedReader(portReader *bufio.Reader, buf chan []byte) {
for {
Expand All @@ -33,8 +56,18 @@ func bufferedWriter(screen screen.Screen, buf chan []byte) {
}

func main() {
name := flag.String("name", "/dev/tty.usbserial", "serial device port")
baud := flag.Int("baud", 115200, "baud rate")
var portPath string
var baud int
var help bool
flag.StringVar(&portPath, "port-path", "/dev/tty.usbserial", "Name/path of the serial port")
flag.IntVar(&baud, "baud", 115200, "The baud rate to use on the connection")
flag.BoolVar(&help, "help", false, "A brief help message")
flag.Parse()

if help {
fmt.Println(helpText)
return
}

err := termbox.Init()
if err != nil {
Expand All @@ -45,7 +78,7 @@ func main() {

screen := screen.NewScreen()

c := &serial.Config{Name: *name, Baud: *baud, ReadTimeout: time.Nanosecond}
c := &serial.Config{Name: portPath, Baud: baud, ReadTimeout: time.Nanosecond}
port, err := serial.OpenPort(c)

if err != nil {
Expand All @@ -55,10 +88,7 @@ func main() {
portReader := bufio.NewReader(port)

// Welcome message
screen.Write(fmt.Sprintf("\nBifrost %s\n\n", version))
screen.Write("Options:\n")
screen.Write(fmt.Sprintf("\t\tPort: %s\n\t\tBaud rate: %d\n\n", *name, *baud))
screen.Write("Press Ctrl+\\ to exit\n\n")
screen.Write(welcomeMessage(portPath, baud))

buf := make(chan []byte)
go bufferedReader(portReader, buf)
Expand Down
46 changes: 36 additions & 10 deletions screen/screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func (s *Screen) write(line string) {
switch seq.Command {
case ansi.SelectGraphicRendition:
s.graphicHandler(seq.Params)
case ansi.CursorPosition:
s.cursorHandler(seq.Params)
case ansi.CursorPosition, ansi.CursorUp,
ansi.CursorDown, ansi.CursorForward, ansi.CursorBack:
s.cursorHandler(seq.Command, seq.Params)
case ansi.EraseInDisplay, ansi.EraseInLine:
s.eraseHandler(seq.Command, seq.Params)
}
Expand Down Expand Up @@ -59,7 +60,6 @@ func (s *Screen) write(line string) {
}
}
}

}
}

Expand Down Expand Up @@ -114,15 +114,41 @@ func (s *Screen) graphicHandler(params []int) {
}
}

func (s *Screen) cursorHandler(params []int) {
if len(params) == 0 {
s.x = 0
s.y = 0
return
func (s *Screen) cursorHandler(command byte, params []int) {
n, m := 1, 1
if len(params) > 0 {
n = params[0]
}

if len(params) > 1 {
m = params[1]
}

s.x = params[1] - 1
s.y = params[0] - 1
switch command {
case ansi.CursorPosition:
s.x = m - 1
s.y = n - 1
case ansi.CursorUp:
s.y = s.y - n
if s.y < 0 {
s.y = 0
}
case ansi.CursorDown:
s.y = s.y + n
if s.y > s.height {
s.y = s.height
}
case ansi.CursorForward:
s.x = s.x + n
if s.x > s.width {
s.x = s.width
}
case ansi.CursorBack:
s.x = s.x - n
if s.x < 0 {
s.x = 0
}
}
}

func (s *Screen) Write(line string) {
Expand Down

0 comments on commit 706f918

Please sign in to comment.