| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-01 12:29:21 +02:00
										 |  |  | require "delegate" | 
					
						
							|  |  |  | require "etc" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "system_command" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class User < DelegateClass(String) | 
					
						
							| 
									
										
										
										
											2019-09-13 05:50:26 +02:00
										 |  |  |   def self.automation_access? | 
					
						
							|  |  |  |     return @automation_access if defined?(@automation_access) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     *_, status = system_command "osascript", args: [ | 
					
						
							|  |  |  |       "-e", "with timeout of 0.5 seconds", | 
					
						
							|  |  |  |       "-e", 'tell application "System Events" to get volume settings', | 
					
						
							|  |  |  |       "-e", "end timeout" | 
					
						
							|  |  |  |     ], print_stderr: false | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @automation_access = status.success? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def self.automation_access_instructions | 
					
						
							|  |  |  |     "Enable Automation Access for “Terminal > System Events” in " \ | 
					
						
							|  |  |  |     "“System Preferences > Security > Privacy > Automation”." | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-01 12:29:21 +02:00
										 |  |  |   def gui? | 
					
						
							|  |  |  |     out, _, status = system_command "who" | 
					
						
							|  |  |  |     return false unless status.success? | 
					
						
							| 
									
										
										
										
											2019-02-19 13:12:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-01 12:29:21 +02:00
										 |  |  |     out.lines | 
					
						
							|  |  |  |        .map(&:split) | 
					
						
							|  |  |  |        .any? { |user, type,| user == self && type == "console" } | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def self.current | 
					
						
							|  |  |  |     @current ||= new(Etc.getpwuid(Process.euid).name) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |