<commit_before>-- Detect layout change
function handleLayoutChange(layout)
  if layout then
    local screenId = hs.window.focusedWindow():screen():id()
    print('current display ' .. screenId)
  end
end

hs.screen.watcher.newWithActiveScreen(handleLayoutChange):start()
print('Monitor-Monitor loaded')<commit_msg>feat(Hammerspoon): Monitor-Monitor code more or less solid<commit_after>-- Detect layout change
function handleLayoutChange(layout)
  if layout then
    local screenId = hs.window.focusedWindow():screen():id()
    setActiveScreen(screenId)
    print('current display ' .. screenId)
  end
end

function setActiveScreen(screenId)
  local devicePath = getSerialOutputDevice()
  if devicePath == "" then return end

  local file = assert(io.open(devicePath, "w"))
  file:write("set " .. screenId .. "\n")
  file:flush()
  file:close()
end

function getSerialOutputDevice()
  local command = "ioreg -c IOSerialBSDClient -r -t " ..
    "| awk 'f;/Photon/{f=1};/IOCalloutDevice/{exit}' " ..
    "| sed -n 's/.*\"\\(\\/dev\\/.*\\)\".*/\\1/p'"
  local handle = io.popen(command)
  local result = handle:read("*a")
  handle:close()
  -- Strip whitespace - https://www.rosettacode.org/wiki/Strip_whitespace_from_a_string/Top_and_tail#Lua
  return result:match("^%s*(.-)%s*$")
end

hs.screen.watcher.newWithActiveScreen(handleLayoutChange):start()
handleLayoutChange(true) -- set the initial screen
print('Monitor-Monitor loaded')
{'fraction_non_alphanumeric': 0.15491974877878575, 'fraction_numerical': 0.0013956734124214933, 'mean_word_length': 4.473282442748092, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3395802', 'n_tokens_mistral': 464, 'n_tokens_neox': 456, 'n_words': 98}