From fb7ff13ea8f7e75e8dcac84152af7ebd197215fe Mon Sep 17 00:00:00 2001 From: Joe Maller Date: Sun, 17 Jun 2018 11:23:40 -0400 Subject: [PATCH] preliminary terminal role. Closes #64. Ref #71 --- admin.yml | 35 +++--- roles/new-user/tasks/create-user.yml | 48 ++++++-- .../terminal/files}/smoke.terminal | 0 roles/terminal/files/terminal_emojis.txt | 28 +++++ roles/terminal/tasks/bash.yml | 50 ++++++++ roles/terminal/tasks/main.yml | 4 + roles/terminal/tasks/theme.yml | 38 ++++++ roles/terminal/templates/bashrc_prompt.j2 | 112 ++++++++++++++++++ 8 files changed, 290 insertions(+), 25 deletions(-) rename {files => roles/terminal/files}/smoke.terminal (100%) create mode 100644 roles/terminal/files/terminal_emojis.txt create mode 100644 roles/terminal/tasks/bash.yml create mode 100644 roles/terminal/tasks/main.yml create mode 100644 roles/terminal/tasks/theme.yml create mode 100644 roles/terminal/templates/bashrc_prompt.j2 diff --git a/admin.yml b/admin.yml index 8630bdb..0bc5219 100644 --- a/admin.yml +++ b/admin.yml @@ -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 } @@ -172,7 +173,9 @@ recurse=yes become: yes - post_tasks: + + + # post_tasks: # - name: Install virtualenvwrapper # pip: name=virtualenvwrapper # environment: @@ -180,19 +183,19 @@ # 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 diff --git a/roles/new-user/tasks/create-user.yml b/roles/new-user/tasks/create-user.yml index 11a73cf..77216b1 100644 --- a/roles/new-user/tasks/create-user.yml +++ b/roles/new-user/tasks/create-user.yml @@ -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' + + diff --git a/files/smoke.terminal b/roles/terminal/files/smoke.terminal similarity index 100% rename from files/smoke.terminal rename to roles/terminal/files/smoke.terminal diff --git a/roles/terminal/files/terminal_emojis.txt b/roles/terminal/files/terminal_emojis.txt new file mode 100644 index 0000000..f408fef --- /dev/null +++ b/roles/terminal/files/terminal_emojis.txt @@ -0,0 +1,28 @@ +☘️ +⚽️ +⚾️ +🌈 +🌎 +🍇 +🍉 +🍊 +🍎 +🍒 +🍓 +🎃 +🎾 +🏀 +🏈 +🐘 +🐙 +🐠 +🐳 +🐶 +🐸 +👹 +👻 +💎 +🔮 +😈 +🤖 +🦋 diff --git a/roles/terminal/tasks/bash.yml b/roles/terminal/tasks/bash.yml new file mode 100644 index 0000000..8f084c1 --- /dev/null +++ b/roles/terminal/tasks/bash.yml @@ -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' + + diff --git a/roles/terminal/tasks/main.yml b/roles/terminal/tasks/main.yml new file mode 100644 index 0000000..177bba3 --- /dev/null +++ b/roles/terminal/tasks/main.yml @@ -0,0 +1,4 @@ +--- + +- import_tasks: theme.yml +- import_tasks: bash.yml diff --git a/roles/terminal/tasks/theme.yml b/roles/terminal/tasks/theme.yml new file mode 100644 index 0000000..f71ec65 --- /dev/null +++ b/roles/terminal/tasks/theme.yml @@ -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 }}' diff --git a/roles/terminal/templates/bashrc_prompt.j2 b/roles/terminal/templates/bashrc_prompt.j2 new file mode 100644 index 0000000..f0a1f18 --- /dev/null +++ b/roles/terminal/templates/bashrc_prompt.j2 @@ -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"