Author: Kevin [kevinschoon@gmail.com]
Hash: e3732717f528be61fcef20d13f9ce44e2bf34aad
Timestamp: Thu, 04 Jul 2019 23:12:58 +0000 (5 years ago)

+22 -14 +/-3 browse
pass config for initialization, add marshaling for status
1diff --git a/main.go b/main.go
2index a7ebcf9..e8675d4 100644
3--- a/main.go
4+++ b/main.go
5 @@ -40,9 +40,9 @@ func start(config *Config) func(*cli.Cmd) {
6 task.ID = id
7 return nil
8 }))
9- runner, err := NewTaskRunner(task, db, NewXnotifier(config.IconPath))
10+ runner, err := NewTaskRunner(task, config)
11 maybe(err)
12- server, err := NewServer(config.SocketPath, runner)
13+ server, err := NewServer(runner, config)
14 maybe(err)
15 server.Start()
16 defer server.Stop()
17 @@ -110,9 +110,9 @@ func begin(config *Config) func(*cli.Cmd) {
18 task.Pomodoros = []*Pomodoro{}
19 return nil
20 }))
21- runner, err := NewTaskRunner(task, db, NewXnotifier(config.IconPath))
22+ runner, err := NewTaskRunner(task, config)
23 maybe(err)
24- server, err := NewServer(config.SocketPath, runner)
25+ server, err := NewServer(runner, config)
26 maybe(err)
27 server.Start()
28 defer server.Stop()
29 diff --git a/runner.go b/runner.go
30index 97869d6..f8cbadf 100644
31--- a/runner.go
32+++ b/runner.go
33 @@ -20,7 +20,11 @@ type TaskRunner struct {
34 duration time.Duration
35 }
36
37- func NewTaskRunner(task *Task, store *Store, notifier Notifier) (*TaskRunner, error) {
38+ func NewTaskRunner(task *Task, config *Config) (*TaskRunner, error) {
39+ store, err := NewStore(config.DBPath)
40+ if err != nil {
41+ return nil, err
42+ }
43 tr := &TaskRunner{
44 taskID: task.ID,
45 taskMessage: task.Message,
46 @@ -30,7 +34,7 @@ func NewTaskRunner(task *Task, store *Store, notifier Notifier) (*TaskRunner, er
47 state: State(0),
48 pause: make(chan bool),
49 toggle: make(chan bool),
50- notifier: notifier,
51+ notifier: NewXnotifier(config.IconPath),
52 duration: task.Duration,
53 }
54 return tr, nil
55 @@ -44,6 +48,10 @@ func (t *TaskRunner) TimeRemaining() time.Duration {
56 return (t.duration - time.Since(t.started)).Truncate(time.Second)
57 }
58
59+ func (t *TaskRunner) SetState(state State) {
60+ t.state = state
61+ }
62+
63 func (t *TaskRunner) run() error {
64 for t.count < t.nPomodoros {
65 // Create a new pomodoro where we
66 @@ -52,8 +60,8 @@ func (t *TaskRunner) run() error {
67 pomodoro := &Pomodoro{}
68 // Start this pomodoro
69 pomodoro.Start = time.Now()
70- // Set state to RUNNING
71- t.state = RUNNING
72+ // Set state to RUNNIN
73+ t.SetState(RUNNING)
74 // Create a new timer
75 timer := time.NewTimer(t.duration)
76 // Record our started time
77 @@ -61,7 +69,7 @@ func (t *TaskRunner) run() error {
78 loop:
79 select {
80 case <-timer.C:
81- t.state = BREAKING
82+ t.SetState(BREAKING)
83 t.count++
84 case <-t.toggle:
85 // Catch any toggles when we
86 @@ -72,7 +80,7 @@ func (t *TaskRunner) run() error {
87 // Record the remaining time of the current pomodoro
88 remaining := t.TimeRemaining()
89 // Change state to PAUSED
90- t.state = PAUSED
91+ t.SetState(PAUSED)
92 // Wait for the user to press [p]
93 <-t.pause
94 // Resume the timer with previous
95 @@ -82,7 +90,7 @@ func (t *TaskRunner) run() error {
96 t.started = time.Now()
97 t.duration = remaining
98 // Restore state to RUNNING
99- t.state = RUNNING
100+ t.SetState(RUNNING)
101 goto loop
102 }
103 pomodoro.End = time.Now()
104 @@ -106,7 +114,7 @@ func (t *TaskRunner) run() error {
105
106 }
107 t.notifier.Notify("Pomo", "Pomo session has completed!")
108- t.state = COMPLETE
109+ t.SetState(COMPLETE)
110 return nil
111 }
112
113 diff --git a/server.go b/server.go
114index 2ae8767..c57ed82 100644
115--- a/server.go
116+++ b/server.go
117 @@ -38,8 +38,8 @@ func (s *Server) Stop() {
118 s.listener.Close()
119 }
120
121- func NewServer(path string, runner *TaskRunner) (*Server, error) {
122- listener, err := net.Listen("unix", path)
123+ func NewServer(runner *TaskRunner, config *Config) (*Server, error) {
124+ listener, err := net.Listen("unix", config.SocketPath)
125 if err != nil {
126 return nil, err
127 }