Class: Kscript::KkMacOptimizeUtils

Inherits:
Base
  • Object
show all
Defined in:
lib/kscript/plugins/kk_mac_optimize_utils.rb

Instance Attribute Summary

Attributes inherited from Base

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#human_output?, inherited, #with_error_handling

Constructor Details

#initialize(*args, **opts) ⇒ KkMacOptimizeUtils

Returns a new instance of KkMacOptimizeUtils.



12
13
14
# File 'lib/kscript/plugins/kk_mac_optimize_utils.rb', line 12

def initialize(*args, **opts)
  super
end

Class Method Details

.argumentsObject



72
73
74
# File 'lib/kscript/plugins/kk_mac_optimize_utils.rb', line 72

def self.arguments
  ''
end

.authorObject



84
85
86
# File 'lib/kscript/plugins/kk_mac_optimize_utils.rb', line 84

def self.author
  'kk'
end

.descriptionObject



88
89
90
# File 'lib/kscript/plugins/kk_mac_optimize_utils.rb', line 88

def self.description
  'Optimize macOS system performance.'
end

.groupObject



80
81
82
# File 'lib/kscript/plugins/kk_mac_optimize_utils.rb', line 80

def self.group
  'macos'
end

.usageObject



76
77
78
# File 'lib/kscript/plugins/kk_mac_optimize_utils.rb', line 76

def self.usage
  'kscript mac_optimize'
end

Instance Method Details

#optimize_allObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kscript/plugins/kk_mac_optimize_utils.rb', line 26

def optimize_all
  logger.kinfo('๐Ÿ”ง Starting macOS system optimization...')

  # Lower priority for OrbStack Helper
  orb_pid = `pgrep -f "OrbStack Helper"`.strip
  unless orb_pid.empty?
    logger.kinfo("๐Ÿ›‘ Found OrbStack Helper (PID: #{orb_pid}), lowering priority...")
    system("sudo renice +15 #{orb_pid}")
    logger.kinfo('โœ… Priority lowered')
  end

  # Close background apps
  apps = ['Telegram', 'Google Chrome']
  apps.each do |app|
    if system("pgrep -x \"#{app}\" > /dev/null")
      logger.kinfo("๐Ÿ›‘ Closing #{app}...")
      system("osascript -e 'tell application \"#{app}\" to quit'")
    end
  end

  # Purge memory cache
  logger.kinfo('๐Ÿงน Purging memory cache...')
  system('sudo purge')
  logger.kinfo('โœ… Memory cache purged')

  # Enable Low Power Mode (macOS 12+)
  macos_version = `sw_vers -productVersion`.strip
  if macos_version.split('.').first.to_i >= 12
    logger.kinfo('โšก Enabling Low Power Mode...')
    system('pmset -a lowpowermode 1')
    logger.kinfo('โœ… Low Power Mode enabled')
  else
    logger.kwarn("โš ๏ธ Your macOS version (#{macos_version}) does not support Low Power Mode, skipping.")
  end

  # Show CPU temperature (requires osx-cpu-temp)
  if system('which osx-cpu-temp > /dev/null')
    temp = `osx-cpu-temp`.strip
    logger.kinfo("๐ŸŒก Current CPU Temperature: #{temp}")
  else
    logger.kinfo("โ„น๏ธ Install 'osx-cpu-temp' to see CPU temperature (brew install osx-cpu-temp)")
  end

  logger.kinfo('๐ŸŽ‰ Optimization complete. Monitor your system for improvements!')
end

#run(*args, **_opts) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/kscript/plugins/kk_mac_optimize_utils.rb', line 16

def run(*args, **_opts)
  with_error_handling do
    if %w[help --help -h].include?(args[0]&.to_s)
      logger.kwarn("Usage: #{self.class.usage}")
    else
      optimize_all
    end
  end
end