Skip to content

Commit

Permalink
add city and fix tests (#205)
Browse files Browse the repository at this point in the history
* add city and fix tests

* add city test
  • Loading branch information
askovpen authored Sep 5, 2024
1 parent 169a3ba commit 637b6ed
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ builds:
- darwin
goarch:
- amd64
- arm64
- id: fbsd
env:
- CGO_ENABLED=0
Expand All @@ -58,6 +59,7 @@ archives:
files:
- gossiped.example.yml
- gossiped.tpl
- city.yml
- colors/*.yml
format_overrides:
- goos: windows
Expand Down
1 change: 1 addition & 0 deletions gossiped.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ sorting:
areas: unread # unread, default
statusbar:
clock: true
citypath: ./city.yml
14 changes: 8 additions & 6 deletions pkg/config/city.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package config

import (
"os"

"github.com/askovpen/gossiped/pkg/types"
"gopkg.in/yaml.v3"
"io/ioutil"
)

func readCity() {
yamlFile, err := ioutil.ReadFile("city.yaml")
yamlFile, err := os.ReadFile(Config.CityPath)
if err != nil {
return
panic(err)
}
err = yaml.Unmarshal(yamlFile, &city)
if err != nil {
return
panic(err)
}
}

// GetCity return city
func GetCity(sa string) string {
if val, ok := city[sa]; ok {
func GetCity(sfa *types.FidoAddr) string {
if val, ok := city[sfa.ShortString()]; ok {
return val
}
return "unknown"
Expand Down
27 changes: 27 additions & 0 deletions pkg/config/city_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package config

import (
"testing"

"github.com/askovpen/gossiped/pkg/types"
. "github.com/franela/goblin"
)

func TestGetCity(t *testing.T) {
err := Read("../../gossiped.example.yml")
if err != nil {
panic(err)
}
g := Goblin(t)
g.Describe("Check City", func() {
g.It("check GetCity() Moscow", func() {
g.Assert(GetCity(types.AddrFromString("2:5020/9696"))).Equal("Moscow Russia")
})
g.It("check GetCity() Gomel", func() {
g.Assert(GetCity(types.AddrFromString("2:452/28.1"))).Equal("Gomel")
})
g.It("check GetCity() Unknown", func() {
g.Assert(GetCity(types.AddrFromString("2:1020/9696"))).Equal("unknown")
})
})
}
8 changes: 5 additions & 3 deletions pkg/config/colorscheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package config
import (
"errors"
"fmt"
"github.com/gdamore/tcell/v2"
"gopkg.in/yaml.v3"
"log"
"os"
"strconv"
"strings"

"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"gopkg.in/yaml.v3"
)

const (
Expand Down Expand Up @@ -301,7 +303,7 @@ func GetElementStyle(section string, element string) tcell.Style {

func FormatTextWithStyle(text string, style tcell.Style) string {
fg, bg, attrs := style.Decompose()
return fmt.Sprintf("[%s:%s:%s]%s", fg.String(), bg.String(), MaskToStringStyle(attrs), text)
return fmt.Sprintf("[%s:%s:%s]%s", fg.String(), bg.String(), MaskToStringStyle(attrs), tview.Escape(text))
}

// initColorAliases()
Expand Down
25 changes: 13 additions & 12 deletions pkg/config/colorscheme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package config

import (
"fmt"
. "github.com/franela/goblin"
"github.com/gdamore/tcell/v2"
"regexp"
"strings"
"testing"

. "github.com/franela/goblin"
"github.com/gdamore/tcell/v2"
)

var (
Expand Down Expand Up @@ -106,16 +107,16 @@ func TestStringToStyle(t *testing.T) {
g.Describe("Check StringToStyle", func() {
g.It("check expected success conversion (fg, bg, styles)", func() {
var testData = map[string]tcell.Style{
"default": tcell.StyleDefault,
"black,white": tcell.Style{}.Foreground(tcell.ColorBlack).Background(tcell.ColorWhite),
"orange, red": tcell.Style{}.Foreground(tcell.ColorOrange).Background(tcell.ColorRed),
"yellow, blue": tcell.Style{}.Foreground(tcell.ColorYellow).Background(tcell.ColorBlue),
"underline default,default": tcell.Style{}.Underline(true),
"bold default,default": tcell.Style{}.Bold(true),
"bold|reverse|underline default,default": tcell.Style{}.Bold(true).Underline(true).Reverse(true),
"reverse yellow,red": tcell.Style{}.Reverse(true).Foreground(tcell.ColorYellow).Background(tcell.ColorRed),
"bold 201,114": tcell.Style{}.Foreground(tcell.Color201).Background(tcell.Color114).Bold(true),
"299,294": tcell.Style{}.Foreground(tcell.ColorDefault).Background(tcell.ColorDefault),
"default": StyleDefault,
"black,white": StyleDefault.Foreground(tcell.ColorBlack).Background(tcell.ColorWhite),
"orange, red": StyleDefault.Foreground(tcell.ColorOrange).Background(tcell.ColorRed),
"yellow, blue": StyleDefault.Foreground(tcell.ColorYellow).Background(tcell.ColorBlue),
"underline default,default": StyleDefault.Underline(true),
"bold default,default": StyleDefault.Bold(true),
"bold|reverse|underline default,default": StyleDefault.Bold(true).Underline(true).Reverse(true),
"reverse yellow,red": StyleDefault.Reverse(true).Foreground(tcell.ColorYellow).Background(tcell.ColorRed),
"bold 201,114": StyleDefault.Foreground(tcell.Color201).Background(tcell.Color114).Bold(true),
"299,294": StyleDefault.Foreground(tcell.ColorDefault).Background(tcell.ColorDefault),
}
for from, to := range testData {
expected, _ := StringToStyle(from)
Expand Down
30 changes: 25 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package config

import (
"errors"
"github.com/askovpen/gossiped/pkg/types"
"github.com/gdamore/tcell/v2"
"gopkg.in/yaml.v3"
"os"
"path"
"path/filepath"
"runtime"
"strings"

"github.com/askovpen/gossiped/pkg/types"
"github.com/gdamore/tcell/v2"
"gopkg.in/yaml.v3"
)

type (
Expand Down Expand Up @@ -39,8 +42,9 @@ type (
Statusbar struct {
Clock bool
}
Sorting SortTypeMap
Colors map[string]ColorMap
Sorting SortTypeMap
Colors map[string]ColorMap
CityPath string
}
)

Expand All @@ -60,13 +64,24 @@ func InitVars() {
PID = "gossipEd+" + runtime.GOOS[0:3] + " " + Version
LongPID = "gossipEd-" + runtime.GOOS + "/" + runtime.GOARCH + " " + Version
}
func tryPath(rootPath string, filePath string) string {
if _, err := os.Stat(filePath); err == nil {
return filePath
}
if _, err := os.Stat(path.Join(rootPath, filePath)); err == nil {
return path.Join(rootPath, filePath)
}
return ""
}

// Read config
func Read(fn string) error {
yamlFile, err := os.ReadFile(fn)
if err != nil {
return err
}
rootPath := filepath.Dir(fn)

err = yaml.Unmarshal(yamlFile, &Config)
if err != nil {
return err
Expand All @@ -77,6 +92,7 @@ func Read(fn string) error {
if Config.Chrs.Default == "" {
return errors.New("Config.Chrs.Default not defined")
}
Config.Template = tryPath(rootPath, Config.Template)
tpl, err := os.ReadFile(Config.Template)
if err != nil {
return err
Expand All @@ -89,6 +105,10 @@ func Read(fn string) error {
if errColors != nil {
return errColors
}
if Config.CityPath == "" {
return errors.New("Config.CityPath not defined")
}
Config.CityPath = tryPath(rootPath, Config.CityPath)
readCity()
return nil
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/ui/viewheader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package ui

import (
"fmt"
"strings"

"github.com/askovpen/gossiped/pkg/config"
"github.com/askovpen/gossiped/pkg/msgapi"
"github.com/askovpen/gossiped/pkg/utils"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
//"github.com/mattn/go-runewidth"
"strings"
)

// ViewHeader widget
Expand Down Expand Up @@ -40,6 +40,7 @@ func NewViewHeader(msg *msgapi.Message) *ViewHeader {
if len(msg.Attrs) > 0 {
repl += "[" + strings.Join(msg.Attrs, " ") + "]"
}
repl += " [" + config.GetCity(msg.FromAddr) + "]"
si = [10][]rune{
[]rune(fmt.Sprintf("%d", msg.MsgNum)),
[]rune(fmt.Sprintf("%d", msgapi.Areas[msgapi.Lookup(msg.Area)].GetCount())),
Expand Down

0 comments on commit 637b6ed

Please sign in to comment.