Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto generate config.yaml on shell start #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ views:
command: ls -la
```

For generating view from `config.yml` file go to its folder:
To apply changes, either restart `zsh`, resource `~/.zshrc` by running `source ~/.zshrc`, or go to its folder:

```sh
cd $ZSH_CUSTOM/plugins/zsh-apple-touchbar
```

and run `generate.rb` file:
and run `source zsh-apply-touchbar.zsh`,
or run `generate.rb` file:

```sh
ruby generate.rb
Expand Down
7 changes: 2 additions & 5 deletions generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ def self.call(*args)
new(*args).call
end

def initialize(file_path = 'config.yml')
def initialize(file_path = __dir__+'/config.yml')
@file_path = file_path
end

def call
[].tap do |code|
code << 'source ${0:A:h}/functions.zsh'
code << "set_state '#{config['default_view']}'"
code << views_functions
code << define_views_functions
code << main_function
code << 'autoload -Uz add-zsh-hook'
code << "add-zsh-hook precmd precmd_apple_touchbar\n"
end.join("\n\n")
end

Expand Down Expand Up @@ -74,4 +71,4 @@ def config
end
end

File.open('zsh-apple-touchbar.zsh', 'w') {|f| f.write(Parser.call) }
File.open(__dir__+'/touchbar-mappings.zsh', 'w') {|f| f.write(Parser.call) }
43 changes: 43 additions & 0 deletions touchbar-mappings.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
set_state 'first'

function first_view() {
remove_and_unbind_keys

set_state 'first'

create_key 1 '👉 pwd' 'pwd |tr -d "\\n" |pbcopy' '-s'
create_key 2 'second view' 'second_view'
create_key 3 'third view' 'third_view'
}

function second_view() {
remove_and_unbind_keys

set_state 'second'

create_key 1 '👈 back' 'first_view'
create_key 2 'current path' 'pwd' '-s'

set_state 'first'
}

function third_view() {
remove_and_unbind_keys

set_state 'third'

create_key 1 '👈 back' 'first_view'
create_key 2 'ls' 'ls -la' '-s'
}

zle -N first_view
zle -N second_view
zle -N third_view

precmd_apple_touchbar() {
case $state in
first) first_view ;;
second) second_view ;;
third) third_view ;;
esac
}
44 changes: 2 additions & 42 deletions zsh-apple-touchbar.zsh
Original file line number Diff line number Diff line change
@@ -1,48 +1,8 @@
source ${0:A:h}/functions.zsh

set_state 'first'
ruby ${0:A:h}/generate.rb

function first_view() {
remove_and_unbind_keys

set_state 'first'

create_key 1 '👉 pwd' 'pwd |tr -d "\\n" |pbcopy' '-s'
create_key 2 'second view' 'second_view'
create_key 3 'third view' 'third_view'
}

function second_view() {
remove_and_unbind_keys

set_state 'second'

create_key 1 '👈 back' 'first_view'
create_key 2 'current path' 'pwd' '-s'

set_state 'first'
}

function third_view() {
remove_and_unbind_keys

set_state 'third'

create_key 1 '👈 back' 'first_view'
create_key 2 'ls' 'ls -la' '-s'
}

zle -N first_view
zle -N second_view
zle -N third_view

precmd_apple_touchbar() {
case $state in
first) first_view ;;
second) second_view ;;
third) third_view ;;
esac
}
source ${0:A:h}/touchbar-mappings.zsh

autoload -Uz add-zsh-hook

Expand Down