@@ -22,17 +22,57 @@ def autolocate!
22
22
end
23
23
24
24
attr_accessor :installation
25
+ attr_reader :prefs_cache
26
+ attr_reader :prefs_response_time
25
27
26
28
# @param installation [ArduinoInstallation] the location of the Arduino program installation
27
29
def initialize ( installation )
28
- @display_mgr = DisplayManager ::instance
29
- @installation = installation
30
+ @display_mgr = DisplayManager ::instance
31
+ @installation = installation
32
+ @prefs_response_time = nil
33
+ @prefs_cache = prefs
34
+ end
35
+
36
+ # fetch preferences to a hash
37
+ def prefs
38
+ resp = nil
39
+ @display_mgr . with_display do
40
+ start = Time . now
41
+ resp = run_and_capture ( "--get-pref" )
42
+ @prefs_response_time = Time . now - start
43
+ puts "prefs_response_time = #{ @prefs_response_time } = #{ Time . now } - #{ start } "
44
+ end
45
+ return nil unless resp [ :success ]
46
+ lines = resp [ :out ] . split ( "\n " ) . select { |l | l . include? "=" }
47
+ ret = lines . each_with_object ( { } ) do |e , acc |
48
+ parts = e . split ( "=" , 2 )
49
+ acc [ parts [ 0 ] ] = parts [ 1 ]
50
+ acc
51
+ end
52
+ ret
30
53
end
31
54
32
55
# run the arduino command
33
- def run ( *args )
56
+ # @return [Hash] {:out => StringIO, :err => StringIO }
57
+ def run ( *args , **kwargs )
34
58
full_args = [ @installation . cmd_path ] + args
35
- @display_mgr . run ( *full_args )
59
+ @display_mgr . run ( *full_args , **kwargs )
60
+
61
+ end
62
+
63
+ # run a command and capture its output
64
+ # @return [Hash] {:out => StringIO, :err => StringIO, :success => bool}
65
+ def run_and_capture ( *args )
66
+ pipe_out , pipe_out_wr = IO . pipe
67
+ pipe_err , pipe_err_wr = IO . pipe
68
+ success = run ( *args , out : pipe_out_wr , err : pipe_err_wr )
69
+ pipe_out_wr . close
70
+ pipe_err_wr . close
71
+ str_out = pipe_out . read
72
+ str_err = pipe_err . read
73
+ pipe_out . close
74
+ pipe_err . close
75
+ { out : str_out , err : str_err , success : success }
36
76
end
37
77
38
78
def board_installed? ( board )
0 commit comments