// Code generated by mockery; DO NOT EDIT.
// github.com/vektra/mockery
// template: testify
package mocks
import (
mock "github.com/stretchr/testify/mock"
)
// NewMockData creates a new instance of MockData. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
// The first argument is typically a *testing.T value.
func NewMockData(t interface {
mock.TestingT
Cleanup(func())
}) *MockData {
mock := &MockData{}
mock.Mock.Test(t)
t.Cleanup(func() { mock.AssertExpectations(t) })
return mock
}
// MockData is an autogenerated mock type for the Data type
type MockData struct {
mock.Mock
}
type MockData_Expecter struct {
mock *mock.Mock
}
func (_m *MockData) EXPECT() *MockData_Expecter {
return &MockData_Expecter{mock: &_m.Mock}
}
// BackgroundColor provides a mock function for the type MockData
func (_mock *MockData) BackgroundColor() string {
ret := _mock.Called()
if len(ret) == 0 {
panic("no return value specified for BackgroundColor")
}
var r0 string
if returnFunc, ok := ret.Get(0).(func() string); ok {
r0 = returnFunc()
} else {
r0 = ret.Get(0).(string)
}
return r0
}
// MockData_BackgroundColor_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'BackgroundColor'
type MockData_BackgroundColor_Call struct {
*mock.Call
}
// BackgroundColor is a helper method to define mock.On call
func (_e *MockData_Expecter) BackgroundColor() *MockData_BackgroundColor_Call {
return &MockData_BackgroundColor_Call{Call: _e.mock.On("BackgroundColor")}
}
func (_c *MockData_BackgroundColor_Call) Run(run func()) *MockData_BackgroundColor_Call {
_c.Call.Run(func(args mock.Arguments) {
run()
})
return _c
}
func (_c *MockData_BackgroundColor_Call) Return(s string) *MockData_BackgroundColor_Call {
_c.Call.Return(s)
return _c
}
func (_c *MockData_BackgroundColor_Call) RunAndReturn(run func() string) *MockData_BackgroundColor_Call {
_c.Call.Return(run)
return _c
}
// ForegroundColor provides a mock function for the type MockData
func (_mock *MockData) ForegroundColor() string {
ret := _mock.Called()
if len(ret) == 0 {
panic("no return value specified for ForegroundColor")
}
var r0 string
if returnFunc, ok := ret.Get(0).(func() string); ok {
r0 = returnFunc()
} else {
r0 = ret.Get(0).(string)
}
return r0
}
// MockData_ForegroundColor_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ForegroundColor'
type MockData_ForegroundColor_Call struct {
*mock.Call
}
// ForegroundColor is a helper method to define mock.On call
func (_e *MockData_Expecter) ForegroundColor() *MockData_ForegroundColor_Call {
return &MockData_ForegroundColor_Call{Call: _e.mock.On("ForegroundColor")}
}
func (_c *MockData_ForegroundColor_Call) Run(run func()) *MockData_ForegroundColor_Call {
_c.Call.Run(func(args mock.Arguments) {
run()
})
return _c
}
func (_c *MockData_ForegroundColor_Call) Return(s string) *MockData_ForegroundColor_Call {
_c.Call.Return(s)
return _c
}
func (_c *MockData_ForegroundColor_Call) RunAndReturn(run func() string) *MockData_ForegroundColor_Call {
_c.Call.Return(run)
return _c
}
// Label provides a mock function for the type MockData
func (_mock *MockData) Label() string {
ret := _mock.Called()
if len(ret) == 0 {
panic("no return value specified for Label")
}
var r0 string
if returnFunc, ok := ret.Get(0).(func() string); ok {
r0 = returnFunc()
} else {
r0 = ret.Get(0).(string)
}
return r0
}
// MockData_Label_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Label'
type MockData_Label_Call struct {
*mock.Call
}
// Label is a helper method to define mock.On call
func (_e *MockData_Expecter) Label() *MockData_Label_Call {
return &MockData_Label_Call{Call: _e.mock.On("Label")}
}
func (_c *MockData_Label_Call) Run(run func()) *MockData_Label_Call {
_c.Call.Run(func(args mock.Arguments) {
run()
})
return _c
}
func (_c *MockData_Label_Call) Return(s string) *MockData_Label_Call {
_c.Call.Return(s)
return _c
}
func (_c *MockData_Label_Call) RunAndReturn(run func() string) *MockData_Label_Call {
_c.Call.Return(run)
return _c
}
// Long provides a mock function for the type MockData
func (_mock *MockData) Long() string {
ret := _mock.Called()
if len(ret) == 0 {
panic("no return value specified for Long")
}
var r0 string
if returnFunc, ok := ret.Get(0).(func() string); ok {
r0 = returnFunc()
} else {
r0 = ret.Get(0).(string)
}
return r0
}
// MockData_Long_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Long'
type MockData_Long_Call struct {
*mock.Call
}
// Long is a helper method to define mock.On call
func (_e *MockData_Expecter) Long() *MockData_Long_Call {
return &MockData_Long_Call{Call: _e.mock.On("Long")}
}
func (_c *MockData_Long_Call) Run(run func()) *MockData_Long_Call {
_c.Call.Run(func(args mock.Arguments) {
run()
})
return _c
}
func (_c *MockData_Long_Call) Return(s string) *MockData_Long_Call {
_c.Call.Return(s)
return _c
}
func (_c *MockData_Long_Call) RunAndReturn(run func() string) *MockData_Long_Call {
_c.Call.Return(run)
return _c
}
// Short provides a mock function for the type MockData
func (_mock *MockData) Short() string {
ret := _mock.Called()
if len(ret) == 0 {
panic("no return value specified for Short")
}
var r0 string
if returnFunc, ok := ret.Get(0).(func() string); ok {
r0 = returnFunc()
} else {
r0 = ret.Get(0).(string)
}
return r0
}
// MockData_Short_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Short'
type MockData_Short_Call struct {
*mock.Call
}
// Short is a helper method to define mock.On call
func (_e *MockData_Expecter) Short() *MockData_Short_Call {
return &MockData_Short_Call{Call: _e.mock.On("Short")}
}
func (_c *MockData_Short_Call) Run(run func()) *MockData_Short_Call {
_c.Call.Run(func(args mock.Arguments) {
run()
})
return _c
}
func (_c *MockData_Short_Call) Return(s string) *MockData_Short_Call {
_c.Call.Return(s)
return _c
}
func (_c *MockData_Short_Call) RunAndReturn(run func() string) *MockData_Short_Call {
_c.Call.Return(run)
return _c
}
package configure
import (
"os"
"github.com/spetix/bar-out-adapters/internal/events"
"github.com/spetix/bar-out-adapters/internal/eventstrategy"
"github.com/spetix/bar-out-adapters/internal/protocols"
"github.com/spetix/bar-out-adapters/internal/render"
"github.com/spetix/bar-out-adapters/pkg/barout/models"
"github.com/spf13/cobra"
)
type SetupBlockletImpl struct {
models.SetupBlocklet
options *render.RenderOptionsImpl
protocol protocols.BlockletProtocol
formatter models.Formatter
eventMgr *events.EventManagerImpl
}
func NewSetupBlockletImpl(f models.Formatter) *SetupBlockletImpl {
return &SetupBlockletImpl{
options: render.NewRenderOptionsImpl(),
formatter: f,
protocol: protocols.ProtocolRaw,
eventMgr: events.NewEventManagerImpl(),
}
}
func (s *SetupBlockletImpl) Setup(cmd *cobra.Command) {
if proto := os.Getenv("protocol"); proto != "" {
s.protocol.Set(proto)
}
cmd.PersistentFlags().Var(&s.protocol, "protocol", "output protocol (raw, i3blocks, waybar, json)")
if txtFmt := os.Getenv("format"); txtFmt != "" {
s.options.SetFormat(&txtFmt)
}
s.options.SetFormat(cmd.PersistentFlags().String("format", s.options.Format(), "format"))
if envLbl := os.Getenv("label"); envLbl != "" {
s.options.SetLabel(&envLbl)
}
s.options.SetLabel(cmd.PersistentFlags().String("label", s.options.Label(), "label"))
if envClr := os.Getenv("color"); envClr != "" {
s.options.SetForegroundColor(&envClr)
}
s.options.SetForegroundColor(cmd.PersistentFlags().String("color", s.options.ForegroundColor(), "color"))
if envBgClr := os.Getenv("bgcolor"); envBgClr != "" {
s.options.SetBackgroundColor(&envBgClr)
}
s.options.SetBackgroundColor(cmd.PersistentFlags().String("bgcolor", s.options.BackgroundColor(), "background color"))
}
// New creates a new BlockletOutput based on the provided protocol string.
// It supports "json", "raw", and "waybar" protocols. If an unsupported protocol is provided, it defaults to "raw".
func (s *SetupBlockletImpl) GetOutput() models.BlockletOutput {
switch s.protocol {
case protocols.ProtocolRaw, protocols.ProtocolI3Blocks:
return protocols.NewRawOut(os.Stdout)
case protocols.ProtocolWaybar:
return protocols.NewWaybarOut(os.Stdout)
case protocols.ProtocolJson:
return protocols.NewJsonOut(os.Stdout)
default:
return protocols.NewRawOut(os.Stdout)
}
}
func (s *SetupBlockletImpl) EventManager() models.EventManager {
switch s.protocol {
case protocols.ProtocolRaw, protocols.ProtocolI3Blocks:
eventstrategy.NewEnvVarStrategy(s.eventMgr)
case protocols.ProtocolWaybar:
eventstrategy.NewNopStrategy(s.eventMgr)
default:
eventstrategy.NewNopStrategy(s.eventMgr)
}
return s.eventMgr
}
func (s *SetupBlockletImpl) Options() models.RenderOptions {
return s.options
}
package events
import (
"github.com/spetix/bar-out-adapters/pkg/barout/models"
)
type EventImpl struct {
name models.EventType
signal int
}
func (e *EventImpl) Name() models.EventType {
return e.name
}
func (e *EventImpl) Signal() int {
return e.signal
}
type EventManagerImpl struct {
handlers models.EventHandlers
strategy models.EventStrategy
}
func NewEventManagerImpl() *EventManagerImpl {
return &EventManagerImpl{
handlers: make(models.EventHandlers, 0),
}
}
func (e *EventManagerImpl) Register(ev models.EventType, hdlr models.EventHdlr) {
if _, exists := e.handlers[ev]; exists {
return
}
e.handlers[ev] = hdlr
}
func (e *EventManagerImpl) Handlers() models.EventHandlers {
return e.handlers
}
func (e *EventManagerImpl) SetStrategy(strategy models.EventStrategy) {
e.strategy = strategy
}
func (e *EventManagerImpl) Run() {
if e.strategy != nil {
e.strategy.Execute()
}
}
package eventstrategy
import (
"os"
"github.com/spetix/bar-out-adapters/internal/events"
"github.com/spetix/bar-out-adapters/pkg/barout/models"
)
type EnvVarStrategy struct {
hdlrs models.EventHandlers
}
func NewEnvVarStrategy(e *events.EventManagerImpl) {
e.SetStrategy(&EnvVarStrategy{
hdlrs: e.Handlers(),
})
}
func (e *EnvVarStrategy) Execute() {
if ev := os.Getenv("BUTTON_BLOCK"); ev != "" {
switch ev {
case models.LeftButton.String():
if hdlr, exists := e.hdlrs[models.LeftButton]; exists {
hdlr()
}
case models.MiddleButton.String():
if hdlr, exists := e.hdlrs[models.MiddleButton]; exists {
hdlr()
}
case models.RightButton.String():
if hdlr, exists := e.hdlrs[models.RightButton]; exists {
hdlr()
}
case models.ScrollUp.String():
if hdlr, exists := e.hdlrs[models.ScrollUp]; exists {
hdlr()
}
case models.ScrollDown.String():
if hdlr, exists := e.hdlrs[models.ScrollDown]; exists {
hdlr()
}
default:
return
}
}
}
package eventstrategy
import (
"github.com/spetix/bar-out-adapters/internal/events"
)
type NopStrategy struct{}
func NewNopStrategy(e *events.EventManagerImpl) {
e.SetStrategy(&NopStrategy{})
}
func (e *NopStrategy) Execute() {
// Do nothing
}
package protocols
import "fmt"
type BlockletProtocol string
const (
ProtocolRaw BlockletProtocol = "raw"
ProtocolI3Blocks BlockletProtocol = "i3blocks"
ProtocolWaybar BlockletProtocol = "waybar"
ProtocolJson BlockletProtocol = "json"
)
func (p BlockletProtocol) String() string {
return string(p)
}
func (p BlockletProtocol) Type() string {
return "BlockletProtocol"
}
func (p *BlockletProtocol) Set(protoStr string) error {
switch protoStr {
case "raw":
*p = ProtocolRaw
case "i3blocks":
*p = ProtocolI3Blocks
case "waybar":
*p = ProtocolWaybar
case "json":
*p = ProtocolJson
default:
*p = ProtocolRaw
return fmt.Errorf("Invalid protocol %s", protoStr)
}
return nil
}
package protocols
import (
"io"
"os"
"github.com/spetix/bar-out-adapters/pkg/barout/models"
)
type I3BlocksOut struct {
baseOutput
}
func NewI3BlocksOut(device *os.File) *I3BlocksOut {
return &I3BlocksOut{
baseOutput: baseOutput{
Device: device,
},
}
}
func (i *I3BlocksOut) Print(d models.Data) {
writer := io.Writer(i.Device)
//defer io.WriteCloser(r.Device).Close()
// long and short format are identical
// writer.Write([]byte(renderOptions.Label))
writer.Write([]byte(d.Short()))
writer.Write([]byte("\n"))
writer.Write([]byte(d.Long()))
writer.Write([]byte("\n"))
if fgColor := d.ForegroundColor(); fgColor != "" {
writer.Write([]byte(fgColor))
writer.Write([]byte("\n"))
}
if bgColor := d.BackgroundColor(); bgColor != "" {
writer.Write([]byte(bgColor))
writer.Write([]byte("\n"))
}
}
package protocols
import (
"encoding/json"
"log"
"os"
"github.com/spetix/bar-out-adapters/pkg/barout/models"
)
type JsonOut struct {
baseOutput
}
type outputjson struct {
Text string `json:"text"`
Tooltip string `json:"tooltip"`
Alt string `json:"alt"`
BackgroundColor string `json:"background-color"`
ForegroundColor string `json:"foreground-color"`
}
func NewJsonOut(device *os.File) *JsonOut {
return &JsonOut{
baseOutput: baseOutput{
Device: device,
},
}
}
func (j *JsonOut) Print(d models.Data) {
newJson := outputjson{
Text: d.Short(),
Tooltip: d.Long(),
Alt: d.Label(),
BackgroundColor: d.BackgroundColor(),
ForegroundColor: d.ForegroundColor(),
}
b, err := json.Marshal(newJson)
if err != nil {
log.Print("json marshal error", err)
}
j.Device.Write(b)
}
package protocols
import (
"io"
"os"
"github.com/spetix/bar-out-adapters/pkg/barout/models"
)
type RawOut struct {
baseOutput
}
func NewRawOut(device *os.File) *RawOut {
return &RawOut{
baseOutput: baseOutput{
Device: device,
},
}
}
func (r *RawOut) Print(d models.Data) {
writer := io.Writer(r.Device)
//defer io.WriteCloser(r.Device).Close()
// long and short format are identical
// writer.Write([]byte(renderOptions.Label))
writer.Write([]byte(d.Short()))
writer.Write([]byte("\n"))
writer.Write([]byte(d.Long()))
writer.Write([]byte("\n"))
if fgColor := d.ForegroundColor(); fgColor != "" {
writer.Write([]byte(fgColor))
writer.Write([]byte("\n"))
}
if bgColor := d.BackgroundColor(); bgColor != "" {
writer.Write([]byte(bgColor))
writer.Write([]byte("\n"))
}
}
package protocols
import (
"encoding/json"
"log"
"os"
"github.com/spetix/bar-out-adapters/pkg/barout/models"
)
type WaybarOut struct {
baseOutput
}
type outputjsonWaybar struct {
Text string `json:"text"`
Tooltip string `json:"tooltip"`
Alt string `json:"alt"`
BackgroundColor string `json:"background-color"`
ForegroundColor string `json:"foreground-color"`
}
func NewWaybarOut(device *os.File) *WaybarOut {
return &WaybarOut{
baseOutput: baseOutput{
Device: device,
},
}
}
func (j *WaybarOut) Print(d models.Data) {
newJson := outputjsonWaybar{
Alt: d.Label(),
Text: d.Short(), //dateutil.Format(unit, rerenderOptions.Unit),
Tooltip: d.Long(), //fmt.Sprint(dateutil.Format(unit, rerenderOptions.Unit), " to xmas"),
BackgroundColor: d.BackgroundColor(),
ForegroundColor: d.ForegroundColor(),
}
b, err := json.Marshal(newJson)
if err != nil {
log.Print("json marshal error", err)
}
j.Device.Write(b)
}
package render
type RenderOptionsImpl struct {
label *string
format *string
foregroundColor *string
backgroundColor *string
}
func (r *RenderOptionsImpl) Label() string {
return *r.label
}
func (r *RenderOptionsImpl) SetLabel(l *string) {
r.label = l
}
func (r *RenderOptionsImpl) Format() string {
return *r.format
}
func (r *RenderOptionsImpl) SetFormat(f *string) {
r.format = f
}
func (r *RenderOptionsImpl) ForegroundColor() string {
return *r.foregroundColor
}
func (r *RenderOptionsImpl) SetForegroundColor(f *string) {
r.foregroundColor = f
}
func (r *RenderOptionsImpl) BackgroundColor() string {
return *r.backgroundColor
}
func (r *RenderOptionsImpl) SetBackgroundColor(b *string) {
r.backgroundColor = b
}
func NewRenderOptionsImpl() *RenderOptionsImpl {
label := "🎄"
format := ""
foregroundColor := "#ffffff"
backgroundColor := "#000000"
return &RenderOptionsImpl{
label: &label,
format: &format,
foregroundColor: &foregroundColor,
backgroundColor: &backgroundColor,
}
}
package barout
import (
"github.com/spetix/bar-out-adapters/internal/configure"
"github.com/spetix/bar-out-adapters/pkg/barout/models"
)
// NewSetupBlocket selects the right driver for the output
func NewSetupBlocklet(f models.Formatter) models.SetupBlocklet {
return configure.NewSetupBlockletImpl(f)
}
package models
// EventHdlr is a function type that represents an event handler. It takes no arguments and returns an error.
type EventHdlr func() error
// EventType is a string type that represents the type of an event.
type EventType string
// type Event interface {
// Name() EventType
// Signal() int
// }
// EventHandlers is a map that associates EventType keys with EventHdlr values. It is used to store event handlers for different event types.
type EventHandlers map[EventType]EventHdlr
// EventStrategy is an interface that defines a strategy for executing events. It has a single method, Execute, which is responsible for executing the event handling logic.
type EventStrategy interface {
Execute()
}
const (
LeftButton EventType = "1"
MiddleButton EventType = "2"
RightButton EventType = "3"
ScrollUp EventType = "4"
ScrollDown EventType = "5"
)
func (e EventType) String() string {
switch e {
case LeftButton:
return "Left Button"
case MiddleButton:
return "Middle Button"
case RightButton:
return "Right Button"
case ScrollUp:
return "Scroll Up"
case ScrollDown:
return "Scroll Down"
default:
return "Unknown event"
}
}
// EventManager is an interface that defines methods for managing events. It allows registering event handlers and running the event handling logic.
type EventManager interface {
Register(ev EventType, hdlr EventHdlr)
Run()
}