Skip to content

Commit

Permalink
Rename Optional to Features
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokokada committed Feb 6, 2024
1 parent 09905ce commit bd8cc30
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 22 deletions.
8 changes: 4 additions & 4 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ var loop int
/*
Create a new TwentyTwentyTwenty struct.
*/
func New(optional Optional, settings Settings) *TwentyTwentyTwenty {
return &TwentyTwentyTwenty{Optional: optional, Settings: settings}
func New(features Features, settings Settings) *TwentyTwentyTwenty {
return &TwentyTwentyTwenty{Features: features, Settings: settings}
}

/*
Expand All @@ -36,7 +36,7 @@ This will start the main twenty-twenty-twenty loop in a goroutine, so avoid
calling this function inside a goroutine.
*/
func (t *TwentyTwentyTwenty) Start() {
if t.Optional.Sound {
if t.Features.Sound {
log.Printf(
"Running twenty-twenty-twenty every %.1f minute(s), with %.f second(s) duration and sound set to %t\n",
t.Settings.Frequency.Minutes(),
Expand Down Expand Up @@ -134,7 +134,7 @@ func (t *TwentyTwentyTwenty) loop() {
log.Printf("Showing notification for %.f second(s)\n", t.Settings.Duration.Seconds())
// wait 1.5x the duration so we have some time for the sounds to
// finish playing
if t.Optional.Sound {
if t.Features.Sound {
go sound.SuspendAfter(min(t.Settings.Duration*3/2, t.Settings.Frequency))
}
err := notification.SendWithDuration(
Expand Down
2 changes: 1 addition & 1 deletion internal/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func newMockNotifier() *mockNotifier {
var (
notifier *mockNotifier
twenty = New(
Optional{
Features{
Sound: false,
Systray: false,
},
Expand Down
8 changes: 4 additions & 4 deletions internal/core/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func ParseFlags(
progname string,
args []string,
version string,
optional Optional,
features Features,
) Settings {
flags := flag.NewFlagSet(progname, flag.ExitOnError)
durationInSec := flags.Uint(
Expand All @@ -25,15 +25,15 @@ func ParseFlags(
"how often the pause should be in seconds",
)
pauseInSec := new(uint)
if optional.Systray {
if features.Systray {
pauseInSec = flags.Uint(
"pause",
60*60,
"how long the pause (from systray) should be in seconds",
)
}
disableSound := new(bool)
if optional.Sound {
if features.Sound {
disableSound = flags.Bool(
"disable-sound",
false,
Expand Down Expand Up @@ -62,7 +62,7 @@ func ParseFlags(
Duration: time.Duration(*durationInSec) * time.Second,
Frequency: time.Duration(*frequencyInSec) * time.Second,
Pause: time.Duration(*pauseInSec) * time.Second,
Sound: optional.Sound && !*disableSound,
Sound: features.Sound && !*disableSound,
Verbose: *verbose,
}
}
4 changes: 2 additions & 2 deletions internal/core/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestParseFlags(t *testing.T) {
const version = "test"

// always return false for sound if disabled
settings := ParseFlags(progname, []string{}, version, Optional{Sound: false, Systray: false})
settings := ParseFlags(progname, []string{}, version, Features{Sound: false, Systray: false})
assert.Equal(t, settings.Sound, false)

var tests = []struct {
Expand All @@ -28,7 +28,7 @@ func TestParseFlags(t *testing.T) {

for _, tt := range tests {
t.Run(strings.Join(tt.args, " "), func(t *testing.T) {
settings := ParseFlags(progname, tt.args, version, Optional{Sound: true, Systray: true})
settings := ParseFlags(progname, tt.args, version, Features{Sound: true, Systray: true})
assert.Equal(t, settings, tt.settings)
})
}
Expand Down
8 changes: 4 additions & 4 deletions internal/core/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ TwentyTwentyTwenty struct.
Keeps the main state of the program.
*/
type TwentyTwentyTwenty struct {
Optional Optional
Settings Settings
Features
Settings

cancelLoopCtx context.CancelFunc
loopCtx context.Context
mu sync.Mutex
}

/*
Optional struct.
Features struct.
This is used for features that are optional in the program, for example if sound
or systray are permanently disabled.
*/
type Optional struct {
type Features struct {
Sound bool
Systray bool
}
Expand Down
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ func main() {
logger := slog.New(handler)
slog.SetDefault(logger)

optional := core.Optional{Sound: sound.Enabled, Systray: systrayEnabled}
settings := core.ParseFlags(os.Args[0], os.Args[1:], version, optional)
features := core.Features{Sound: sound.Enabled, Systray: systrayEnabled}
settings := core.ParseFlags(os.Args[0], os.Args[1:], version, features)

if settings.Verbose {
lvl.Set(slog.LevelDebug)
}

if optional.Sound {
if features.Sound {
err := sound.Init(!settings.Sound)
if err != nil {
log.Printf("Error while initialising sound: %v\n", err)
log.Println("Disabling sound")
optional.Sound = false
features.Sound = false
settings.Sound = false
}
}
Expand All @@ -55,13 +55,13 @@ func main() {
if err != nil {
log.Fatalf("Test notification failed: %v. Exiting...", err)
}
twenty = core.New(optional, settings)
twenty = core.New(features, settings)
// we need to start notification cancellation in a goroutine to show the
// systray as soon as possible (since it depends on the loop() call), but we
// also need to give it access to the core.Ctx to cancel it if necessary
twenty.Start()
go func() {
if optional.Sound {
if features.Sound {
// wait the 1.5x of duration so we have some time for the sounds to
// finish playing
go sound.SuspendAfter(min(settings.Duration*3/2, settings.Frequency))
Expand Down
2 changes: 1 addition & 1 deletion systray.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func onReady() {
false,
)
mSound := new(systray.MenuItem)
if twenty.Optional.Sound {
if twenty.Features.Sound {
mSound = systray.AddMenuItemCheckbox("Sound", "Enable notification sound", twenty.Settings.Sound)
}
systray.AddSeparator()
Expand Down

0 comments on commit bd8cc30

Please sign in to comment.