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

Allow customization of terminal title #302

Merged
merged 1 commit into from
Sep 25, 2020
Merged
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
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased
- Add GEOMETRY_TITLE and GEOMETRY_CMDTITLE as display locations
- Add geometry_cmd function to display currently-running command in GEOMETRY_CMDTITLE

### Fixed
- Fix git functions erroring out in non-git directories (thanks @duncanbeevers!)
Expand Down
28 changes: 22 additions & 6 deletions geometry.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ typeset -gA GEOMETRY; GEOMETRY[ROOT]=${0:A:h}
(($+GEOMETRY_PROMPT)) || GEOMETRY_PROMPT=(geometry_echo geometry_status geometry_path)
(($+GEOMETRY_RPROMPT)) || GEOMETRY_RPROMPT=(geometry_exec_time geometry_git geometry_hg geometry_echo)
(($+GEOMETRY_INFO)) || GEOMETRY_INFO=()
(($+GEOMETRY_TITLE)) || GEOMETRY_TITLE=(geometry_path)
(($+GEOMETRY_CMDTITLE)) || GEOMETRY_CMDTITLE=(geometry_cmd geometry_hostname)

autoload -U add-zsh-hook

function { local fun; for fun ("${GEOMETRY[ROOT]}"/functions/*) . $fun }

(( $+functions[ansi] )) || ansi() { (($# - 2)) || echo -n "%F{$1}$2%f"; }
(( $+functions[deansi] )) || deansi() { (($# - 1)) || echo -n "$(echo "$1" | sed s/$(echo "\033")\\\[\[0-9\]\\\{1,2\\\}m//g)"; }
(( $+functions[geometry_cmd])) || geometry_cmd() { echo $GEOMETRY_LAST_CMD }

# Takes number of seconds and formats it for humans
# from https://github.com/sindresorhus/pretty-time-zsh
Expand Down Expand Up @@ -59,13 +63,25 @@ geometry::hostcolor() {
echo ${colors[${index}]}
}

# set title to COMMAND @ CURRENT_DIRECTORY
geometry::set_title() { print -n "\e]0;${2} @ ${PWD##*/}\a"; }
add-zsh-hook preexec geometry::set_title
# set cmd title (while command is running)
geometry::set_cmdtitle() {
# Make command title available for optional consumption by geometry_cmd
GEOMETRY_LAST_CMD=$2
local ansiCmdTitle=$(print -P $(geometry::wrap $PWD $GEOMETRY_CMDTITLE))
local cmdTitle=$(deansi "$ansiCmdTitle")

# clear title after command ends
geometry::clear_title() { print -n '\e]0;%~\a'; }
add-zsh-hook precmd geometry::clear_title
echo -ne "\e]1;$cmdTitle\a"
}
add-zsh-hook preexec geometry::set_cmdtitle

# set ordinary title (after command has completed)
geometry::set_title() {
local ansiTitle=$(print -P $(geometry::wrap $PWD $GEOMETRY_TITLE))
local title=$(deansi "$ansiTitle")

echo -ne "\e]1;$title\a"
}
add-zsh-hook precmd geometry::set_title

# join outputs of functions - pwd first
geometry::wrap() {
Expand Down
16 changes: 13 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,27 @@ tool | add to `.zshrc`

![showing prompt customization with new function](./images/screenshots/functions.png)

Geometry has very little architecture. Three environment variables define what is shown on the left, right, and on enter - `GEOMETRY_PROMPT`, `GEOMETRY_RPROMPT`, and `GEOMETRY_INFO`.
Geometry displays output in several places. The output displayed in each location is determined by the plugins configured for that location.
These are the supported locations, along with the environment variable used to configure each one.

Most of these functions only show up if it makes sense to (for example, `geometry_git` only shows up if in a git repository).
Variable Name | Text display location
---------------------|-------------------------------------------------------
GEOMETRY_PROMPT | Text shown to the left of the cursor
GEOMETRY_RPROMPT | Text shown at the right edge of the terminal
GEOMETRY_INFO | Text shown after pressing enter with no input
GEOMETRY_TITLE | Text shown in the terminal title
GEOMETRY_CMDTITLE | Text shown in the terminal title when a command is run

To customize the prompt, just add any function to any of the `GEOMETRY_PROMPT`, `GEOMETRY_RPROMPT`, or `GEOMETRY_INFO` variables:
To customize the prompt, add any function to the list of functions for the desired display location:

```sh
GEOMETRY_PROMPT=(geometry_status geometry_path) # redefine left prompt
GEOMETRY_RPROMPT+=(geometry_exec_time pwd) # append exec_time and pwd right prompt
GEOMETRY_TITLE=(geometry_node)
```

Most of these functions only show up if it makes sense to (for example, `geometry_git` only shows up if in a git repository).

Please check out and share third-party functions on our [functions wiki page][]

For more details on how to create a function, check out [our contribution guide][]
Expand Down