【LUA】转载github自用二改模版——调节音量、显示七日天气、历史剪贴板、系统信息显示

本文主要是介绍【LUA】转载github自用二改模版——调节音量、显示七日天气、历史剪贴板、系统信息显示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

二改模版笔记

自动重新加载HS

function reloadConfig(files)doReload = falsefor _,file in pairs(files) doif file:sub(-4) == ".lua" thendoReload = trueendendif doReload thenhs.reload()end
end
myWatcher = hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig):start()
hs.alert.show("Config loaded")

调节音量

function changeVolume(diff)return function()local current = hs.audiodevice.defaultOutputDevice():volume()local new = math.min(100, math.max(0, math.floor(current + diff)))if new > 0 thenhs.audiodevice.defaultOutputDevice():setMuted(false)endhs.alert.closeAll(0.0)hs.alert.show("Volume " .. new .. "%", {}, 0.5)hs.audiodevice.defaultOutputDevice():setVolume(new)end
endhs.hotkey.bind({"shift","ctrl",'command'}, 'Down', changeVolume(-3))
hs.hotkey.bind({"shift","ctrl",'command'}, 'Up', changeVolume(3))

显示七日天气

local urlApi = 'http://v1.yiketianqi.com/free/api'
local menubar = hs.menubar.new()
local menuData = {}local weaEmoji = {lei = '🌩️',qing = '☀️',shachen = '😷',wu = '🌫',xue = '❄️',yu = '🌧',yujiaxue = '🌨',yun = '☁️',zhenyu = '🌧',yin = '⛅️',default = ''
}function updateMenubar()menubar:setTooltip("Weather Info")menubar:setMenu(menuData)
endfunction getWeather()hs.http.doAsyncRequest(urlApi, "GET", nil,nil, function(code, body, htable)if code ~= 200 thenprint('get weather error:'..code)returnendrawjson = hs.json.decode(body)city = rawjson.citymenuData = {}for k, v in pairs(rawjson.data) doif k == 1 thenmenubar:setTitle(weaEmoji[v.wea_img])if v.win_speed == "<3级" thentitlestr = string.format("%s  %s %s  🌡️%s-%s°C", city,weaEmoji[v.wea_img], v.wea, v.tem_night, v.tem_day)elsetitlestr = string.format("%s  %s 🌡️%s-%s°C  💨%s %s %s", city,weaEmoji[v.wea_img],v.tem_night, v.tem_day, v.win_speed,  v.win, v.wea)enditem = { title = titlestr }table.insert(menuData, item)table.insert(menuData, {title = '-'})elseif v.win_speed == "<3级" thentitlestr = string.format("%s  %s 🌡️%s-%s°C     %s",  v.date, weaEmoji[v.wea_img], v.tem_night, v.tem_day, v.wea)elsetitlestr = string.format("%s  %s 🌡️%s-%s°C  💨%s %s %s",  v.date, weaEmoji[v.wea_img],v.tem_night, v.tem_day, v.win_speed, v.win, v.wea)enditem = { title = titlestr }table.insert(menuData, item)endendupdateMenubar()end)
endmenubar:setTitle('👀')
getWeather()
updateMenubar()
hs.timer.doEvery(3000, getWeather)

剪贴板

--[[From https://github.com/victorso/.hammerspoon/blob/master/tools/clipboard.luaModified by Diego Zamboni
]]---- Feel free to change those settings
local frequency = 0.8 -- Speed in seconds to check for clipboard changes. If you check too frequently, you will loose performance, if you check sparsely you will loose copies
local hist_size = 100 -- How many items to keep on history
local label_length = 70 -- How wide (in characters) the dropdown menu should be. Copies larger than this will have their label truncated and end with "…" (unicode for elipsis ...)
local honor_clearcontent = false --asmagill request. If any application clears the pasteboard, we also remove it from the history https://groups.google.com/d/msg/hammerspoon/skEeypZHOmM/Tg8QnEj_N68J
local pasteOnSelect = false -- Auto-type on click-- Don't change anything bellow this line
local jumpcut = hs.menubar.new()
jumpcut:setTooltip("Clipboard history")
local pasteboard = require("hs.pasteboard") -- http://www.hammerspoon.org/docs/hs.pasteboard.html
local settings = require("hs.settings") -- http://www.hammerspoon.org/docs/hs.settings.html
local last_change = pasteboard.changeCount() -- displays how many times the pasteboard owner has changed // Indicates a new copy has been made--Array to store the clipboard history
local clipboard_history = settings.get("so.victor.hs.jumpcut") or {} --If no history is saved on the system, create an empty historyfunction subStringUTF8(str, startIndex, endIndex)if startIndex < 0 thenstartIndex = subStringGetTotalIndex(str) + startIndex + 1endif endIndex ~= nil and endIndex < 0 thenendIndex = subStringGetTotalIndex(str) + endIndex + 1endif endIndex == nil then return string.sub(str, subStringGetTrueIndex(str, startIndex))elsereturn string.sub(str, subStringGetTrueIndex(str, startIndex), subStringGetTrueIndex(str, endIndex + 1) - 1)end
end--返回当前截取字符串正确下标
function subStringGetTrueIndex(str, index)local curIndex = 0local i = 1local lastCount = 1repeat lastCount = subStringGetByteCount(str, i)i = i + lastCountcurIndex = curIndex + 1until(curIndex >= index)return i - lastCount
end--返回当前字符实际占用的字符数
function subStringGetByteCount(str, index)local curByte = string.byte(str, index)local byteCount = 1if curByte == nil thenbyteCount = 0elseif curByte > 0 and curByte <= 127 thenbyteCount = 1elseif curByte>=192 and curByte<=223 thenbyteCount = 2elseif curByte>=224 and curByte<=239 thenbyteCount = 3elseif curByte>=240 and curByte<=247 thenbyteCount = 4endreturn byteCount
end-- Append a history counter to the menu
function setTitle()if (#clipboard_history == 0) thenjumpcut:setTitle("✂") -- Unicode magicelsejumpcut:setTitle("✂") -- Unicode magic--      jumpcut:setTitle("✂ ("..#clipboard_history..")") -- updates the menu counterend
endfunction putOnPaste(string,key)if (pasteOnSelect) thenhs.eventtap.keyStrokes(string)pasteboard.setContents(string)last_change = pasteboard.changeCount()elseif (key.alt == true) then -- If the option/alt key is active when clicking on the menu, perform a "direct paste", without changing the clipboardhs.eventtap.keyStrokes(string) -- Defeating paste blocking http://www.hammerspoon.org/go/#pasteblockelsepasteboard.setContents(string)last_change = pasteboard.changeCount() -- Updates last_change to prevent item duplication when putting on pasteendend
end-- Clears the clipboard and history
function clearAll()pasteboard.clearContents()clipboard_history = {}settings.set("so.victor.hs.jumpcut",clipboard_history)now = pasteboard.changeCount()setTitle()
end-- Clears the last added to the history
function clearLastItem()table.remove(clipboard_history,#clipboard_history)settings.set("so.victor.hs.jumpcut",clipboard_history)now = pasteboard.changeCount()setTitle()
endfunction pasteboardToClipboard(item)-- Loop to enforce limit on qty of elements in history. Removes the oldest itemswhile (#clipboard_history >= hist_size) dotable.remove(clipboard_history,1)endtable.insert(clipboard_history, item)settings.set("so.victor.hs.jumpcut",clipboard_history) -- updates the saved historysetTitle() -- updates the menu counter
end-- Dynamic menu by cmsj https://github.com/Hammerspoon/hammerspoon/issues/61#issuecomment-64826257
populateMenu = function(key)setTitle() -- Update the counter every time the menu is refreshedmenuData = {}if (#clipboard_history == 0) thentable.insert(menuData, {title="None", disabled = true}) -- If the history is empty, display "None"elsefor k,v in pairs(clipboard_history) doif (string.len(v) > label_length) thentable.insert(menuData,1, {title=subStringUTF8(v,0,label_length).."…", fn = function() putOnPaste(v,key) end }) -- Truncate long stringselsetable.insert(menuData,1, {title=v, fn = function() putOnPaste(v,key) end })end -- end if elseend-- end forend-- end if else-- footertable.insert(menuData, {title="-"})table.insert(menuData, {title="Clear All", fn = function() clearAll() end })if (key.alt == true or pasteOnSelect) thentable.insert(menuData, {title="Direct Paste Mode ✍", disabled=true})endreturn menuData
end-- If the pasteboard owner has changed, we add the current item to our history and update the counter.
function storeCopy()now = pasteboard.changeCount()if (now > last_change) thencurrent_clipboard = pasteboard.getContents()-- asmagill requested this feature. It prevents the history from keeping items removed by password managersif (current_clipboard == nil and honor_clearcontent) thenclearLastItem()elsepasteboardToClipboard(current_clipboard)endlast_change = nowend
end--Checks for changes on the pasteboard. Is it possible to replace with eventtap?
timer = hs.timer.new(frequency, storeCopy)
timer:start()setTitle() --Avoid wrong title if the user already has something on his saved history
jumpcut:setMenu(populateMenu)hs.hotkey.bind({"cmd", "shift"}, "v", function() jumpcut:popupMenu(hs.mouse.getAbsolutePosition()) end)

系统信息显示

--- 显示系统信息
--- 可显示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 thenlocal interface_detail = hs.network.interfaceDetails(interface)if interface_detail.IPv4 thenlocal ipv4 = interface_detail.IPv4.Addresses[1]table.insert(menuData, {title = "IPv4:" .. ipv4,tooltip = "Copy Ipv4 to clipboard",fn = function()hs.pasteboard.setContents(ipv4)end})endlocal 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})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}\'')elseobj.last_down =  0obj.last_down =  0endlocal date=os.date("%Y-%m-%d %a");table.insert(menuData, {title = 'Date: '..date,tooltip = 'Copy Now DateTime',fn = function()hs.pasteboard.setContents(os.date("%Y-%m-%d %H:%M:%S"))end})table.insert(menuData, {title = '打开:监  视  器    (⇧⌃A)',tooltip = 'Show Activity Monitor',fn = function()bindActivityMonitorKey()end})table.insert(menuData, {title = '打开:磁盘工具    (⇧⌃D)',tooltip = 'Show Disk Utility',fn = function()bindDiskKey()end})table.insert(menuData, {title = '打开:系统日历    (⇧⌃C)',tooltip = 'Show calendar',fn = function()bindCalendarKey()end})menubaritem:setMenu(menuData)
endfunction scan()if interface thenobj.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}\'')elseobj.current_down  = 0obj.current_up = 0endobj.cpu_used = getCpu()obj.disk_used = getRootVolumes()obj.mem_used = getVmStats()obj.down_bytes = obj.current_down - obj.last_downobj.up_bytes = obj.current_up - obj.last_upobj.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_downobj.last_up = obj.current_uplocal 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
endfunction format_speed(bytes)-- 单位 Byte/sif bytes < 1024 thenreturn string.format('%6.0f', bytes) .. ' B/s'else-- 单位 KB/sif bytes < 1048576 then-- 因为是每两秒刷新一次,所以要除以 (1024 * 2)return string.format('%6.1f', bytes / 2048) .. ' KB/s'-- 单位 MB/selse-- 除以 (1024 * 1024 * 2)return string.format('%6.1f', bytes / 2097152) .. ' MB/s'endend
endfunction getCpu()local data = hs.host.cpuUsage()local cpu = (data["overall"]["active"])return formatPercent(cpu)
endfunction 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--第三种方法,由于部分设备pageSize获取不正确,所以只能通过已使用页数+缓存页数+空闲页数计算总页数local megsUsed =  vmStats.pagesWiredDown -- 联动内存megsUsed = megsUsed + vmStats.pagesUsedByVMCompressor -- 被压缩内存megsUsed = megsUsed + vmStats.pagesActive +vmStats.pagesSpeculative -- APP内存local megsCached = vmStats.fileBackedPages   --缓存内存local freeMegs = vmStats.pagesFree   --空闲内存local totalMegs = megsUsed + megsCached + freeMegslocal usedMem = megsUsed/totalMegs * 100return formatPercent(usedMem)
endfunction getRootVolumes()local vols = hs.fs.volume.allVolumes()for key, vol in pairs(vols) dolocal size = vol.NSURLVolumeTotalCapacityKeylocal free = vol.NSURLVolumeAvailableCapacityKeylocal usedSSD = (1-free/size) * 100if ( string.find(vol.NSURLVolumeNameKey,'Macintosh') ~= nil) thenreturn formatPercent(usedSSD)endendreturn ' 0%'
endfunction formatPercent(percent)if ( percent <= 0 ) thenreturn "  1%"elseif ( percent < 10 ) thenreturn "  " .. string.format("%.f", percent) .. "%"elseif  (percent > 99 )thenreturn "100%"elsereturn string.format("%.f", percent) .. "%"end
endlocal setSysInfo= function()--    if config ~=nil and config[1].showSysInfo ~= 'on' thenif 1 thenif(menuBarItem ~= nil and menuBarItem:isInMenuBar() == false) thenreturnendif (menuBarItem == nil) thenprint("设置状态栏:系统信息")menuBarItem= hs.menubar.new()elseif (menuBarItem:isInMenuBar() == false) thenmenuBarItem:delete()menuBarItem= hs.menubar.new()endinit()scan()if obj.timer thenobj.timer:stop()obj.timer = nilend-- 三秒刷新一次obj.timer = hs.timer.doEvery(3, scan):start()end
endfunction initData()setSysInfo()--监听系统信息开关的状态,判断是否要重置hs.timer.doEvery(1, setSysInfo)
end-- 初始化
initData()

这篇关于【LUA】转载github自用二改模版——调节音量、显示七日天气、历史剪贴板、系统信息显示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/656720

相关文章

C#读取本地网络配置信息全攻略分享

《C#读取本地网络配置信息全攻略分享》在当今数字化时代,网络已深度融入我们生活与工作的方方面面,对于软件开发而言,掌握本地计算机的网络配置信息显得尤为关键,而在C#编程的世界里,我们又该如何巧妙地读取... 目录一、引言二、C# 读取本地网络配置信息的基础准备2.1 引入关键命名空间2.2 理解核心类与方法

什么是cron? Linux系统下Cron定时任务使用指南

《什么是cron?Linux系统下Cron定时任务使用指南》在日常的Linux系统管理和维护中,定时执行任务是非常常见的需求,你可能需要每天执行备份任务、清理系统日志或运行特定的脚本,而不想每天... 在管理 linux 服务器的过程中,总有一些任务需要我们定期或重复执行。就比如备份任务,通常会选在服务器资

电脑显示hdmi无信号怎么办? 电脑显示器无信号的终极解决指南

《电脑显示hdmi无信号怎么办?电脑显示器无信号的终极解决指南》HDMI无信号的问题却让人头疼不已,遇到这种情况该怎么办?针对这种情况,我们可以采取一系列步骤来逐一排查并解决问题,以下是详细的方法... 无论你是试图为笔记本电脑设置多个显示器还是使用外部显示器,都可能会弹出“无HDMI信号”错误。此消息可能

TP-LINK/水星和hasivo交换机怎么选? 三款网管交换机系统功能对比

《TP-LINK/水星和hasivo交换机怎么选?三款网管交换机系统功能对比》今天选了三款都是”8+1″的2.5G网管交换机,分别是TP-LINK水星和hasivo交换机,该怎么选呢?这些交换机功... TP-LINK、水星和hasivo这三台交换机都是”8+1″的2.5G网管交换机,我手里的China编程has

使用Python检查CPU型号并弹出警告信息

《使用Python检查CPU型号并弹出警告信息》本教程将指导你如何编写一个Python程序,该程序能够在启动时检查计算机的CPU型号,如果检测到CPU型号包含“I3”,则会弹出一个警告窗口,感兴趣的小... 目录教程目标方法一所需库步骤一:安装所需库步骤二:编写python程序步骤三:运行程序注意事项方法二

基于Qt实现系统主题感知功能

《基于Qt实现系统主题感知功能》在现代桌面应用程序开发中,系统主题感知是一项重要的功能,它使得应用程序能够根据用户的系统主题设置(如深色模式或浅色模式)自动调整其外观,Qt作为一个跨平台的C++图形用... 目录【正文开始】一、使用效果二、系统主题感知助手类(SystemThemeHelper)三、实现细节

CentOS系统使用yum命令报错问题及解决

《CentOS系统使用yum命令报错问题及解决》文章主要讲述了在CentOS系统中使用yum命令时遇到的错误,并提供了个人解决方法,希望对大家有所帮助,并鼓励大家支持脚本之家... 目录Centos系统使用yum命令报错找到文件替换源文件为总结CentOS系统使用yum命令报错http://www.cppc

PostgreSQL如何查询表结构和索引信息

《PostgreSQL如何查询表结构和索引信息》文章介绍了在PostgreSQL中查询表结构和索引信息的几种方法,包括使用`d`元命令、系统数据字典查询以及使用可视化工具DBeaver... 目录前言使用\d元命令查看表字段信息和索引信息通过系统数据字典查询表结构通过系统数据字典查询索引信息查询所有的表名可

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

基于人工智能的图像分类系统

目录 引言项目背景环境准备 硬件要求软件安装与配置系统设计 系统架构关键技术代码示例 数据预处理模型训练模型预测应用场景结论 1. 引言 图像分类是计算机视觉中的一个重要任务,目标是自动识别图像中的对象类别。通过卷积神经网络(CNN)等深度学习技术,我们可以构建高效的图像分类系统,广泛应用于自动驾驶、医疗影像诊断、监控分析等领域。本文将介绍如何构建一个基于人工智能的图像分类系统,包括环境