Skip to content

Commit

Permalink
Refactor folder structure of features
Browse files Browse the repository at this point in the history
  • Loading branch information
diogox committed Feb 23, 2021
1 parent 21b6880 commit f2aa1d3
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 103 deletions.
6 changes: 3 additions & 3 deletions internal/cli/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
bspwmdesktop "github.com/diogox/bspm/internal/bspwm/desktop"
bspwmevent "github.com/diogox/bspm/internal/bspwm/event"
bspwmnode "github.com/diogox/bspm/internal/bspwm/node"
"github.com/diogox/bspm/internal/feature"
"github.com/diogox/bspm/internal/feature/state"
transparentmonocle "github.com/diogox/bspm/internal/feature/transparent_monocle"
"github.com/diogox/bspm/internal/feature/transparent_monocle/state"
"github.com/diogox/bspm/internal/ipc"
"github.com/diogox/bspm/internal/log"
)
Expand All @@ -37,7 +37,7 @@ func runDaemon(logger *log.Logger) error {
return fmt.Errorf("failed to initialise bspwm client: %v", err)
}

monocle, cancel, err := feature.StartTransparentMonocle(
monocle, cancel, err := transparentmonocle.Start(
logger,
state.NewTransparentMonocle(),
bspwm.NewService(
Expand Down
65 changes: 0 additions & 65 deletions internal/feature/state/transparent_monocle.go

This file was deleted.

8 changes: 0 additions & 8 deletions internal/feature/state/transparent_monocle_exports_test.go

This file was deleted.

File renamed without changes.
65 changes: 65 additions & 0 deletions internal/feature/transparent_monocle/state/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//go:generate mockgen -package state -destination ./state_mock.go -self_package github.com/diogox/bspm/internal/feature/transparent_monocle/state github.com/diogox/bspm/internal/feature/transparent_monocle/state Manager

package state

import (
"errors"
"sync"

"github.com/diogox/bspc-go"
)

type (
Manager interface {
Get(desktopID bspc.ID) (State, error)
Set(desktopID bspc.ID, st State)
Delete(desktopID bspc.ID) (State, error)
}

State struct {
SelectedNodeID *bspc.ID
HiddenNodeIDs []bspc.ID
}

manager struct {
desktops *sync.Map
}
)

func NewTransparentMonocle() manager {
return manager{
desktops: &sync.Map{},
}
}

func (tm manager) Get(desktopID bspc.ID) (State, error) {
stObj, ok := tm.desktops.Load(desktopID)
if !ok {
return State{}, ErrNotFound
}

st, ok := stObj.(State)
if !ok {
return State{}, errors.New("invalid state type")
}

return st, nil
}

func (tm manager) Set(desktopID bspc.ID, st State) {
tm.desktops.Store(desktopID, st)
}

func (tm manager) Delete(desktopID bspc.ID) (State, error) {
stObj, ok := tm.desktops.LoadAndDelete(desktopID)
if !ok {
return State{}, ErrNotFound
}

st, ok := stObj.(State)
if !ok {
return State{}, errors.New("invalid state type")
}

return st, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/diogox/bspm/internal/feature/state"
"github.com/diogox/bspm/internal/feature/transparent_monocle/state"
)

func TestTransparentMonocle_Get(t *testing.T) {
t.Run("should get stored state", func(t *testing.T) {
var (
desktopID = bspc.ID(1)
selectedNodeID = bspc.ID(2)
st = state.TransparentMonocleState{
st = state.State{
SelectedNodeID: &selectedNodeID,
HiddenNodeIDs: []bspc.ID{3},
}
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestTransparentMonocle_Set(t *testing.T) {
var (
desktopID = bspc.ID(1)
selectedNodeID = bspc.ID(2)
st = state.TransparentMonocleState{
st = state.State{
SelectedNodeID: &selectedNodeID,
HiddenNodeIDs: []bspc.ID{3},
}
Expand All @@ -79,7 +79,7 @@ func TestTransparentMonocle_Delete(t *testing.T) {
var (
desktopID = bspc.ID(1)
selectedNodeID = bspc.ID(2)
st = state.TransparentMonocleState{
st = state.State{
SelectedNodeID: &selectedNodeID,
HiddenNodeIDs: []bspc.ID{3},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package state

import "sync"

func (tm manager) WithMap(m *sync.Map) manager {
tm.desktops = m
return tm
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:generate mockgen -package feature -destination ./bspc_mock.go github.com/diogox/bspc-go Client
//go:generate mockgen -package feature -destination ./transparent_monocle_mock.go -self_package github.com/diogox/bspm/internal/feature github.com/diogox/bspm/internal/feature TransparentMonocle
//go:generate mockgen -package transparentmonocle -destination ./bspc_mock.go github.com/diogox/bspc-go Client
//go:generate mockgen -package transparentmonocle -destination ./transparent_monocle_mock.go -self_package github.com/diogox/bspm/internal/feature/transparent_monocle github.com/diogox/bspm/internal/feature/transparent_monocle Feature

package feature
package transparentmonocle

import (
"errors"
Expand All @@ -13,12 +13,12 @@ import (
"github.com/diogox/bspm/internal/bspwm"
"github.com/diogox/bspm/internal/bspwm/filter"

"github.com/diogox/bspm/internal/feature/state"
"github.com/diogox/bspm/internal/feature/transparent_monocle/state"
"github.com/diogox/bspm/internal/log"
)

type (
TransparentMonocle interface {
Feature interface {
ToggleCurrentDesktop() error
FocusPreviousHiddenNode() error
FocusNextHiddenNode() error
Expand All @@ -27,15 +27,15 @@ type (
transparentMonocle struct {
logger *log.Logger
service bspwm.Service
desktops state.TransparentMonocle
desktops state.Manager
}
)

func StartTransparentMonocle(
func Start(
logger *log.Logger,
desktops state.TransparentMonocle,
desktops state.Manager,
service bspwm.Service,
) (TransparentMonocle, func(), error) {
) (Feature, func(), error) {
service.Events().On(bspc.EventTypeNodeAdd, func(eventPayload interface{}) error {
payload, ok := eventPayload.(bspc.EventNodeAdd)
if !ok {
Expand Down Expand Up @@ -286,7 +286,7 @@ func StartTransparentMonocle(
func handleNodeRemoved(
logger *log.Logger,
service bspwm.Service,
desktops state.TransparentMonocle,
desktops state.Manager,
desktopID bspc.ID,
nodeID bspc.ID,
) error {
Expand All @@ -309,7 +309,7 @@ func handleNodeRemoved(
}

if isHiddenNode {
desktops.Set(desktopID, state.TransparentMonocleState{
desktops.Set(desktopID, state.State{
SelectedNodeID: st.SelectedNodeID,
HiddenNodeIDs: removeFromSlice(st.HiddenNodeIDs, nodeID),
})
Expand Down Expand Up @@ -345,7 +345,7 @@ func handleNodeRemoved(
newHiddenNodeIDs = removeFromSlice(st.HiddenNodeIDs, *newSelectedNodeID)
}

desktops.Set(desktopID, state.TransparentMonocleState{
desktops.Set(desktopID, state.State{
SelectedNodeID: newSelectedNodeID,
HiddenNodeIDs: newHiddenNodeIDs,
})
Expand All @@ -356,7 +356,7 @@ func handleNodeRemoved(
func handleNodeAdded(
logger *log.Logger,
service bspwm.Service,
desktops state.TransparentMonocle,
desktops state.Manager,
desktopID bspc.ID,
nodeID bspc.ID,
) error {
Expand Down Expand Up @@ -393,7 +393,7 @@ func handleNodeAdded(
newHiddenNodeIDs = append(newHiddenNodeIDs, *st.SelectedNodeID)
}

desktops.Set(desktopID, state.TransparentMonocleState{
desktops.Set(desktopID, state.State{
SelectedNodeID: &nodeID,
HiddenNodeIDs: newHiddenNodeIDs,
})
Expand Down Expand Up @@ -464,15 +464,15 @@ func (tm transparentMonocle) enableMode(desktop bspc.Desktop) error {
}
}

tm.desktops.Set(desktop.ID, state.TransparentMonocleState{
tm.desktops.Set(desktop.ID, state.State{
SelectedNodeID: selectedNodeID,
HiddenNodeIDs: hiddenNodeIDs,
})

return nil
}

func (tm transparentMonocle) disableMode(st state.TransparentMonocleState) error {
func (tm transparentMonocle) disableMode(st state.State) error {
for _, n := range st.HiddenNodeIDs {
if err := tm.service.Nodes().SetVisibility(n, true); err != nil {
return fmt.Errorf("failed to show node: %w", err)
Expand Down Expand Up @@ -515,7 +515,7 @@ func (tm transparentMonocle) FocusPreviousHiddenNode() error {
return fmt.Errorf("failed to hide %d node: %v", st.SelectedNodeID, err)
}

tm.desktops.Set(desktop.ID, state.TransparentMonocleState{
tm.desktops.Set(desktop.ID, state.State{
SelectedNodeID: &nextNodeID,
HiddenNodeIDs: append([]bspc.ID{*st.SelectedNodeID}, removeFromSlice(st.HiddenNodeIDs, nextNodeID)...),
})
Expand Down Expand Up @@ -552,7 +552,7 @@ func (tm transparentMonocle) FocusNextHiddenNode() error {
return fmt.Errorf("failed to hide %d node: %v", st.SelectedNodeID, err)
}

tm.desktops.Set(desktop.ID, state.TransparentMonocleState{
tm.desktops.Set(desktop.ID, state.State{
SelectedNodeID: &nextNodeID,
HiddenNodeIDs: append(removeFromSlice(st.HiddenNodeIDs, nextNodeID), *st.SelectedNodeID),
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package feature_test
package transparentmonocle_test

import (
"testing"
Expand All @@ -11,8 +11,8 @@ import (

"github.com/diogox/bspm/internal/bspwm"
bspwmevent "github.com/diogox/bspm/internal/bspwm/event"
"github.com/diogox/bspm/internal/feature"
"github.com/diogox/bspm/internal/feature/state"
transparentmonocle "github.com/diogox/bspm/internal/feature/transparent_monocle"
"github.com/diogox/bspm/internal/feature/transparent_monocle/state"
"github.com/diogox/bspm/internal/log"
)

Expand All @@ -26,7 +26,7 @@ func TestNewTransparentMonocle(t *testing.T) {
var (
mockEventManager = bspwmevent.NewMockManager(ctrl)
mockService = bspwm.NewMockService(ctrl)
mockState = state.NewMockTransparentMonocle(ctrl)
mockState = state.NewMockManager(ctrl)
)

mockService.EXPECT().
Expand All @@ -48,7 +48,7 @@ func TestNewTransparentMonocle(t *testing.T) {
logger, err := log.New(zaptest.NewLogger(t), false)
require.NoError(t, err)

_, _, err = feature.StartTransparentMonocle(logger, mockState, mockService)
_, _, err = transparentmonocle.Start(logger, mockState, mockService)
assert.NoError(t, err)
})
}

0 comments on commit f2aa1d3

Please sign in to comment.