From 36f5175b6f690d048de7d77fe5f2490ed56f37f5 Mon Sep 17 00:00:00 2001 From: sugood <15820258199@163.com> Date: Fri, 14 Jan 2022 22:28:28 +0800 Subject: [PATCH] =?UTF-8?q?v0.1.4=201=E3=80=81=E8=B0=83=E6=95=B4APP?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E5=BF=AB=E6=8D=B7=E9=94=AE=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0shift+ctrl+D=E6=89=93=E5=BC=80=E7=A3=81=E7=9B=98?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=EF=BC=8C=E5=88=A0=E9=99=A4=E6=89=93=E5=BC=80?= =?UTF-8?q?IDEA=E7=9A=84=E5=BF=AB=E6=8D=B7=E9=94=AE=EF=BC=8C=E5=B0=BD?= =?UTF-8?q?=E9=87=8F=E5=8F=AA=E4=BF=9D=E7=95=99=E7=B3=BB=E7=BB=9F=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E7=9A=84=E5=BF=AB=E9=94=AE=E9=94=AE=202=E3=80=81?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=8A=B6=E6=80=81=E6=A0=8F=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E4=BF=A1=E6=81=AF=E7=9A=84=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E3=80=82=E5=8F=AF=E6=98=BE=E7=A4=BACPU=E3=80=81=E5=86=85?= =?UTF-8?q?=E5=AD=98=E3=80=81=E7=A1=AC=E7=9B=98=E5=8D=A0=E7=94=A8=E5=92=8C?= =?UTF-8?q?=E5=AE=9E=E6=97=B6=E7=BD=91=E7=BB=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + data/initConfig.json | 1 + init.lua | 4 +- modules/commons.lua | 16 ++- modules/launcher.lua | 14 +-- modules/systemInfo.lua | 264 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 290 insertions(+), 10 deletions(-) create mode 100644 modules/systemInfo.lua diff --git a/.gitignore b/.gitignore index 91290a5..59ece68 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /.idea/ /*.zip .idea/ +build/ \ No newline at end of file diff --git a/data/initConfig.json b/data/initConfig.json index 5b564c3..5d348e1 100644 --- a/data/initConfig.json +++ b/data/initConfig.json @@ -5,6 +5,7 @@ "isSyncTime" : "off", "rootPassword" : "", "caffeine" : "off", + "showSysInfo" : "on", "baiduAppSecret" : "eI3BLUVURG5Zzb8Tdgdm", "youdaoAppSecret" : "", "baiduAppid" : "20201017000591957" diff --git a/init.lua b/init.lua index 466028b..448be99 100644 --- a/init.lua +++ b/init.lua @@ -7,10 +7,10 @@ require "modules/commons" require "modules/system" require "modules/windows" require "modules/launcher" +require "modules/systemInfo" require "modules/snippet" require "modules/timesync" require "modules/caffeine" require "modules/inputstat" require "modules/dict" -require "modules/jsonFormat" - +require "modules/jsonFormat" \ No newline at end of file diff --git a/modules/commons.lua b/modules/commons.lua index 03b396c..410f71f 100644 --- a/modules/commons.lua +++ b/modules/commons.lua @@ -4,7 +4,7 @@ --- DateTime: 2020/10/24 14:13 --- local console = require("hs.console") -version = "v0.1.3" +version = "v0.1.4" configPath= "~/.hammerspoon/data/config.json" initConfigPath= "~/.hammerspoon/data/initConfig.json" config = {} @@ -64,6 +64,16 @@ function switchCaffeine() hs.reload() end +function switchSysInfo() + if config[1].showSysInfo == 'on' then + config[1].showSysInfo = 'off' + else + config[1].showSysInfo = 'on' + end + hs.json.write(config,configPath, true, true) + hs.reload() +end + --设置全局菜单栏 function initMenu() macMenubar = hs.menubar.new() @@ -131,6 +141,10 @@ function initMenu() switchCaffeine() end }, { title = "-" }, + { title = "系统状态:" .. config[1].showSysInfo, fn = function() + switchSysInfo() + end }, + { title = "-" }, { title = "打开键盘偏好设置", fn = function() hs.osascript.applescript([[ tell application "System Preferences" reveal anchor "InputSources" of pane "com.apple.preference.keyboard" diff --git a/modules/launcher.lua b/modules/launcher.lua index 7fa2eef..fd3e17b 100644 --- a/modules/launcher.lua +++ b/modules/launcher.lua @@ -8,14 +8,14 @@ local fnutils = require "hs.fnutils" grid.setMargins({0, 0}) applist = { - {shortcut = 'I',appname = 'IntelliJ IDEA'}, - {shortcut = 'T',appname = 'Terminal'}, - {shortcut = 'Z',appname = 'Calculator'}, - {shortcut = 'D',appname = 'Finder'}, - {shortcut = 'Q',appname = 'Activity Monitor'}, - {shortcut = 'S',appname = 'System Preferences'}, - {shortcut = 'C',appname = 'Google Chrome'}, {shortcut = 'A',appname = 'Launchpad'}, + {shortcut = 'C',appname = 'Google Chrome'}, + {shortcut = 'D',appname = 'Disk Utility'}, + {shortcut = 'E',appname = 'Finder'}, + {shortcut = 'Q',appname = 'Activity Monitor'}, + {shortcut = 'T',appname = 'Terminal'}, + {shortcut = 'S',appname = 'System Preferences'}, + {shortcut = 'Z',appname = 'Calculator'}, } fnutils.each(applist, function(entry) diff --git a/modules/systemInfo.lua b/modules/systemInfo.lua new file mode 100644 index 0000000..c5733be --- /dev/null +++ b/modules/systemInfo.lua @@ -0,0 +1,264 @@ +--- +--- 显示系统信息 +--- 可显示CPU\内存\硬盘\网络等实时信息 +--- Created by sugood(https://github.com/sugood). +--- DateTime: 2022/01/14 22:00 +--- + +local menubaritem = hs.menubar.new() +local menuData = {} + +-- ipv4Interface ipv6 Interface +local interface = hs.network.primaryInterfaces() + +-- 该对象用于存储全局变量,避免每次获取速度都创建新的局部变量 +local obj = {} + +function init() + if interface then + local interface_detail = hs.network.interfaceDetails(interface) + if interface_detail.IPv4 then + local ipv4 = interface_detail.IPv4.Addresses[1] + table.insert(menuData, { + title = "IPv4:" .. ipv4, + tooltip = "Copy Ipv4 to clipboard", + fn = function() + hs.pasteboard.setContents(ipv4) + end + }) + end + local mac = hs.execute('ifconfig ' .. interface .. ' | grep ether | awk \'{print $2}\'') + table.insert(menuData, { + title = 'MAC:' .. mac, + tooltip = 'Copy MAC to clipboard', + fn = function() + hs.pasteboard.setContents(mac) + end + }) + table.insert(menuData, { + title = 'Activity Monitor', + tooltip = 'Show Activity Monitor', + fn = function() + bindActivityMonitorKey() + end + }) + table.insert(menuData, { + title = 'Disk Utility', + tooltip = 'Show Disk Utility', + fn = function() + bindDiskKey() + end + }) + + obj.last_down = hs.execute('netstat -ibn | grep -e ' .. interface .. ' -m 1 | awk \'{print $7}\'') + obj.last_up = hs.execute('netstat -ibn | grep -e ' .. interface .. ' -m 1 | awk \'{print $10}\'') + end + + menubaritem:setMenu(menuData) +end + +function scan() + if interface then + obj.current_down = hs.execute('netstat -ibn | grep -e ' .. interface .. ' -m 1 | awk \'{print $7}\'') + obj.current_up = hs.execute('netstat -ibn | grep -e ' .. interface .. ' -m 1 | awk \'{print $10}\'') + + obj.cpu_used = getCpu() + obj.disk_used = getRootVolumes() + obj.mem_used = getVmStats() + obj.down_bytes = obj.current_down - obj.last_down + obj.up_bytes = obj.current_up - obj.last_up + + obj.down_speed = format_speed(obj.down_bytes) + obj.up_speed = format_speed(obj.up_bytes) + + obj.display_text = hs.styledtext.new('▲ ' .. obj.up_speed .. '\n'..'▼ ' .. obj.down_speed , {font={size=9}, color={hex='#FFFFFF'}, paragraphStyle={alignment="left", maximumLineHeight=18}}) + obj.display_disk_text = hs.styledtext.new(obj.disk_used ..'\n'.. 'SSD ' , {font={size=9}, color={hex='#FFFFFF'}, paragraphStyle={alignment="left", maximumLineHeight=18}}) + obj.display_mem_text = hs.styledtext.new(obj.mem_used ..'\n'.. 'MEM ' , {font={size=9}, color={hex='#FFFFFF'}, paragraphStyle={alignment="left", maximumLineHeight=18}}) + obj.display_cpu_text = hs.styledtext.new(obj.cpu_used ..'\n'.. 'CPU ' , {font={size=9}, color={hex='#FFFFFF'}, paragraphStyle={alignment="left", maximumLineHeight=18}}) + + obj.last_down = obj.current_down + obj.last_up = obj.current_up + + local canvas = hs.canvas.new{x = 0, y = 0, h = 24, w = 30+30+30+60} + -- canvas[1] = {type = 'text', text = obj.display_text} + canvas:appendElements({ + type = "text", + text = obj.display_cpu_text, + -- withShadow = true, + trackMouseEnterExit = true, + },{ + type = "text", + text = obj.display_disk_text, + -- withShadow = true, + trackMouseEnterExit = true, + frame = { x = 30, y = "0", h = "1", w = "1", } + },{ + type = "text", + text = obj.display_mem_text, + -- withShadow = true, + trackMouseEnterExit = true, + frame = { x = 60, y = "0", h = "1", w = "1", } + },{ + type = "text", + text = obj.display_text, + -- withShadow = true, + trackMouseEnterExit = true, + frame = { x = 90, y = "0", h = "1", w = "1", } + }) + menubaritem:setIcon(canvas:imageFromCanvas()) + canvas:delete() + canvas = nil + end +end + +function format_speed(bytes) + -- 单位 Byte/s + if bytes < 1024 then + return string.format('%6.0f', bytes) .. ' B/s' + else + -- 单位 KB/s + if bytes < 1048576 then + -- 因为是每两秒刷新一次,所以要除以 (1024 * 2) + return string.format('%6.1f', bytes / 2048) .. ' KB/s' + -- 单位 MB/s + else + -- 除以 (1024 * 1024 * 2) + return string.format('%6.1f', bytes / 2097152) .. ' MB/s' + end + end +end + +-- function showAllVolumes() +-- local vols = hs.fs.volume.allVolumes() +-- local msg = "volumes: \n" +-- for key, vol in pairs(vols) do +-- local size = vol.NSURLVolumeTotalCapacityKey +-- local free = vol.NSURLVolumeAvailableCapacityKey +-- msg = msg .. key .. 1-free/size.."\n" +-- end +-- return hs.alert( +-- msg, +-- { +-- ["textSize"] = 12 +-- }, +-- hs.screen.mainScreen(), +-- 12 +-- ) +-- end + +function getCpu() + local data = hs.host.cpuUsage() + local cpu = (data["overall"]["active"]) + return formatPercent(cpu) +end + +function getVmStats() + + local vmStats = hs.host.vmStat() + --1024^2 + local megDiv = 1048576 + local megMulti = vmStats.pageSize / megDiv + + local totalMegs = vmStats.memSize / megDiv --总内存 + local megsCached = vmStats.fileBackedPages * megMulti --缓存内存 + local freeMegs = vmStats.pagesFree * megMulti --空闲内存 + + --第一种方法使用 APP内存+联动内存+被压缩内存 = 已使用内存 + -- local megsUsed = vmStats.pagesWiredDown * megMulti -- 联动内存 + -- megsUsed = megsUsed + vmStats.pagesUsedByVMCompressor * megMulti -- 被压缩内存 + -- megsUsed = megsUsed + (vmStats.pagesActive +vmStats.pagesSpeculative)* megMulti -- APP内存 + + --第二种方法使用 总内存-缓存内存-空闲内存 = 已使用内存 + local megsUsed = totalMegs - megsCached - freeMegs + + local usedMem = megsUsed/totalMegs * 100 + return formatPercent(usedMem) +end + +function getRootVolumes() + local vols = hs.fs.volume.allVolumes() + local msg = "" + for key, vol in pairs(vols) do + local size = vol.NSURLVolumeTotalCapacityKey + local free = vol.NSURLVolumeAvailableCapacityKey + local usedSSD = (1-free/size) * 100 + -- msg = msg .. (1-free/size) * 100 + return formatPercent(usedSSD) + end +end + +function formatPercent(percent) + if ( percent <= 0 ) then + return " 1%" + elseif ( percent < 10 ) then + return " " .. string.format("%.f", percent) .. "%" + elseif (percent > 99 )then + return "100%" + else + return string.format("%.f", percent) .. "%" + end +end + +function resetSysInfoMeun() + if(config ~=nil and config[1].showSysInfo == 'on' and menuBarItem:isInMenuBar() == false) then + print("重置状态栏") + menuBarItem:delete() + menuBarItem= hs.menubar.new() + -- menuBarItem:setTitle("") + -- menuBarItem:setIcon("~/.hammerspoon/icon/caffeine-on.pdf") + --hs.caffeinate.set("displayIdle", true) + -- queryCpu() + init() + scan() + if obj.timer then + obj.timer:stop() + obj.timer = nil + end + -- 第三个参数表示当发生异常情况时,定时器是否继续执行下去 + obj.timer = hs.timer.doEvery(2, scan):start() + else + menuBarItem:delete() + end +end + +local setSysInfo= function() + if config ~=nil and config[1].showSysInfo == 'on' and menuBarItem == nil then + print("设置状态栏:系统信息") + menuBarItem= hs.menubar.new() + + -- menuBarItem:setTitle("") + -- menuBarItem:setIcon("~/.hammerspoon/icon/caffeine-on.pdf") + -- queryCpu() + init() + scan() + if obj.timer then + obj.timer:stop() + obj.timer = nil + end + -- 第三个参数表示当发生异常情况时,定时器是否继续执行下去 + obj.timer = hs.timer.doEvery(3, scan):start() + + -- else + -- hs.caffeinate.set("displayIdle", false) + end +end + + +function initData() + setSysInfo() + --监听系统信息开关的状态,判断是否要重置 + hs.timer.doEvery(1, resetSysInfoMeun) + -- showAllVolumes() +end + +-- 初始化 +initData() + +-- 按下添加快捷键时映射到活动监视器快捷键 +function bindActivityMonitorKey() + hs.eventtap.keyStroke({ "ctrl", "shift"}, "Q") +end +-- 按下添加快捷键时映射到磁盘工具快捷键 +function bindDiskKey() + hs.eventtap.keyStroke({ "ctrl", "shift"}, "D") +end \ No newline at end of file