Skip to content

Commit

Permalink
Fix cursor displayed when parsing a new feed in bottom bar
Browse files Browse the repository at this point in the history
  • Loading branch information
giulianopz committed Aug 30, 2023
1 parent 8b956e1 commit 72fb0fb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 32 deletions.
41 changes: 25 additions & 16 deletions internal/display/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,11 @@ type display struct {
mu sync.Mutex

// current position of cursor
current *Pos
current *pos
// previous positions of cursor
previous []*Pos
previous []*pos

hideCursor bool

// size of terminal window
height int
Expand Down Expand Up @@ -153,16 +155,15 @@ type display struct {
currentArticleUrl string
}

type Pos struct {
type pos struct {
// cursor's position within visible content window
cy, cx int

// offsets of rendered content window
startoff, endoff int
}

func (d *display) trackPos() {
d.previous = append(d.previous, &Pos{
d.previous = append(d.previous, &pos{
cx: d.current.cx,
cy: d.current.cy,
startoff: d.current.startoff,
Expand All @@ -188,13 +189,13 @@ func New(debugMode bool) *display {

d := &display{
debugMode: debugMode,
current: &Pos{
current: &pos{
cx: 1,
cy: 1,
startoff: 0,
endoff: 0,
},
previous: make([]*Pos, 0),
previous: make([]*pos, 0),
ListenToKeyStrokes: true,
config: &config.Config{},
cache: cache.NewCache(),
Expand Down Expand Up @@ -232,14 +233,24 @@ func (d *display) setBottomMessage(msg string) {
}

func (d *display) setTmpBottomMessage(t time.Duration, msg string) {
previous := d.bottomBarMsg

fmt.Fprint(os.Stdout, ansi.HideCursor())
previousMsg := d.bottomBarMsg
d.setBottomMessage(msg)
d.hideCursor = true

go func() {
time.AfterFunc(t, func() {
d.setBottomMessage(previous)

defer func() {
d.setBottomMessage(previousMsg)
d.hideCursor = false
}()

fmt.Fprint(os.Stdout, ansi.MoveCursor(d.height, 1))
fmt.Fprint(os.Stdout, ansi.EraseToEndOfLine(ansi.ERASE_ENTIRE_LINE))
fmt.Fprint(os.Stdout, ansi.SGR(ansi.REVERSE_COLOR))
fmt.Fprint(os.Stdout, util.PadToRight(previousMsg, d.width))
fmt.Fprint(os.Stdout, ansi.SGR(ansi.ALL_ATTRIBUTES_OFF))
})
}()
}
Expand Down Expand Up @@ -372,9 +383,9 @@ func (d *display) draw(buf *bytes.Buffer) {

var runes int

if d.currentSection != ARTICLE_TEXT && !d.editingMode {
if d.currentSection != ARTICLE_TEXT {
var arrow string
if i == d.currentRow() {
if i != d.height && i == d.currentRow() {
// https://en.wikipedia.org/wiki/Geometric_Shapes_(Unicode_block)
arrow = "\u25B6"
}
Expand Down Expand Up @@ -458,11 +469,9 @@ func (d *display) RefreshScreen() {

d.draw(buf)

if d.editingMode {
fmt.Fprint(buf, ansi.MoveCursor(d.height, d.current.cx))
fmt.Fprint(buf, ansi.MoveCursor(d.current.cy, d.current.cx))
if d.editingMode && !d.hideCursor {
fmt.Fprint(buf, ansi.ShowCursor())
} else {
fmt.Fprint(buf, ansi.MoveCursor(d.current.cy, d.current.cx))
}

fmt.Fprint(os.Stdout, buf.String())
Expand Down
8 changes: 4 additions & 4 deletions internal/display/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (d *display) whileReading(input byte, quitC chan bool) {
if d.currentSection == URLS_LIST {
if _, err := d.fetchFeed(string(d.raw[d.currentRow()])); err != nil {
log.Default().Println(err)
d.setTmpBottomMessage(3*time.Second, "cannot parse feed!")
d.setTmpBottomMessage(2*time.Second, "cannot parse feed!")
return
}
}
Expand All @@ -164,7 +164,7 @@ func (d *display) whileReading(input byte, quitC chan bool) {
if d.currentSection == ARTICLES_LIST {
if !util.IsHeadless() {
if err := util.OpenWithBrowser(string(d.raw[d.currentRow()])); err != nil {
d.setTmpBottomMessage(1*time.Second, err.Error())
d.setTmpBottomMessage(2*time.Second, err.Error())
}
}
}
Expand All @@ -173,7 +173,7 @@ func (d *display) whileReading(input byte, quitC chan bool) {
if d.currentSection == ARTICLES_LIST {
if util.IsLynxPresent() {
if err := util.OpenWithLynx(string(d.raw[d.currentRow()])); err != nil {
d.setTmpBottomMessage(1*time.Second, err.Error())
d.setTmpBottomMessage(2*time.Second, err.Error())
}
}
}
Expand Down Expand Up @@ -303,7 +303,7 @@ func (d *display) whileEditing(input byte) {
case input == QUIT:
{
d.setBottomMessage(urlsListSectionMsg)
d.setTmpBottomMessage(1*time.Second, "editing aborted!")
d.setTmpBottomMessage(2*time.Second, "editing aborted!")
d.exitEditingMode()
d.resetCurrentPos()
}
Expand Down
21 changes: 10 additions & 11 deletions internal/display/loading.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ func (d *display) fetchFeed(url string) (*feed.Feed, error) {
parsedFeed, err := d.parser.Parse(url)
if err != nil {
log.Default().Println(err)
d.setTmpBottomMessage(3*time.Second, "cannot parse feed!")
d.setTmpBottomMessage(2*time.Second, "cannot parse feed!")
return nil, err
}

if err := d.cache.AddFeed(parsedFeed, url); err != nil {
log.Default().Println(err)
d.setTmpBottomMessage(3*time.Second, fmt.Sprintf("cannot load feed from url: %s", url))
d.setTmpBottomMessage(2*time.Second, fmt.Sprintf("cannot load feed from url: %s", url))
return nil, err
}

Expand Down Expand Up @@ -112,7 +112,7 @@ func (d *display) fetchAllFeeds() {
}

if err := g.Wait(); err != nil {
d.setTmpBottomMessage(3*time.Second, "cannot reload all feeds!")
d.setTmpBottomMessage(2*time.Second, "cannot reload all feeds!")
} else {
go func() {
if err := d.cache.Encode(); err != nil {
Expand All @@ -137,7 +137,7 @@ func (d *display) loadArticleList(url string) error {
found = true

if len(cachedFeed.Items) == 0 {
d.setTmpBottomMessage(3*time.Second, "feed not yet loaded: press r!")
d.setTmpBottomMessage(2*time.Second, "feed not yet loaded: press r!")
return fmt.Errorf("feed not loaded")
}

Expand Down Expand Up @@ -173,7 +173,7 @@ func (d *display) loadArticleList(url string) error {
}
}
if !found {
d.setTmpBottomMessage(3*time.Second, "feed not yet loaded: press r!")
d.setTmpBottomMessage(2*time.Second, "feed not yet loaded: press r!")
return fmt.Errorf("cannot find articles of feed with url: %s", url)
}
return nil
Expand All @@ -195,7 +195,7 @@ func (d *display) loadArticleText(url string) error {
text, err := html.ExtractText(i.Url)
if err != nil {
log.Default().Println(err)
d.setTmpBottomMessage(3*time.Second, fmt.Sprintf("cannot load article from url: %s", url))
d.setTmpBottomMessage(2*time.Second, fmt.Sprintf("cannot load article from url: %s", url))
return fmt.Errorf("cannot load aricle")
}

Expand Down Expand Up @@ -239,27 +239,26 @@ func (d *display) addNewFeed() {

for _, f := range d.config.Feeds {
if f.Url == url {
d.setTmpBottomMessage(3*time.Second, "already added!")
d.setTmpBottomMessage(2*time.Second, "already added!")
return
}
}

parsedFeed, err := d.fetchFeed(url)
if err != nil {
log.Default().Println(err)
d.setTmpBottomMessage(3*time.Second, "cannot parse feed!")
return
}

if err := d.config.AddFeed(parsedFeed, url); err != nil {
log.Default().Println(err)
d.setTmpBottomMessage(3*time.Second, "cannot add new feed to config!")
d.setTmpBottomMessage(2*time.Second, "cannot add new feed to config!")
return
}

if err := d.config.Encode(); err != nil {
log.Default().Println(err)
d.setTmpBottomMessage(3*time.Second, "cannot write new feed to config!")
d.setTmpBottomMessage(2*time.Second, "cannot write new feed to config!")
return
}

Expand All @@ -270,6 +269,6 @@ func (d *display) addNewFeed() {
d.current.startoff = (len(d.raw) - 1) / d.getContentWindowLen() * d.getContentWindowLen()

d.setBottomMessage(urlsListSectionMsg)
d.setTmpBottomMessage(3*time.Second, "new feed saved!")
d.setTmpBottomMessage(2*time.Second, "new feed saved!")
d.exitEditingMode()
}
2 changes: 1 addition & 1 deletion internal/display/rendering.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (d *display) renderArticleList() {
}
}
} else {
d.setTmpBottomMessage(1*time.Second, "cannot load article list!")
d.setTmpBottomMessage(2*time.Second, "cannot load article list!")
}
}

Expand Down

0 comments on commit 72fb0fb

Please sign in to comment.