Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
keidax committed Nov 26, 2019
0 parents commit f0de710
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*.cr]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/docs/
/bin/
/.shards/
*.dwarf

/lib/hoop
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: crystal

# Uncomment the following if you'd like Travis to run specs and check code formatting
# script:
# - crystal spec
# - crystal tool format --check
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2019 Gabriel Holodak

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# theme-watcher

TODO: Write a description here

## Installation

TODO: Write installation instructions here

## Usage

TODO: Write usage instructions here

## Development

TODO: Write development instructions here

## Contributing

1. Fork it (<https://github.com/your-github-user/theme-watcher/fork>)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request

## Contributors

- [Gabriel Holodak](https://github.com/your-github-user) - creator and maintainer
6 changes: 6 additions & 0 deletions shard.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 1.0
shards:
hoop:
path: ~/code/hoop
version: 0

17 changes: 17 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: theme-watcher
version: 0.1.0

authors:
- Gabriel Holodak <[email protected]>

targets:
theme-watcher:
main: src/theme-watcher.cr

dependencies:
hoop:
path: ~/code/hoop

crystal: 0.31.1

license: MIT
2 changes: 2 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "spec"
require "../src/theme-watcher"
9 changes: 9 additions & 0 deletions spec/theme-watcher_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "./spec_helper"

describe Theme::Watcher do
# TODO: Write tests

it "works" do
false.should eq(true)
end
end
36 changes: 36 additions & 0 deletions src/ns_extensions.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "hoop"

include Hoop

# NSAutoreleasePool.new
# NSApp.activation_policy = LibAppKit::NSApplicationActivationPolicy::Regular

class NSDistributedNotificationCenter < NSNotificationCenter
# register_class
# static_method "defaultCenter", nil, "NSDistributedNotificationCenter", "default_center"
# TODO: "NSString" should really be "NSNotificationName" but I'm not sure how to make a string pointer
# method "addObserver:selector:name:object:", ["id", "SEL", "NSString", "id"], "void", "add_observer"
end

class NSUserDefaults < NSObject
register_class
static_method "standardUserDefaults", nil, "NSUserDefaults", "standard_user_defaults"
method "stringForKey:", ["NSString"], "SafeString", "string_for_key"
end

class SafeString
@str : String?

# Necessary to safely handle a *NSString
def initialize(ptr : UInt8*)
if ptr.null?
@str = nil
else
@str = NSString.new(ptr).to_s
end
end

def with_default(default : String) String
@str || default
end
end
58 changes: 58 additions & 0 deletions src/theme-watcher.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require "hoop"
require "./ns_extensions"

DARK_THEME = "gruvbox-dark-hard"
LIGHT_THEME = "one-light"


class ShellWriter
@zsh : Process

def initialize
pipe_file = File.open(Path["~/color_pipe.txt"].expand, "wb")
@zsh = Process.new("zsh", ["-s"],
input: Process::Redirect::Pipe,
output: pipe_file,
error: Process::Redirect::Inherit,
)
@zsh.input << %(eval "$($DOTDIR/base16/base16-shell/profile_helper.sh)"\n)
end

def write_theme(theme : String)
@zsh.input << "eval base16_#{theme}\n"
end
end

class ThemeHandler < NSObject
@writer = ShellWriter.new

export_class
action "theme_changed", "notice", "themeChanged:" do
# notification = NSNotification.new(notice)

case current_theme
when /light/i
# 🌝 🌞 πŸŒ• πŸŒ™ 🌜
puts("πŸŒ• #{LIGHT_THEME}")
@writer.write_theme(LIGHT_THEME)
when /dark/i
# 🌚 πŸŒ‘
puts("πŸŒ‘ #{DARK_THEME}")
@writer.write_theme(DARK_THEME)
else
puts("πŸ’€ #{current_theme}")
end
end

def current_theme : String
NSUserDefaults.standard_user_defaults.string_for_key("AppleInterfaceStyle").with_default("Light")
end
end

handler = ThemeHandler.new.to_objc
dnc = NSDistributedNotificationCenter.default_center
dnc.add_observer(handler, "themeChanged:", "AppleInterfaceThemeChangedNotification", nil)

puts "running"
NSApp.run
puts "done"
21 changes: 21 additions & 0 deletions src/theme_setter.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
abstract class ThemeSetter
abstract def set_theme(theme : String)
end

class ShellThemeSetter < ThemeSetter
@zsh : Process

def initialize
pipe_file = File.open(Path["~/color_pipe.txt"].expand, "wb")
@zsh = Process.new("zsh", ["-s"],
input: Process::Redirect::Pipe,
output: pipe_file,
error: Process::Redirect::Inherit,
)
@zsh.input << %(eval "$($DOTDIR/base16/base16-shell/profile_helper.sh)"\n)
end

def set_theme(theme : String)
@zsh.input << "eval base16_#{theme}\n"
end
end

0 comments on commit f0de710

Please sign in to comment.