Commits

Richo Healey committed c8c0e47

Turh colors on or off with --set

Comments (0)

Files changed (5)

         puts "twat -a #{opts[:account]}"
       rescue ConfigVersionIncorrect
         puts "Your config file is out of date. Run with --update-config to rememdy"
+      rescue InvalidBool
+        puts "Invalid value, valid values are #{Options::BOOL_VALID.join("|")}"
       end
     end
   end

lib/twat/actions.rb

 
     # Format a tweet all pretty like
     def format(twt)
-      # if config.color
-      if twt.user.screen_name == account_name.to_s
-        puts "#{twt.user.screen_name.bold.blue}: #{twt.text}"
+      if config.colors?
+        if twt.user.screen_name == account_name.to_s
+          puts "#{twt.user.screen_name.bold.blue}: #{twt.text}"
+        else
+          puts "#{twt.user.screen_name.bold.cyan}: #{twt.text}"
+        end
       else
-        puts "#{twt.user.screen_name.bold.cyan}: #{twt.text}"
+        puts "#{twt.user.screen_name}: #{twt.text}"
       end
     end
 

lib/twat/config.rb

       return config[:accounts]
     end
 
+    def colors?
+      colors = config[:colors] || "true"
+      Options.bool_true?(colors)
+    end
+
     def default_account
       raise NoDefaultAccount unless config.include?(:default)
       return config[:default].to_sym

lib/twat/exceptions.rb

   class InvalidCredentials < Exception; end
   class ConfigVersionIncorrect < Exception; end
   class InvalidSetOpt < Exception; end
+  class InvalidBool < Exception; end
 end

lib/twat/options.rb

 module Twat
   class Options
+    BOOL_TRUE=["yes", "true", "1", "on"]
+    BOOL_FALSE=["no", "false", "0", "off"]
+    BOOL_VALS = BOOL_TRUE + BOOL_FALSE
+    def self.bool_true?(val)
+      BOOL_TRUE.include?(val.to_s.downcase)
+    end
+
+    def self.bool_false?(val)
+      BOOL_FALSE.include?(val.to_s.downcase)
+    end
+
+    def self.bool_valid?(val)
+      BOOL_VALS.include?(val.to_s.downcase)
+    end
 
     # A set of wrappers around the global config object to set given attributes
     # Catching failures is convenient because of the method_missing? hook
     def method_missing(sym, *args, &block)
+      puts sym
       raise InvalidSetOpt
     end
 
       config.save!
     end
 
+    def colors=(value)
+      val = value.to_sym
+      raise InvalidBool unless Options.bool_valid?(val)
+      config[:colors] = val
+      config.save!
+    end
+
   end
 end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.