~chrisppy/beagles

dbc3b58fff71583a1064258c352bf84df8fec2cb — Chris Palmer 1 year, 4 months ago 4c96985
More structure refactor in ui and update README
7 files changed, 289 insertions(+), 201 deletions(-)

M README.md
M ui/content.go
A ui/helpPage.go
A ui/listPage.go
A ui/navigation.go
A ui/status.go
M ui/ui.go
M README.md => README.md +1 -1
@@ 12,7 12,7 @@ see :help in the application or beagles(1)

Install the dependencies:

- go (>=1.13)
- go (>=1.14)
- scdoc

Then compile beagles:

M ui/content.go => ui/content.go +1 -1
@@ 93,7 93,7 @@ func (w *content) setText(item *model.Item) {
		t, err := ht.FromString(c, options)
		if err != nil {
			w.Widget.SetTextColor(tcell.GetColor(w.ErrColor))
			text = err.Error()
			t = err.Error()
		}
		text = t
	}

A ui/helpPage.go => ui/helpPage.go +112 -0
@@ 0,0 1,112 @@
// This file is part of beagles.
//
// Copyright © 2020 Chris Palmer <chris@red-oxide.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package ui

import (
	"fmt"

	"git.sr.ht/~chrisppy/beagles/config"
	"github.com/gdamore/tcell"
	tui "gitlab.com/tslocum/cview"
)

func (i *UI) createHelpPage(config *config.Config) {
	text := `beagles v%s is available under the terms of the GPL-3.0 license.
Copyright © 2020  Chris Palmer <chris@red-oxide.org>
https://git.sr.ht/~chrisppy/beagles

KEYS:
	h,🠔
		move to the left section (if applicable)
	
	j,🠗
		navigate down in the current section (if applicable)
	
	k,🠕
		navigate up in the current section (if applicable)
	
	l,🠖
		move the the right section (if applicable)
	
	m
		mark current post as read when in --LIST-- or --CONTENT--

	n
		open post url when in --LIST-- or --CONTENT--

	:
		enter command mode

COMMANDS:
	add [url]
		add a feed

	remove,rm [url]
		remove the feed

	update,up
		update all feeds

	help,h,?
		display help page

	list
		display list page
	
	quit,q
		exit the application

SEE ALSO:
	Additional information can be found in the manual pages:
		beagles(1)
		beagles-config(5)
`

	h := tui.NewTextView()
	h.SetText(fmt.Sprintf(text, i.Version))
	h.SetTextAlign(tui.AlignLeft)

	bgColor := config.BackgroundColor
	fgColor := config.ForegroundColor
	if config.Help != nil {
		bgColor = config.Help.BackgroundColor
		fgColor = config.Help.ForegroundColor
	}

	if bgColor == "transparent" {
		h.SetBackgroundColor(tcell.ColorDefault)
	} else {
		h.SetBackgroundColor(tcell.GetColor(bgColor))
	}

	h.SetTextColor(tcell.GetColor(fgColor))

	cv := tui.NewFlex()
	cv.SetDirection(tui.FlexRow)
	cv.AddItem(newTitleLine(config), 1, 0, false)
	cv.AddItem(h, 0, 1, false)
	cv.AddItem(i.statusLine.Widget, 1, 1, false)
	cv.AddItem(i.commandLine.Widget, 1, 0, false)

	view := tui.NewFlex()
	view.AddItem(cv, 0, 1, false)

	resize := true
	visable := true
	i.pages.AddPage("help", view, resize, visable)
}

A ui/listPage.go => ui/listPage.go +45 -0
@@ 0,0 1,45 @@
// This file is part of beagles.
//
// Copyright © 2020 Chris Palmer <chris@red-oxide.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package ui

import (
	"git.sr.ht/~chrisppy/beagles/config"
	tui "gitlab.com/tslocum/cview"
)

func (i *UI) createListPage(config *config.Config) {
	qc := tui.NewFlex()
	qc.SetDirection(tui.FlexColumn)
	qc.AddItem(i.list.Widget, 0, 3, false)
	qc.AddItem(newSeparator(config, true), 1, 0, false)
	qc.AddItem(i.content.Widget, 0, 6, false)

	cv := tui.NewFlex()
	cv.SetDirection(tui.FlexRow)
	cv.AddItem(newTitleLine(config), 1, 0, false)
	cv.AddItem(qc, 0, 1, false)
	cv.AddItem(i.statusLine.Widget, 1, 1, false)
	cv.AddItem(i.commandLine.Widget, 1, 0, false)

	view := tui.NewFlex()
	view.AddItem(cv, 0, 1, false)

	resize := true
	visable := true
	i.pages.AddPage("list", view, resize, visable)
}

A ui/navigation.go => ui/navigation.go +79 -0
@@ 0,0 1,79 @@
// This file is part of beagles.
//
// Copyright © 2020 Chris Palmer <chris@red-oxide.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package ui

import (
	"github.com/gdamore/tcell"
)

func (i *UI) moveLeft(event *tcell.EventKey) *tcell.EventKey {
	switch i.status {
	case contentStatus:
		if i.inCmdMode {
			return event
		}
		i.setStatus(listStatus)

		return nil
	}
	return event
}
func (i *UI) moveRight(event *tcell.EventKey) *tcell.EventKey {
	switch i.status {
	case listStatus:
		if i.inCmdMode {
			return event
		}

		i.setStatus(contentStatus)

		return nil
	}
	return event
}
func (i *UI) moveUp(event *tcell.EventKey) *tcell.EventKey {
	switch i.status {
	case listStatus:
		if i.inCmdMode {
			return event
		}

		index := i.list.decrement()
		if index != -1 {
			i.content.setText(&i.DB.Queue[index].Item)
		}

		return nil
	}
	return event
}
func (i *UI) moveDown(event *tcell.EventKey) *tcell.EventKey {
	switch i.status {
	case listStatus:
		if i.inCmdMode {
			return event
		}
		index := i.list.increment()
		if index != -1 {
			i.content.setText(&i.DB.Queue[index].Item)
		}

		return nil
	}
	return event
}

A ui/status.go => ui/status.go +51 -0
@@ 0,0 1,51 @@
// This file is part of beagles.
//
// Copyright © 2020 Chris Palmer <chris@red-oxide.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package ui

type statusType int

const (
	listStatus statusType = iota
	contentStatus
	commandStatus
	helpStatus
)

func (i *UI) setStatus(s statusType) {
	switch s {
	case commandStatus:
		i.inCmdMode = true
		i.commandLine.setText(":")
		i.statusLine.setText("-- COMMAND --")
		i.app.switchTo(i.commandLine.Widget)
		return
	case contentStatus:
		i.inCmdMode = false
		i.statusLine.setText("-- CONTENT --")
		i.app.switchTo(i.content.Widget)
	case helpStatus:
		i.inCmdMode = false
		i.statusLine.setText("-- HELP --")
		i.pages.SwitchToPage("help")
	default:
		i.inCmdMode = false
		i.statusLine.setText("-- LIST --")
		i.pages.SwitchToPage("list")
	}
	i.status = s
}

M ui/ui.go => ui/ui.go +0 -199
@@ 18,7 18,6 @@
package ui

import (
	"fmt"
	"strings"

	"git.sr.ht/~chrisppy/beagles/config"


@@ 28,15 27,6 @@ import (
	tui "gitlab.com/tslocum/cview"
)

type statusType int

const (
	listStatus statusType = iota
	contentStatus
	commandStatus
	helpStatus
)

// UI is the structure containing all the ui elements, and the link to the
// database and config
type UI struct {


@@ 266,192 256,3 @@ func (i *UI) markRead(event *tcell.EventKey) *tcell.EventKey {

	return event
}

func (i *UI) moveLeft(event *tcell.EventKey) *tcell.EventKey {
	switch i.status {
	case contentStatus:
		if i.inCmdMode {
			return event
		}
		i.setStatus(listStatus)

		return nil
	}
	return event
}
func (i *UI) moveRight(event *tcell.EventKey) *tcell.EventKey {
	switch i.status {
	case listStatus:
		if i.inCmdMode {
			return event
		}

		i.setStatus(contentStatus)

		return nil
	}
	return event
}
func (i *UI) moveUp(event *tcell.EventKey) *tcell.EventKey {
	switch i.status {
	case listStatus:
		if i.inCmdMode {
			return event
		}

		index := i.list.decrement()
		if index != -1 {
			i.content.setText(&i.DB.Queue[index].Item)
		}

		return nil
	}
	return event
}
func (i *UI) moveDown(event *tcell.EventKey) *tcell.EventKey {
	switch i.status {
	case listStatus:
		if i.inCmdMode {
			return event
		}
		index := i.list.increment()
		if index != -1 {
			i.content.setText(&i.DB.Queue[index].Item)
		}

		return nil
	}
	return event
}

func (i *UI) createListPage(config *config.Config) {
	qc := tui.NewFlex()
	qc.SetDirection(tui.FlexColumn)
	qc.AddItem(i.list.Widget, 0, 3, false)
	qc.AddItem(newSeparator(config, true), 1, 0, false)
	qc.AddItem(i.content.Widget, 0, 6, false)

	cv := tui.NewFlex()
	cv.SetDirection(tui.FlexRow)
	cv.AddItem(newTitleLine(config), 1, 0, false)
	cv.AddItem(qc, 0, 1, false)
	cv.AddItem(i.statusLine.Widget, 1, 1, false)
	cv.AddItem(i.commandLine.Widget, 1, 0, false)

	view := tui.NewFlex()
	view.AddItem(cv, 0, 1, false)

	resize := true
	visable := true
	i.pages.AddPage("list", view, resize, visable)
}

func (i *UI) createHelpPage(config *config.Config) {
	text := `beagles v%s is available under the terms of the GPL-3.0 license.
Copyright © 2020  Chris Palmer <chris@red-oxide.org>
https://git.sr.ht/~chrisppy/beagles

KEYS:
	h,🠔
		move to the left section (if applicable)
	
	j,🠗
		navigate down in the current section (if applicable)
	
	k,🠕
		navigate up in the current section (if applicable)
	
	l,🠖
		move the the right section (if applicable)
	
	m
		mark current post as read when in --LIST-- or --CONTENT--

	n
		open post url when in --LIST-- or --CONTENT--

	:
		enter command mode

COMMANDS:
	add [url]
		add a feed

	remove,rm [url]
		remove the feed

	update,up
		update all feeds

	help,h,?
		display help page

	list
		display list page
	
	quit,q
		exit the application

SEE ALSO:
	Additional information can be found in the manual pages:
		beagles(1)
		beagles-config(5)
`

	h := tui.NewTextView()
	h.SetText(fmt.Sprintf(text, i.Version))
	h.SetTextAlign(tui.AlignLeft)

	bgColor := config.BackgroundColor
	fgColor := config.ForegroundColor
	if config.Help != nil {
		bgColor = config.Help.BackgroundColor
		fgColor = config.Help.ForegroundColor
	}

	if bgColor == "transparent" {
		h.SetBackgroundColor(tcell.ColorDefault)
	} else {
		h.SetBackgroundColor(tcell.GetColor(bgColor))
	}

	h.SetTextColor(tcell.GetColor(fgColor))

	cv := tui.NewFlex()
	cv.SetDirection(tui.FlexRow)
	cv.AddItem(newTitleLine(config), 1, 0, false)
	cv.AddItem(h, 0, 1, false)
	cv.AddItem(i.statusLine.Widget, 1, 1, false)
	cv.AddItem(i.commandLine.Widget, 1, 0, false)

	view := tui.NewFlex()
	view.AddItem(cv, 0, 1, false)

	resize := true
	visable := true
	i.pages.AddPage("help", view, resize, visable)
}

func (i *UI) setStatus(s statusType) {
	switch s {
	case commandStatus:
		i.inCmdMode = true
		i.commandLine.setText(":")
		i.statusLine.setText("-- COMMAND --")
		i.app.switchTo(i.commandLine.Widget)
		return
	case contentStatus:
		i.inCmdMode = false
		i.statusLine.setText("-- CONTENT --")
		i.app.switchTo(i.content.Widget)
	case helpStatus:
		i.inCmdMode = false
		i.statusLine.setText("-- HELP --")
		i.pages.SwitchToPage("help")
	default:
		i.inCmdMode = false
		i.statusLine.setText("-- LIST --")
		i.pages.SwitchToPage("list")
	}
	i.status = s
}