Skip to content

Commit

Permalink
handle bad routine objects
Browse files Browse the repository at this point in the history
  • Loading branch information
snhilde committed Aug 23, 2020
1 parent 398ef7f commit 850cca2
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 0 deletions.
12 changes: 12 additions & 0 deletions sbbattery/battery.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func New(colors ...[3]string) *Routine {

// Update reads the current battery capacity left and calculates a percentage based on it.
func (r *Routine) Update() (bool, error) {
if r == nil {
return false, errors.New("Bad routine")
}

// Handle error in New or error reading max capacity.
if r.max <= 0 {
return false, r.err
Expand Down Expand Up @@ -115,6 +119,10 @@ func (r *Routine) Update() (bool, error) {

// String formats the percentage of battery left.
func (r *Routine) String() string {
if r == nil {
return "Bad routine"
}

var c string
if r.perc > 25 {
c = r.colors.normal
Expand All @@ -138,6 +146,10 @@ func (r *Routine) String() string {

// Error formats and returns an error message.
func (r *Routine) Error() string {
if r == nil {
return "Bad routine"
}

if r.err == nil {
r.err = errors.New("Unknown error")
}
Expand Down
12 changes: 12 additions & 0 deletions sbcputemp/cputemp.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func New(colors ...[3]string) *Routine {
// Update reads out the value of each sensor, gets an average of all temperatures, and converts it from milliCelsius to
// Celsius. If we have trouble reading a particular sensor, then we'll skip it on this pass.
func (r *Routine) Update() (bool, error) {
if r == nil {
return false, errors.New("Bad routine")
}

// Handle error in New.
if len(r.files) == 0 {
return false, r.err
Expand Down Expand Up @@ -111,6 +115,10 @@ func (r *Routine) Update() (bool, error) {

// String prints a formatted temperature average in degrees Celsius.
func (r *Routine) String() string {
if r == nil {
return "Bad routine"
}

var c string
if r.temp < 75 {
c = r.colors.normal
Expand All @@ -125,6 +133,10 @@ func (r *Routine) String() string {

// Error formats and returns an error message.
func (r *Routine) Error() string {
if r == nil {
return "Bad routine"
}

if r.err == nil {
r.err = errors.New("Unknown error")
}
Expand Down
12 changes: 12 additions & 0 deletions sbcpuusage/cpuusage.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ func New(colors ...[3]string) *Routine {
// Update gets the current CPU stats, compares them to the last-read stats, and calculates the percentage of CPU
// currently being used.
func (r *Routine) Update() (bool, error) {
if r == nil {
return false, errors.New("Bad routine")
}

// Handle error in New.
if r.threads < 0 {
return false, r.err
Expand Down Expand Up @@ -125,6 +129,10 @@ func (r *Routine) Update() (bool, error) {

// String prints the formatted CPU percentage.
func (r *Routine) String() string {
if r == nil {
return "Bad routine"
}

var c string

if r.perc < 75 {
Expand All @@ -140,6 +148,10 @@ func (r *Routine) String() string {

// Error formats and returns an error message.
func (r *Routine) Error() string {
if r == nil {
return "Bad routine"
}

if r.err == nil {
r.err = errors.New("Unknown error")
}
Expand Down
12 changes: 12 additions & 0 deletions sbdisk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func New(paths []string, colors ...[3]string) *Routine {
// Update gets the amount of used and total disk space and converts them into a human-readable size for each provided
// filesystem.
func (r *Routine) Update() (bool, error) {
if r == nil {
return false, errors.New("Bad routine")
}

// Handle error in New.
if len(r.disks) == 0 {
return false, r.err
Expand All @@ -114,6 +118,10 @@ func (r *Routine) Update() (bool, error) {

// String formats and prints the amounts of disk space for each provided filesystem.
func (r *Routine) String() string {
if r == nil {
return "Bad routine"
}

c := ""
b := new(strings.Builder)
for i, disk := range r.disks {
Expand All @@ -138,6 +146,10 @@ func (r *Routine) String() string {

// Error formats and returns an error message.
func (r *Routine) Error() string {
if r == nil {
return "Bad routine"
}

if r.err == nil {
r.err = errors.New("Unknown error")
}
Expand Down
12 changes: 12 additions & 0 deletions sbfan/fan.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func New(colors ...[3]string) *Routine {

// Update reads the current fan speed in RPM and calculates the percentage of the maximum speed.
func (r *Routine) Update() (bool, error) {
if r == nil {
return false, errors.New("Bad routine")
}

// Handle any error encountered in New.
if r.fanPath == "" || r.max == 0 {
return false, r.err
Expand All @@ -95,6 +99,10 @@ func (r *Routine) Update() (bool, error) {

// String prints the current speed in RPM.
func (r *Routine) String() string {
if r == nil {
return "Bad routine"
}

var c string

perc := (r.speed * 100) / r.max
Expand All @@ -115,6 +123,10 @@ func (r *Routine) String() string {

// Error formats and returns an error message.
func (r *Routine) Error() string {
if r == nil {
return "Bad routine"
}

if r.err == nil {
r.err = errors.New("Unknown error")
}
Expand Down
12 changes: 12 additions & 0 deletions sbload/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func New(colors ...[3]string) *Routine {

// Update calls Sysinfo() and calculates load averages.
func (r *Routine) Update() (bool, error) {
if r == nil {
return false, errors.New("Bad routine")
}

// Handle any error encountered in New.
if r.err != nil {
return true, r.err
Expand All @@ -83,6 +87,10 @@ func (r *Routine) Update() (bool, error) {

// String prints the 3 load averages with 2 decimal places of precision.
func (r *Routine) String() string {
if r == nil {
return "Bad routine"
}

var c string

if r.load1 >= 2 || r.load5 >= 2 || r.load15 >= 2 {
Expand All @@ -98,6 +106,10 @@ func (r *Routine) String() string {

// Error formats and returns an error message.
func (r *Routine) Error() string {
if r == nil {
return "Bad routine"
}

if r.err == nil {
r.err = errors.New("Unknown error")
}
Expand Down
12 changes: 12 additions & 0 deletions sbnetwork/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ func New(inames []string, colors ...[3]string) *Routine {

// Update gets the current readings of the rx/tx files for each interface.
func (r *Routine) Update() (bool, error) {
if r == nil {
return false, errors.New("Bad routine")
}

// Handle any error from New.
if len(r.ilist) == 0 {
return false, r.err
Expand Down Expand Up @@ -142,6 +146,10 @@ func (r *Routine) Update() (bool, error) {

// String calculates the byte difference for each interface, and formats and prints it.
func (r *Routine) String() string {
if r == nil {
return "Bad routine"
}

var c string
var b strings.Builder

Expand Down Expand Up @@ -170,6 +178,10 @@ func (r *Routine) String() string {

// Error formats and returns an error message.
func (r *Routine) Error() string {
if r == nil {
return "Bad routine"
}

if r.err == nil {
r.err = errors.New("Unknown error")
}
Expand Down
12 changes: 12 additions & 0 deletions sbnordvpn/nordvpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func New(colors ...[3]string) *Routine {

// Update runs the command and captures the output.
func (r *Routine) Update() (bool, error) {
if r == nil {
return false, errors.New("Bad routine")
}

cmd := exec.Command("nordvpn", "status")
output, err := cmd.Output()
if err != nil {
Expand All @@ -77,11 +81,19 @@ func (r *Routine) Update() (bool, error) {

// String formats and prints the current connection status.
func (r *Routine) String() string {
if r == nil {
return "Bad routine"
}

return r.color + r.parsed + colorEnd
}

// Error formats and returns an error message.
func (r *Routine) Error() string {
if r == nil {
return "Bad routine"
}

if r.err == nil {
r.err = errors.New("Unknown error")
}
Expand Down
12 changes: 12 additions & 0 deletions sbram/ram.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func New(colors ...[3]string) *Routine {
// missing the amount of cached RAM). Instead, we're going to read out /proc/meminfo and grab the values we need from
// there. All lines of that file have three fields: field name, value, and unit
func (r *Routine) Update() (bool, error) {
if r == nil {
return false, errors.New("Bad routine")
}

file, err := ioutil.ReadFile("/proc/meminfo")
if err != nil {
r.err = errors.New("Error reading file")
Expand All @@ -97,6 +101,10 @@ func (r *Routine) Update() (bool, error) {

// String formats and prints the used and total system memory.
func (r *Routine) String() string {
if r == nil {
return "Bad routine"
}

var color string

if r.perc < 75 {
Expand All @@ -112,6 +120,10 @@ func (r *Routine) String() string {

// Error formats and returns an error message.
func (r *Routine) Error() string {
if r == nil {
return "Bad routine"
}

if r.err == nil {
r.err = errors.New("Unknown error")
}
Expand Down
12 changes: 12 additions & 0 deletions sbtime/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func New(format string, colors ...[3]string) *Routine {

// Update updates the routine's current time.
func (r *Routine) Update() (bool, error) {
if r == nil {
return false, errors.New("Bad routine")
}

// Handle error in New.
if r.formatA == "" || r.formatB == "" {
if r.err == nil {
Expand All @@ -81,6 +85,10 @@ func (r *Routine) Update() (bool, error) {

// String prints the time in the provided format.
func (r *Routine) String() string {
if r == nil {
return "Bad routine"
}

format := r.formatA
if r.time.Second()%2 != 0 {
format = r.formatB
Expand All @@ -91,6 +99,10 @@ func (r *Routine) String() string {

// Error formats and returns an error message.
func (r *Routine) Error() string {
if r == nil {
return "Bad routine"
}

if r.err == nil {
r.err = errors.New("Unknown error")
}
Expand Down
12 changes: 12 additions & 0 deletions sbtodo/todo.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ func New(path string, colors ...[3]string) *Routine {

// Update reads the TODO file again, if it was modified since the last read.
func (r *Routine) Update() (bool, error) {
if r == nil {
return false, errors.New("Bad routine")
}

// Handle any error from New.
if r.info.Name() == "" {
if r.err == nil {
Expand Down Expand Up @@ -115,6 +119,10 @@ func (r *Routine) Update() (bool, error) {
// 3. If one line has content and the next line with content is indented (tabs or spaces), print "line1 -> line2".
// 4. If two lines have content and both are flush, print "line1 | line2".
func (r *Routine) String() string {
if r == nil {
return "Bad routine"
}

var b strings.Builder

r.line1 = strings.TrimSpace(r.line1)
Expand Down Expand Up @@ -148,6 +156,10 @@ func (r *Routine) String() string {

// Error formats and returns an error message.
func (r *Routine) Error() string {
if r == nil {
return "Bad routine"
}

if r.err == nil {
r.err = errors.New("Unknown error")
}
Expand Down
12 changes: 12 additions & 0 deletions sbvolume/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func New(control string, colors ...[3]string) *Routine {

// Update runs the 'amixer' command and parses the output for mute status and volume percentage.
func (r *Routine) Update() (bool, error) {
if r == nil {
return false, errors.New("Bad routine")
}

// Handle error from New.
if r.control == "" {
if r.err == nil {
Expand Down Expand Up @@ -116,6 +120,10 @@ func (r *Routine) Update() (bool, error) {

// String prints either an error, the mute status, or the volume percentage.
func (r *Routine) String() string {
if r == nil {
return "Bad routine"
}

if r.muted {
return r.colors.warning + "Vol mute" + colorEnd
}
Expand All @@ -125,6 +133,10 @@ func (r *Routine) String() string {

// Error formats and returns an error message.
func (r *Routine) Error() string {
if r == nil {
return "Bad routine"
}

if r.err == nil {
r.err = errors.New("Unknown error")
}
Expand Down
Loading

0 comments on commit 850cca2

Please sign in to comment.