Skip to content

Commit a8033e0

Browse files
author
Jonathan Dahan
authored
Merge pull request #302 from duncanbeevers/termtitle
Allow customization of terminal title
2 parents ca99295 + 16a565d commit a8033e0

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
44
and this project adheres to [Semantic Versioning](http://semver.org/).
55

66
## Unreleased
7+
- Add GEOMETRY_TITLE and GEOMETRY_CMDTITLE as display locations
8+
- Add geometry_cmd function to display currently-running command in GEOMETRY_CMDTITLE
79

810
### Fixed
911
- Fix git functions erroring out in non-git directories (thanks @duncanbeevers!)

geometry.zsh

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ typeset -gA GEOMETRY; GEOMETRY[ROOT]=${0:A:h}
99
(($+GEOMETRY_PROMPT)) || GEOMETRY_PROMPT=(geometry_echo geometry_status geometry_path)
1010
(($+GEOMETRY_RPROMPT)) || GEOMETRY_RPROMPT=(geometry_exec_time geometry_git geometry_hg geometry_echo)
1111
(($+GEOMETRY_INFO)) || GEOMETRY_INFO=()
12+
(($+GEOMETRY_TITLE)) || GEOMETRY_TITLE=(geometry_path)
13+
(($+GEOMETRY_CMDTITLE)) || GEOMETRY_CMDTITLE=(geometry_cmd geometry_hostname)
1214

1315
autoload -U add-zsh-hook
1416

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

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

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

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

66-
# clear title after command ends
67-
geometry::clear_title() { print -n '\e]0;%~\a'; }
68-
add-zsh-hook precmd geometry::clear_title
73+
echo -ne "\e]1;$cmdTitle\a"
74+
}
75+
add-zsh-hook preexec geometry::set_cmdtitle
76+
77+
# set ordinary title (after command has completed)
78+
geometry::set_title() {
79+
local ansiTitle=$(print -P $(geometry::wrap $PWD $GEOMETRY_TITLE))
80+
local title=$(deansi "$ansiTitle")
81+
82+
echo -ne "\e]1;$title\a"
83+
}
84+
add-zsh-hook precmd geometry::set_title
6985

7086
# join outputs of functions - pwd first
7187
geometry::wrap() {

readme.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,27 @@ tool | add to `.zshrc`
4343

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

46-
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`.
46+
Geometry displays output in several places. The output displayed in each location is determined by the plugins configured for that location.
47+
These are the supported locations, along with the environment variable used to configure each one.
4748

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

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

5259
```sh
5360
GEOMETRY_PROMPT=(geometry_status geometry_path) # redefine left prompt
5461
GEOMETRY_RPROMPT+=(geometry_exec_time pwd) # append exec_time and pwd right prompt
62+
GEOMETRY_TITLE=(geometry_node)
5563
```
5664

65+
Most of these functions only show up if it makes sense to (for example, `geometry_git` only shows up if in a git repository).
66+
5767
Please check out and share third-party functions on our [functions wiki page][]
5868

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

0 commit comments

Comments
 (0)