Skip to content

Commit

Permalink
preliminary terminal role. Closes #64. Ref #71
Browse files Browse the repository at this point in the history
  • Loading branch information
joemaller committed Jun 17, 2018
1 parent cedd468 commit fb7ff13
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 25 deletions.
35 changes: 19 additions & 16 deletions admin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@
- { role: finder, tags: finder, when: user_lib is defined and user_lib_prefs is defined }
- { role: dock, tags: dock, when: user_lib is defined and user_lib_prefs is defined }
- { role: safari, tags: safari, when: user_lib is defined and user_lib_prefs is defined }
- { role: homebrew, tags: [homebrew], apps: "{{ homebrew_apps }}" }
- { role: homebrew_cask, tags: [homebrew, cask], apps: "{{ cask_apps }}" }
- { role: terminal, tags: [terminal], become: no }
- { role: homebrew, apps: "{{ homebrew_apps }}", become: no }
- { role: homebrew_cask, apps: "{{ cask_apps }}", become: no }
- { role: node.js }
- { role: vagrant, vagrant_user: "{{ username }}", tags: [vagrant] }
- { role: sublime-text }
Expand Down Expand Up @@ -172,27 +173,29 @@
recurse=yes
become: yes

post_tasks:


# post_tasks:
# - name: Install virtualenvwrapper
# pip: name=virtualenvwrapper
# environment:
# PATH: /usr/local/bin:{{ ansible_env["PATH"] }}
# become: yes
# tags: pip

- name: Copy terminal settings file
copy: src=files/smoke.terminal dest=/tmp/smoke.terminal
tags: terminal
# - name: Copy terminal settings file
# copy: src=files/smoke.terminal dest=/tmp/smoke.terminal
# tags: terminal

- name: Set terminal to Smoke theme
command: open /tmp/smoke.terminal
ignore_errors: yes
tags: terminal
# - name: Set terminal to Smoke theme
# command: open /tmp/smoke.terminal
# ignore_errors: yes
# tags: terminal

- name: 'Terminal: Set startup window'
shell: 'defaults write {{ user_lib_prefs }}/com.apple.Terminal "Startup Window Settings" -string smoke'
tags: terminal
# - name: 'Terminal: Set startup window'
# shell: 'defaults write {{ user_lib_prefs }}/com.apple.Terminal "Startup Window Settings" -string smoke'
# tags: terminal

- name: 'Terminal: Set default window'
shell: 'defaults write {{ user_lib_prefs }}/com.apple.Terminal "Default Window Settings" -string smoke'
tags: terminal
# - name: 'Terminal: Set default window'
# shell: 'defaults write {{ user_lib_prefs }}/com.apple.Terminal "Default Window Settings" -string smoke'
# tags: terminal
48 changes: 39 additions & 9 deletions roles/new-user/tasks/create-user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,42 @@
- name: Remove the newuser script
file: path={{ newuser_script.stdout }} state=absent

- name: Prepend /usr/local/bin to $PATH in .profile
lineinfile:
dest=/Users/{{ username }}/.profile
state=present
regexp="^export\s+PATH=/usr/local/bin:\\$PATH"
insertafter=EOF
line="export
PATH=/usr/local/bin:$PATH"
create=yes
# - name: Prepend /usr/local/bin to $PATH in .bashrc
# lineinfile:
# dest: /Users/{{ username }}/.bashrc
# state: present
# regexp: '^export\s+PATH=/usr/local/bin:\\$PATH'
# insertafter: EOF
# line: 'export PATH=/usr/local/bin:$PATH\n'
# create: yes

# - set_fact:
# terminal_emoji: "{{ lookup('template', 'roles/new-user/files/terminal_emojis.txt').split() | random }}"

# - set_fact:
# bashrc_prompt: "{{ lookup('template', 'templates/bashrc.j2') }}"

# - name: Prepend /usr/local/bin to $PATH in .bashrc
# lineinfile:
# dest: /Users/{{ username }}/.bashrc
# state: present
# regexp: '^export\s+PATH=/usr/local/bin:\$PATH'
# insertafter: EOF
# line: 'export PATH=/usr/local/bin:$PATH'
# create: yes

# - name: Append custom prompt to .bashrc
# blockinfile:
# dest: /Users/{{ username }}/.bashrc
# block: '{{ lookup("template", "roles/new-user/templates/bashrc.j2") }}'
# create: yes
# insertafter: EOF
# state: present

# - name: Normalize spacing around ANSIBLE MANAGED BLOCK blocks
# replace:
# path: /Users/{{ username }}/.bashrc
# regexp: '\s*(# (?:BEGIN|END) ANSIBLE MANAGED BLOCK)\s*'
# replace: '\n\n\1\n'


File renamed without changes.
28 changes: 28 additions & 0 deletions roles/terminal/files/terminal_emojis.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
☘️
⚽️
⚾️
🌈
🌎
🍇
🍉
🍊
🍎
🍒
🍓
🎃
🎾
🏀
🏈
🐘
🐙
🐠
🐳
🐶
🐸
👹
👻
💎
🔮
😈
🤖
🦋
50 changes: 50 additions & 0 deletions roles/terminal/tasks/bash.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---

- user:
name: '{{ username }}'
shell: /bin/bash
become: yes

- set_fact:
terminal_emoji: "{{ lookup('template', 'files/terminal_emojis.txt').split() | random }}"

# - set_fact:
# bashrc_prompt: "{{ lookup('template', 'templates/bashrc_prompt.j2') }}"

- name: Load .bashrc from .profile
lineinfile:
dest: /Users/{{ username }}/.profile
state: present
owner: '{{ username }}'
group: staff
regexp: '\[ -r $HOME/\.bashrc \] && source \$HOME/\.bashrc'
insertbefore: BOF
line: '[ -r $HOME/.bashrc ] && source $HOME/.bashrc'
create: yes

- name: Prepend /usr/local/bin to $PATH in .bashrc
lineinfile:
dest: /Users/{{ username }}/.bashrc
state: present
regexp: '^export\s+PATH=/usr/local/bin:\$PATH'
insertafter: EOF
line: 'export PATH=/usr/local/bin:$PATH'
create: yes

- name: Append custom prompt to .bashrc
blockinfile:
dest: /Users/{{ username }}/.bashrc
block: '{{ lookup("template", "templates/bashrc_prompt.j2") }}'
create: yes
owner: '{{ username }}'
group: staff
insertafter: EOF
state: present

- name: Normalize spacing around ANSIBLE MANAGED BLOCK blocks
replace:
path: /Users/{{ username }}/.bashrc
regexp: '\s*(# (?:BEGIN|END) ANSIBLE MANAGED BLOCK)\s*'
replace: '\n\n\1\n'


4 changes: 4 additions & 0 deletions roles/terminal/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---

- import_tasks: theme.yml
- import_tasks: bash.yml
38 changes: 38 additions & 0 deletions roles/terminal/tasks/theme.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---

# - name: Check that Terminal preferences file exists
# stat:
# path: '{{ user_lib_prefs }}/com.apple.Terminal.plist'
# register: terminal_prefs

- name: Copy terminal settings file
copy:
src: files/smoke.terminal
dest: /tmp/smoke.terminal
# become: yes
# become_user: '{{ username }}'

- name: Use Smoke theme
command: open /tmp/smoke.terminal
ignore_errors: yes
become: yes
become_user: '{{ username }}'

# - name: Check that Terminal preferences file exists
# copy:
# dest: '{{ user_lib_prefs }}/com.apple.Terminal.plist'
# content: '{}'
# owner: '{{ username }}'
# group: staff
# mode: 0600
# when: not terminal_prefs.stat.exists

- name: 'Terminal: Set startup window'
shell: 'defaults write {{ user_lib_prefs }}/com.apple.Terminal "Startup Window Settings" -string smoke'
# become: yes
# become_user: '{{ username }}'

- name: 'Terminal: Set default window'
shell: 'defaults write {{ user_lib_prefs }}/com.apple.Terminal "Default Window Settings" -string smoke'
# become: yes
# become_user: '{{ username }}'
112 changes: 112 additions & 0 deletions roles/terminal/templates/bashrc_prompt.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# https://gist.github.com/joemaller/3165ace4b1f8a50924990ad373236ce8
#
# Append this file to the end of your ~/.bashrc file
# Your terminal prompt will look something like this:
#
# username@hostname /output/of/pwd [git-branch]$


# https://gist.github.com/4503986
#
# A set of color functions to help with fancy $PS1 prompts
#
# This:
# PROMPT_COMMAND='PS1="\e[32;1m\]\u@\[\e[35;1m\]\H \[\e[0m\]\w]\$ "''
#
# Can be more clearly written as:
# PROMPT_COMMAND='PS1="$(DARK_GREEN \\u@)$(PURPLE \\H) \w]\$ "''
#
# note: Escape codes returned from these functions will not work in a directly
# assigned $PS1, use PROMPT_COMMAND to prevent extra escaping.
#

# Text Colors
function RED { echo "\[\e[0;91m\]$1\[\e[0m\]"; }
function DARK_RED { echo "\[\e[0;31m\]$1\[\e[0m\]"; }
function YELLOW { echo "\[\e[0;93m\]$1\[\e[0m\]"; }
function DARK_YELLOW { echo "\[\e[0;33m\]$1\[\e[0m\]"; }
function GREEN { echo "\[\e[0;92m\]$1\[\e[0m\]"; }
function DARK_GREEN { echo "\[\e[0;32m\]$1\[\e[0m\]"; }
function CYAN { echo "\[\e[0;96m\]$1\[\e[0m\]"; }
function DARK_CYAN { echo "\[\e[0;36m\]$1\[\e[0m\]"; }
function BLUE { echo "\[\e[0;94m\]$1\[\e[0m\]"; }
function DARK_BLUE { echo "\[\e[0;34m\]$1\[\e[0m\]"; }
function MAGENTA { echo "\[\e[0;95m\]$1\[\e[0m\]"; }
function PURPLE { echo "\[\e[0;35m\]$1\[\e[0m\]"; }
function BLACK { echo "\[\e[0;30m\]$1\[\e[0m\]"; }
function GRAY { echo "\[\e[0;90m\]$1\[\e[0m\]"; }
function LIGHT_GRAY { echo "\[\e[0;37m\]$1\[\e[0m\]"; }
function WHITE { echo "\[\e[0;97m\]$1\[\e[0m\]"; }

# Black on Background Color
function B_ON_RED { echo "\[\e[0;30;101m\]$1\[\e[0m\]"; }
function B_ON_DARK_RED { echo "\[\e[0;30;41m\]$1\[\e[0m\]"; }
function B_ON_YELLOW { echo "\[\e[0;30;103m\]$1\[\e[0m\]"; }
function B_ON_DARK_YELLOW { echo "\[\e[0;30;43m\]$1\[\e[0m\]"; }
function B_ON_GREEN { echo "\[\e[0;30;102m\]$1\[\e[0m\]"; }
function B_ON_DARK_GREEN { echo "\[\e[0;30;42m\]$1\[\e[0m\]"; }
function B_ON_CYAN { echo "\[\e[0;30;106m\]$1\[\e[0m\]"; }
function B_ON_DARK_CYAN { echo "\[\e[0;30;46m\]$1\[\e[0m\]"; }
function B_ON_BLUE { echo "\[\e[0;30;104m\]$1\[\e[0m\]"; }
function B_ON_DARK_BLUE { echo "\[\e[0;30;44m\]$1\[\e[0m\]"; }
function B_ON_MAGENTA { echo "\[\e[0;30;105m\]$1\[\e[0m\]"; }
function B_ON_PURPLE { echo "\[\e[0;30;45m\]$1\[\e[0m\]"; }
function B_ON_BLACK { echo "\[\e[0;30;40m\]$1\[\e[0m\]"; }
function B_ON_DARK_GRAY { echo "\[\e[0;30;100m\]$1\[\e[0m\]"; }
function B_ON_GRAY { echo "\[\e[0;30;47m\]$1\[\e[0m\]"; }
function B_ON_LIGHT_GRAY { echo "\[\e[0;30;107m\]$1\[\e[0m\]"; }

# White on Background Color
function W_ON_RED { echo "\[\e[0;97;101m\]$1\[\e[0m\]"; }
function W_ON_DARK_RED { echo "\[\e[0;97;41m\]$1\[\e[0m\]"; }
function W_ON_YELLOW { echo "\[\e[0;97;103m\]$1\[\e[0m\]"; }
function W_ON_DARK_YELLOW { echo "\[\e[0;97;43m\]$1\[\e[0m\]"; }
function W_ON_GREEN { echo "\[\e[0;97;102m\]$1\[\e[0m\]"; }
function W_ON_DARK_GREEN { echo "\[\e[0;97;42m\]$1\[\e[0m\]"; }
function W_ON_CYAN { echo "\[\e[0;97;106m\]$1\[\e[0m\]"; }
function W_ON_DARK_CYAN { echo "\[\e[0;97;46m\]$1\[\e[0m\]"; }
function W_ON_BLUE { echo "\[\e[0;97;104m\]$1\[\e[0m\]"; }
function W_ON_DARK_BLUE { echo "\[\e[0;97;44m\]$1\[\e[0m\]"; }
function W_ON_MAGENTA { echo "\[\e[0;97;105m\]$1\[\e[0m\]"; }
function W_ON_PURPLE { echo "\[\e[0;97;45m\]$1\[\e[0m\]"; }
function W_ON_BLACK { echo "\[\e[0;97;40m\]$1\[\e[0m\]"; }
function W_ON_DARK_GRAY { echo "\[\e[0;97;100m\]$1\[\e[0m\]"; }
function W_ON_GRAY { echo "\[\e[0;97;47m\]$1\[\e[0m\]"; }
function W_ON_LIGHT_GRAY { echo "\[\e[0;97;107m\]$1\[\e[0m\]"; }

# https://gist.github.com/joemaller/4527475
#
# A simple bash function for coloring the current git branch.
# - Red: Tree is dirty
# - Yellow: All modified files are staged
# - Green: Tree is clean
#
# This uses the color functions from https://gist.github.com/4503986

function git_branch() {
is_git=$(git status --porcelain 2> /dev/null ) || return
branch=$(git symbolic-ref --short HEAD 2> /dev/null)
if [ -z "$branch" ]; then
branch=$(git status | sed -n 1p )
fi
is_dirty=$(git update-index --refresh 2> /dev/null)
is_staged=$(git diff-index --cached HEAD 2> /dev/null)
if [ -n "$is_dirty" ]; then
echo $(DARK_RED "[$branch]")
elif [ -n "$is_staged" ]; then
echo $(DARK_YELLOW "[$branch]")
else
echo $(DARK_GREEN "[$branch]")
fi
}

# Variable definitions:
# \u current user's name
# \h hostname to first dot
# \w current working directory

function set_ps1() {
PS1="\[{{ terminal_emoji }}\] $(GRAY \\u) $(GRAY @) $(GRAY \\h) $(DARK_CYAN \\w) $(git_branch)$(CYAN \\$) "
}

PROMPT_COMMAND="set_ps1; $PROMPT_COMMAND"

0 comments on commit fb7ff13

Please sign in to comment.