pass config for initialization, add marshaling for status

This commit is contained in:
Kevin 2019-07-04 19:12:58 -04:00
parent 00ea73813a
commit e3732717f5
3 changed files with 22 additions and 14 deletions

View File

@ -40,9 +40,9 @@ func start(config *Config) func(*cli.Cmd) {
task.ID = id
return nil
}))
runner, err := NewTaskRunner(task, db, NewXnotifier(config.IconPath))
runner, err := NewTaskRunner(task, config)
maybe(err)
server, err := NewServer(config.SocketPath, runner)
server, err := NewServer(runner, config)
maybe(err)
server.Start()
defer server.Stop()
@ -110,9 +110,9 @@ func begin(config *Config) func(*cli.Cmd) {
task.Pomodoros = []*Pomodoro{}
return nil
}))
runner, err := NewTaskRunner(task, db, NewXnotifier(config.IconPath))
runner, err := NewTaskRunner(task, config)
maybe(err)
server, err := NewServer(config.SocketPath, runner)
server, err := NewServer(runner, config)
maybe(err)
server.Start()
defer server.Stop()

View File

@ -20,7 +20,11 @@ type TaskRunner struct {
duration time.Duration
}
func NewTaskRunner(task *Task, store *Store, notifier Notifier) (*TaskRunner, error) {
func NewTaskRunner(task *Task, config *Config) (*TaskRunner, error) {
store, err := NewStore(config.DBPath)
if err != nil {
return nil, err
}
tr := &TaskRunner{
taskID: task.ID,
taskMessage: task.Message,
@ -30,7 +34,7 @@ func NewTaskRunner(task *Task, store *Store, notifier Notifier) (*TaskRunner, er
state: State(0),
pause: make(chan bool),
toggle: make(chan bool),
notifier: notifier,
notifier: NewXnotifier(config.IconPath),
duration: task.Duration,
}
return tr, nil
@ -44,6 +48,10 @@ func (t *TaskRunner) TimeRemaining() time.Duration {
return (t.duration - time.Since(t.started)).Truncate(time.Second)
}
func (t *TaskRunner) SetState(state State) {
t.state = state
}
func (t *TaskRunner) run() error {
for t.count < t.nPomodoros {
// Create a new pomodoro where we
@ -52,8 +60,8 @@ func (t *TaskRunner) run() error {
pomodoro := &Pomodoro{}
// Start this pomodoro
pomodoro.Start = time.Now()
// Set state to RUNNING
t.state = RUNNING
// Set state to RUNNIN
t.SetState(RUNNING)
// Create a new timer
timer := time.NewTimer(t.duration)
// Record our started time
@ -61,7 +69,7 @@ func (t *TaskRunner) run() error {
loop:
select {
case <-timer.C:
t.state = BREAKING
t.SetState(BREAKING)
t.count++
case <-t.toggle:
// Catch any toggles when we
@ -72,7 +80,7 @@ func (t *TaskRunner) run() error {
// Record the remaining time of the current pomodoro
remaining := t.TimeRemaining()
// Change state to PAUSED
t.state = PAUSED
t.SetState(PAUSED)
// Wait for the user to press [p]
<-t.pause
// Resume the timer with previous
@ -82,7 +90,7 @@ func (t *TaskRunner) run() error {
t.started = time.Now()
t.duration = remaining
// Restore state to RUNNING
t.state = RUNNING
t.SetState(RUNNING)
goto loop
}
pomodoro.End = time.Now()
@ -106,7 +114,7 @@ func (t *TaskRunner) run() error {
}
t.notifier.Notify("Pomo", "Pomo session has completed!")
t.state = COMPLETE
t.SetState(COMPLETE)
return nil
}

View File

@ -38,8 +38,8 @@ func (s *Server) Stop() {
s.listener.Close()
}
func NewServer(path string, runner *TaskRunner) (*Server, error) {
listener, err := net.Listen("unix", path)
func NewServer(runner *TaskRunner, config *Config) (*Server, error) {
listener, err := net.Listen("unix", config.SocketPath)
if err != nil {
return nil, err
}