diff --git a/README.md b/README.md index 5e4481c..4d4833d 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,8 @@ of pending commits, if any; * the error code of the last command, if it has failed in some way; * a smart mark: ± for git directories, ☿ for mercurial, ‡ for svn, $ for simple user, a red # for root. +* if you ask for, the liquidprompt will be replicated in your terminal window's +title (without the colors) You can temporarily deactivate the liquid prompt and come back to your previous one by typing `prompt_off`. Use `prompt_on` to bring it back. You can deactivate @@ -123,6 +125,7 @@ building: * `LP_ENABLE_SVN`, if you want to have subversion informations * `LP_ENABLE_HG`, if you want to have mercurial informations * `LP_ENABLE_VCS_ROOT`, if you want to show VCS informations with root account +* `LP_ENABLE_TITLE`, if you want to use the prompt as your terminal window's title Note that if required commands are not installed, enabling the corresponding feature will have no effect. @@ -135,8 +138,7 @@ in your config file, you will not have the battery informations. ### ADD A PS1 PREFIX You can prefix the `LP_PS1` variable with anything you want using the -`LP_PS1_PREFIX`. The following example activate title change on xterm-like -windows: +`LP_PS1_PREFIX`. The following example activate a custom window's title: LP_PS1_PREFIX="\[\e]0;\u@\h: \w\a\]" @@ -162,6 +164,7 @@ Available features: * `LP_SVN` subversion * `LP_ERR` last error code * `LP_MARK` prompt mark +* `LP_TITLE` the prompt as a window's title escaped sequence For example, if you just want to have a liquidprompt displaying the user and the host, with a normal full path in blue and only the git support: @@ -240,6 +243,8 @@ Special characters: * `LP_MARK_SVN` (default: "‡") prompt mark in svn repositories * `LP_MARK_GIT` (default: "±") prompt mark in git repositories * `LP_MARK_UNTRACKED` (default: "*") if git has untracked files +* `LP_TITLE_OPEN` (default: "\e]0;") escape character opening a window's title +* `LP_TITLE_CLOSE` (default: "\a") escape character closing a window's title ## KNOWN LIMITATIONS AND BUGS @@ -254,4 +259,6 @@ the display of the liquid prompt. * Subversion repository cannot display commits to be pushed, this is a limitation of the Subversion versionning model. * The proxy detection only uses the `$http_proxy` environment variable. +* The window's title escape sequence may not work properly on some terminals +(like xterm-256) diff --git a/liquid.ps1 b/liquid.ps1 index 5b7c478..b670c36 100644 --- a/liquid.ps1 +++ b/liquid.ps1 @@ -18,7 +18,7 @@ # LP_ERR last error code # LP_MARK prompt mark # LP_TIME current time -# LP_PS1_PREFIX user-defined general-purpose prefix +# LP_PS1_PREFIX user-defined general-purpose prefix (default set a generic prompt as the window title) # Remember that most features come with their corresponding colors, # see the README. @@ -45,4 +45,11 @@ fi # add return code and prompt mark LP_PS1="${LP_PS1}${LP_ERR}${LP_MARK}" +# "invisible" parts +# Get the current prompt on the fly and make it a title +LP_TITLE=$(_lp_title $PS1) + +# Insert it in the prompt +PS1="${LP_TITLE}${PS1}" + # vim: set et sts=4 sw=4 tw=120 ft=sh: diff --git a/liquid.theme b/liquid.theme index 205ecef..0c3c1a7 100644 --- a/liquid.theme +++ b/liquid.theme @@ -30,8 +30,8 @@ else LP_MARK_UNTRACKED="*" fi -# Use the prefix to change the title of the terminal window -LP_PS1_PREFIX="\[\e]0;\u@\h: \w\a\]" +# Do not prefix the prompt +LP_PS1_PREFIX="" # Colors # Available colors are: diff --git a/liquidprompt b/liquidprompt index 5bfad71..d15d50c 100755 --- a/liquidprompt +++ b/liquidprompt @@ -189,7 +189,9 @@ _lp_source_config() LP_PATH_KEEP=${LP_PATH_KEEP:-2} LP_HOSTNAME_ALWAYS=${LP_HOSTNAME_ALWAYS:-0} LP_PS1=${LP_PS1:-""} - LP_PS1_PREFIX=${LP_PS1_PREFIX:-"\[\e]0;\u@\h: \w\a\]"} + LP_PS1_PREFIX=${LP_PS1_PREFIX:-""} + LP_TITLE_OPEN=${LP_TITLE_OPEN:-"\e]0;"} + LP_TITLE_CLOSE=${LP_TITLE_CLOSE:-"\a"} LP_ENABLE_PERM=${LP_ENABLE_PERM:-1} LP_ENABLE_SHORTEN_PATH=${LP_ENABLE_SHORTEN_PATH:-1} @@ -203,6 +205,7 @@ _lp_source_config() LP_ENABLE_TIME=${LP_ENABLE_TIME:-0} LP_ENABLE_VIRTUALENV=${LP_ENABLE_VIRTUALENV:-1} LP_ENABLE_VCS_ROOT=${LP_ENABLE_VCS_ROOT:-0} + LP_ENABLE_TITLE=${LP_ENABLE_TITLE:-0} LP_MARK_BATTERY=${LP_MARK_BATTERY:-"⌁"} LP_MARK_ADAPTER=${LP_MARK_ADAPTER:-"⏚"} @@ -914,6 +917,36 @@ _lp_load_color() # DESIGN # ########## +# Remove all colors and escape characters of the given string and return a pure text +_lp_as_text() +{ + # Remove colors from the computed prompt + local pst + pst=$(echo $1 | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g") + + # Remove escape sequences + pst=$(echo $pst | sed "s,\\\\\\[\|\\\\\\],,g") + + echo -n "$pst" +} + +_lp_title() +{ + [[ "$LP_ENABLE_TITLE" != 1 ]] && return + + # Get the current computed prompt as pure text + local txt + txt=$(_lp_as_text "$1") + + # Use it in the window's title + # Escapes whill tells bash to ignore the non-printing control characters when calculating the width of the prompt. + # Otherwise line editing commands will mess the cursor positionning + local title + title="${_LP_OPEN_ESC}${LP_TITLE_OPEN}${txt}${LP_TITLE_CLOSE}${_LP_CLOSE_ESC}" + + echo -n "${title}" +} + # Set the prompt mark to ± if git, to ☿ if mercurial, to ‡ if subversion # to # if root and else $ _lp_smart_mark() @@ -977,6 +1010,7 @@ _lp_time() # Construct the prompt # ######################## + _lp_set_prompt() { # as this get the last returned code, it should be called first @@ -1021,7 +1055,7 @@ _lp_set_prompt() fi if [[ -z $LP_PS1 ]] ; then - # add time, jobs, load and battery + # add title escape time, jobs, load and battery PS1="${LP_PS1_PREFIX}${LP_TIME}${LP_BATT}${LP_LOAD}${LP_JOBS}" # add user, host and permissions colon PS1="${PS1}[${LP_USER}${LP_HOST}${LP_PERM}" @@ -1043,6 +1077,14 @@ _lp_set_prompt() # add return code and prompt mark PS1="${PS1}${LP_ERR}${LP_MARK}" + # "invisible" parts + + # Get the current prompt on the fly and make it a title + LP_TITLE=$(_lp_title $PS1) + + # Insert it in the prompt + PS1="${LP_TITLE}${PS1}" + # Glue the bash prompt always go to the first column. # Avoid glitches after interrupting a command with Ctrl-C # Does not seem to be necessary anymore? @@ -1050,6 +1092,8 @@ _lp_set_prompt() else PS1=$LP_PS1 fi + + } # Activate the liquid prompt