Skip to content

Commit

Permalink
stash
Browse files Browse the repository at this point in the history
  • Loading branch information
giulianopz committed Jul 24, 2023
1 parent 4bf7a72 commit 1878df8
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 80 deletions.
1 change: 1 addition & 0 deletions pkg/ansi/codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
// display attributes
ALL_ATTRIBUTES_OFF = 0
BOLD = 1
FAINT = 2
REVERSE_COLOR = 7
SET_FG_COLOR = 38
SET_BG_COLOR = 48
Expand Down
16 changes: 8 additions & 8 deletions pkg/ansi/escape_sequences.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ func MoveCursor(y, x int) string {

// SGR sets display attributes
// see: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
func SGR(n int) string {
return fmt.Sprintf(SGR_FMT, strconv.Itoa(n))
func SGR(nums ...int) string {
params := make([]string, 0)
for _, n := range nums {
params = append(params, strconv.Itoa(n))
}
return fmt.Sprintf(SGR_FMT, strings.Join(params, ";"))
}

func SetColors(fg, bg int) string {
colors := strings.Join([]string{
strconv.Itoa(fg),
strconv.Itoa(bg),
}, ";")
return fmt.Sprintf(SGR_FMT, colors)
func WhiteFG() string {
return SGR(38, 2, 255, 255, 255)
}

func Erase(n int) string {
Expand Down
88 changes: 66 additions & 22 deletions pkg/display/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,57 @@ const (
articleTextSectionMsg = "HELP: \u232B = go back | \u25B2 = scroll up | \u25BC = scroll down"
)

type cell struct {
char rune
fg, bg [3]int
params []int
}

func newCell(c rune) *cell {
return &cell{
char: c,
//fg: [3]int{255, 255, 255},
//params: []int{ansi.FAINT},
}
}

func fromString(s string) []*cell {
cells := make([]*cell, 0)
for _, r := range s {
cells = append(cells, newCell(r))
}
return cells
}

func fromStringWithStyle(s string, params ...int) []*cell {
cells := make([]*cell, 0)
for _, r := range s {
cells = append(cells, newCell(r).withStyle(params...))
}
return cells
}

func (c *cell) withStyle(a ...int) *cell {
c.params = make([]int, 0)
c.params = append(c.params, a...)
return c
}

func (c cell) String() string {
if len(c.params) == 0 {
return string(c.char)
}
return ansi.SGR(c.params...) + string(c.char)
}

func stringify(cells []*cell) string {
var ret string
for _, c := range cells {
ret += c.String()
}
return ret
}

type display struct {
// cursor's position within terminal window
cx int
Expand All @@ -65,7 +116,7 @@ type display struct {
// display raw text
raw [][]byte
// display rendered text
rendered [][]rune
rendered [][]*cell
// gob cache
cache *cache.Cache

Expand Down Expand Up @@ -146,8 +197,8 @@ func (d *display) appendToRaw(s string) {
d.raw = append(d.raw, []byte(s))
}

func (d *display) appendToRendered(s string) {
d.rendered = append(d.rendered, []rune(s))
func (d *display) appendToRendered(cells []*cell) {
d.rendered = append(d.rendered, cells)
}

func (d *display) currentRow() int {
Expand All @@ -166,7 +217,7 @@ func (d *display) resetCoordinates() {

func (d *display) resetRows() {
d.raw = make([][]byte, 0)
d.rendered = make([][]rune, 0)
d.rendered = make([][]*cell, 0)
}

func (d *display) insertCharAt(c string, i int) {
Expand Down Expand Up @@ -203,7 +254,7 @@ func (d *display) LoadCache() error {
return nil
}

func (d *display) exitEditingMode(color int) {
func (d *display) exitEditingMode() {
d.editingMode = false
d.editingBuf = []string{}
}
Expand Down Expand Up @@ -247,10 +298,10 @@ func (d *display) draw(buf *bytes.Buffer) {
buf.WriteString(fmt.Sprintf("%s\r\n", app.Version))
}

/* main content */

buf.WriteString(ansi.SGR(ansi.ALL_ATTRIBUTES_OFF))

/* main content */

for k := 0; k < d.width; k++ {
buf.WriteString("-")
}
Expand All @@ -274,38 +325,31 @@ func (d *display) draw(buf *bytes.Buffer) {
for i := d.startoff; i <= d.endoff; i++ {

if i == d.currentRow() && d.currentSection != ARTICLE_TEXT && !d.editingMode {

buf.WriteString(ansi.SGR(ansi.REVERSE_COLOR))
}

// TODO check that the terminal supports Unicode output, before outputting a Unicode character
// if so, the "LANG" env variable should contain "UTF"

line := string(d.rendered[i])
if line == "" {
line = " "
} else {
runes := utf8.RuneCountInString(line)
if runes > d.width {
log.Default().Printf("truncating current line because its length %d exceeda screen width: %d\n", i, runes)
line = util.Truncate(line, d.width)
}
line := d.rendered[i]
if len(line) > d.width {
log.Default().Printf("truncating current line because its length %d exceeda screen width: %d\n", len(line), d.width)
line = line[:d.width]
}

log.Default().Printf("writing to buf line #%d: %q\n", i, line)

buf.WriteString("\x1b[38;2;255;255;255m")
_, err := buf.Write([]byte(line))
_, err := buf.WriteString(stringify(line))
if err != nil {
log.Default().Printf("cannot write byte array %q: %v", []byte(" "), err)
log.Default().Printf("cannot write line: %v", err)
}

buf.WriteString("\r\n")

if i == d.currentRow() && d.currentSection != ARTICLE_TEXT {
buf.WriteString(ansi.SGR(ansi.ALL_ATTRIBUTES_OFF))
}

buf.WriteString("\r\n")

printed++
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/display/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"
"unicode/utf8"

"github.com/giulianopz/newscanoe/pkg/ansi"
"github.com/giulianopz/newscanoe/pkg/ascii"
"github.com/giulianopz/newscanoe/pkg/util"
"golang.org/x/sys/unix"
Expand Down Expand Up @@ -264,7 +263,7 @@ func (d *display) whileEditing(input byte) {
{
d.setBottomMessage(urlsListSectionMsg)
d.setTmpBottomMessage(1*time.Second, "editing aborted!")
d.exitEditingMode(ansi.WHITE_FG)
d.exitEditingMode()
d.resetCoordinates()
}
default:
Expand Down
3 changes: 1 addition & 2 deletions pkg/display/loading.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"
"time"

"github.com/giulianopz/newscanoe/pkg/ansi"
"github.com/giulianopz/newscanoe/pkg/html"
"github.com/giulianopz/newscanoe/pkg/util"
)
Expand Down Expand Up @@ -266,5 +265,5 @@ func (d *display) addEnteredFeedUrl() {

d.setBottomMessage(urlsListSectionMsg)
d.setTmpBottomMessage(3*time.Second, "new feed saved!")
d.exitEditingMode(ansi.GREEN_FG)
d.exitEditingMode()
}
51 changes: 22 additions & 29 deletions pkg/display/rendering.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package display

import (
"fmt"
"log"
"strings"
"time"
Expand All @@ -19,21 +18,18 @@ func (d *display) renderURLs() {
cached[cachedFeed.Url] = cachedFeed
}

d.rendered = make([][]rune, 0)
d.rendered = make([][]*cell, 0)
for row := range d.raw {
url := d.raw[row]
if !strings.Contains(string(url), "#") {
cachedFeed, present := cached[string(url)]
if !present {
d.appendToRendered(string(url))
d.appendToRendered(fromString(string(url)))
} else {
if cachedFeed.New {
d.appendToRendered(fmt.Sprintf("%s%s%s",
ansi.SGR(1),
cachedFeed.Title,
ansi.SGR(ansi.ALL_ATTRIBUTES_OFF)))
d.appendToRendered(fromStringWithStyle(cachedFeed.Title, ansi.BOLD))
} else {
d.appendToRendered(cachedFeed.Title)
d.appendToRendered(fromString(cachedFeed.Title))
}
}
}
Expand All @@ -49,16 +45,13 @@ func (d *display) renderArticlesList() {
}
}

d.rendered = make([][]rune, 0)
d.rendered = make([][]*cell, 0)
if currentFeed != nil {
for _, item := range currentFeed.GetItemsOrderedByDate() {
if item.New {
d.appendToRendered(fmt.Sprintf("%s%s%s",
ansi.SGR(1),
util.RenderArticleRow(item.PubDate, item.Title),
ansi.SGR(ansi.ALL_ATTRIBUTES_OFF)))
d.appendToRendered(fromStringWithStyle(util.RenderArticleRow(item.PubDate, item.Title), ansi.BOLD))
} else {
d.appendToRendered(util.RenderArticleRow(item.PubDate, item.Title))
d.appendToRendered(fromString(util.RenderArticleRow(item.PubDate, item.Title)))
}
}
} else {
Expand Down Expand Up @@ -88,39 +81,39 @@ func (d *display) renderArticleText() {
}
}

d.rendered = make([][]rune, 0)
line := make([]rune, 0)
d.rendered = make([][]*cell, 0)
line := make([]*cell, 0)
for _, c := range runes {

if c == '\r' || c == '\n' {

if len(line) != 0 {
d.rendered = append(d.rendered, add(margin, line))
}
d.rendered = append(d.rendered, []rune{})
line = make([]rune, 0)
d.rendered = append(d.rendered, []*cell{})
line = make([]*cell, 0)
continue
}

if c == '\t' {
for i := 0; i < 4; i++ {
line = append(line, ' ')
line = append(line, newCell(' '))
}
continue
}

if len(line) < textSpace {
line = append(line, c)
line = append(line, newCell(c))
} else {

if line[len(line)-1] != ' ' {
if line[len(line)-1].char != ' ' {

var lastIdx int = len(line)
tmp := make([]rune, 0)
tmp := make([]*cell, 0)
for i := lastIdx - 1; i >= 0; i-- {
lastIdx--
if line[i] != ' ' {
tmp = append([]rune{line[i]}, tmp...)
if line[i].char != ' ' {
tmp = append([]*cell{line[i]}, tmp...)
} else {
break
}
Expand All @@ -131,10 +124,10 @@ func (d *display) renderArticleText() {
} else {

d.rendered = append(d.rendered, add(margin, line))
line = make([]rune, 0)
line = make([]*cell, 0)
}

line = append(line, c)
line = append(line, newCell(c))
}
}

Expand All @@ -143,11 +136,11 @@ func (d *display) renderArticleText() {
}
}

func add(margin int, line []rune) []rune {
func add(margin int, line []*cell) []*cell {
if margin != 0 {
padded := make([]rune, 0)
padded := make([]*cell, 0)
for margin != 0 {
padded = append(padded, ' ')
padded = append(padded, newCell(' '))
margin--
}
padded = append(padded, line...)
Expand Down
File renamed without changes.
17 changes: 0 additions & 17 deletions pkg/util/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,3 @@ see paragraph 2.2: https://www.ietf.org/rfc/rfc3986.txt
func IsSpecialChar(input byte) bool {
return slices.Contains(specialChars, input)
}

func Truncate(str string, length int) string {
if length <= 0 {
return ""
}

truncated := ""
count := 0
for _, rune := range str {
truncated += string(rune)
count++
if count >= length {
break
}
}
return truncated
}

0 comments on commit 1878df8

Please sign in to comment.