Skip to content

Commit

Permalink
Add foreground colour support to terminal
Browse files Browse the repository at this point in the history
Updated Fyne in vendor to get color support in TextGrid
  • Loading branch information
andydotxyz committed Mar 2, 2020
1 parent 45a0fc6 commit 08833d5
Show file tree
Hide file tree
Showing 32 changed files with 549 additions and 222 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

A Linux terminal emulator using the Fyne toolkit.

<img alt="screenshot" src="screenshot.png" width="556" />
<img alt="screenshot" src="screenshot.png" width="929" />
43 changes: 42 additions & 1 deletion escape.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package terminal

import (
"image/color"
"log"
"strconv"
"strings"

"fyne.io/fyne/theme"
)

var currentFG color.Color

func (t *Terminal) handleEscape(code string) {
switch code { // exact matches
case "H", ";H":
Expand All @@ -16,6 +21,9 @@ func (t *Terminal) handleEscape(code string) {
t.clearScreen()
case "K":
row := t.content.Row(t.cursorRow)
if t.cursorCol > len(row) {
return
}
t.content.SetRow(t.cursorRow, row[:t.cursorCol])
default: // check mode (last letter) then match
message := code[:len(code)-1]
Expand All @@ -25,13 +33,46 @@ func (t *Terminal) handleEscape(code string) {
row, _ := strconv.Atoi(parts[0])
col, _ := strconv.Atoi(parts[1])

if row < len(t.content.Buffer) {
if row < len(t.content.Content) {
t.cursorRow = row
}
line := t.content.Row(t.cursorRow)
if col < len(line) {
t.cursorCol = col
}
case "m":
if message == "" || message == "0" {
currentFG = nil
return
}

modes := strings.Split(message, ";")
for _, mode := range modes {
switch mode {
case "7": // reverse
currentFG = theme.BackgroundColor()
case "27": // reverse off
currentFG = nil
case "30":
currentFG = color.Black
case "31":
currentFG = &color.RGBA{255, 0, 0, 255}
case "32":
currentFG = &color.RGBA{0, 255, 0, 255}
case "33":
currentFG = &color.RGBA{255, 255, 0, 255}
case "34":
currentFG = &color.RGBA{0, 0, 255, 255}
case "35":
currentFG = &color.RGBA{255, 0, 255, 255}
case "36":
currentFG = &color.RGBA{0, 255, 255, 255}
case "37":
currentFG = color.White
default:
log.Println("Unsupported graphics mode", mode)
}
}
default:
log.Println("Unrecognised Escape:", code)
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/fyne-io/terminal
go 1.13

require (
fyne.io/fyne v1.2.2-0.20200229144543-75524de963d0
fyne.io/fyne v1.2.4-0.20200302154258-a1e26ef951f6
github.com/creack/pty v1.1.9
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d // indirect
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 // indirect
)
9 changes: 2 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
fyne.io/fyne v1.2.2-0.20200223223253-472c76c32bb7 h1:I5aBskVBj1ouMFU+SiQrmeFYX+4HREerixMlawiMgCo=
fyne.io/fyne v1.2.2-0.20200223223253-472c76c32bb7/go.mod h1:AwxWB9CEwG1B7XFmnj5zRW5jEI5aiyeXYe4Ve83L96c=
fyne.io/fyne v1.2.2-0.20200229144543-75524de963d0 h1:dXWcw/j9rE2vN8cgAkUo6RgPsfMfxPx+oUWfHLvREqs=
fyne.io/fyne v1.2.2-0.20200229144543-75524de963d0/go.mod h1:AwxWB9CEwG1B7XFmnj5zRW5jEI5aiyeXYe4Ve83L96c=
fyne.io/fyne v1.2.4-0.20200302154258-a1e26ef951f6 h1:xIllRcR1Wua8vCkqY1KvLsc1UfYNyWIlvRY6qjqpKXM=
fyne.io/fyne v1.2.4-0.20200302154258-a1e26ef951f6/go.mod h1:5nHgSQyRojVlrXE7qrNeRfVNXD8OUDaRt/Dudk5D0us=
github.com/Kodeworks/golang-image-ico v0.0.0-20141118225523-73f0f4cfade9/go.mod h1:7uhhqiBaR4CpN0k9rMjOtjpcfGd6DG2m04zQxKnWQ0I=
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
github.com/creack/pty v1.1.9 h1:uDmaGzcdjhF4i/plgjmEsriH11Y0o7RKapEf/LDaM3w=
Expand Down Expand Up @@ -34,8 +32,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709 h1:Ko2LQMrRU+Oy/+EDBwX7eZ2jp3C47eDBB8EIhKTun+I=
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d h1:1ZiEyfaQIg3Qh0EoqpwAakHVhecoE5wlSg5GjnafJGw=
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8 h1:idBdZTd9UioThJp8KpM/rTSinK/ChZFBE43/WtIy8zg=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
Expand All @@ -47,7 +43,6 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2eP
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb h1:fgwFCsaw9buMuxNd6+DQfAuSFqbNiQZpcgJQAgJsK6k=
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
Expand Down
49 changes: 46 additions & 3 deletions output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package terminal

import (
"image/color"
"time"

"fyne.io/fyne/widget"
)

func (t *Terminal) handleOutput(buf []byte) {
Expand Down Expand Up @@ -64,14 +67,23 @@ func (t *Terminal) handleOutput(buf []byte) {
continue
case '\n':
row := t.content.Row(t.cursorRow)
row = append(row, []rune(out)...)
// TODO use styles here too
for i, r := range out {
i += t.cursorCol
if i >= len(row) {
row = append(row, widget.TextGridCell{Rune: r})
} else {
row[i].Rune = r
}
}
t.content.SetRow(t.cursorRow, row)

// TODO this needs to apply to styles too, how do we do this?
if t.cursorRow == int(t.config.Rows-1) {
for i = 0; i < t.cursorRow; i++ {
t.content.SetRow(i, t.content.Row(i+1))
}
t.content.SetRow(i, []rune{})
t.content.SetRow(i, []widget.TextGridCell{})
} else {
t.cursorRow++
}
Expand All @@ -88,6 +100,10 @@ func (t *Terminal) handleOutput(buf []byte) {
case '\t': // TODO remove silly approximation
out += " "
default:
if currentFG != nil {
// TODO not if we discard out
t.setCellStyle(t.cursorRow, t.cursorCol+len(out), currentFG)
}
out += string(r)
}
esc = -5
Expand All @@ -99,12 +115,39 @@ func (t *Terminal) handleOutput(buf []byte) {
return
}
row := t.content.Row(t.cursorRow)
row = append(row, []rune(out)...)
for i, r := range out {
i += t.cursorCol
if i >= len(row) {
row = append(row, widget.TextGridCell{Rune: r})
} else {
row[i].Rune = r
}
}
t.content.SetRow(t.cursorRow, row)
t.cursorCol += len(out)
t.Refresh()
}

func (t *Terminal) setCellStyle(row, col int, fgStyle color.Color) {
if row < 0 {
return
}
for len(t.content.Content) <= row {
t.content.Content = append(t.content.Content, []widget.TextGridCell{})
}

line := t.content.Row(row)
if col < 0 {
return
}

for len(line) <= col {
line = append(line, widget.TextGridCell{})
}
t.content.SetRow(row, line)
line[col].TextColor = fgStyle
}

func (t *Terminal) ringBell() {
t.bell = true
t.Refresh()
Expand Down
Binary file modified screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 21 additions & 1 deletion vendor/fyne.io/fyne/CHANGELOG.md

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

61 changes: 61 additions & 0 deletions vendor/fyne.io/fyne/CONTRIBUTING.md

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

20 changes: 20 additions & 0 deletions vendor/fyne.io/fyne/README-mobile.md

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

9 changes: 4 additions & 5 deletions vendor/fyne.io/fyne/README.md

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

4 changes: 2 additions & 2 deletions vendor/fyne.io/fyne/app.go

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

2 changes: 2 additions & 0 deletions vendor/fyne.io/fyne/canvasobject.go

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

2 changes: 1 addition & 1 deletion vendor/fyne.io/fyne/go.mod

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

4 changes: 2 additions & 2 deletions vendor/fyne.io/fyne/go.sum

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

Loading

0 comments on commit 08833d5

Please sign in to comment.