prompt: show in tmux instead
Anirudh Oppiliappan x@icyphox.sh
Thu, 27 Jan 2022 17:33:22 +0530
4 files changed,
51 insertions(+),
108 deletions(-)
M
bash/.bashrc.d/99-prompt.bash
→
bash/.bashrc.d/99-prompt.bash
@@ -1,52 +1,6 @@
-# red="\e[31m" -# grn="\e[32m" -# ylw="\e[33m" -# cyn="\e[36m" -# blu="\e[34m" -# prp="\e[35m" -# bprp="\e[35;1m" -# gry="\e[94m" -# rst="\e[0m" -# -# git_branch() { -# [[ -d "$PWD/.git" ]] && { -# local git_status="$(git status 2> /dev/null)" -# local on_branch="On branch ([^${IFS}]*)" -# local on_commit="HEAD detached at ([^${IFS}]*)" -# status="$(git status --porcelain 2> /dev/null)" -# local exit="$?" -# color="" -# -# if [[ "$exit" -eq 0 ]]; then -# if [[ "${#status}" -eq 0 ]]; then -# color="${grn}" -# else -# color="${red}" -# fi -# else -# printf '' -# fi -# -# -# if [[ $git_status =~ $on_branch ]]; then -# local branch=${BASH_REMATCH[1]} -# printf '%b' "($color$branch$rst)" -# elif [[ $git_status =~ $on_commit ]]; then -# local commit=${BASH_REMATCH[1]} -# printf '%b' "($color$commit$rst)" -# fi -# -# } -# } -# -# prompt_pwd() { -# printf '%b' "\001${cyn}\002$(dirs +0)\001${rst}\002" -# } -# -# rootornot() { -# [[ "$(id -u)" -eq 0 ]] && -# printf '%b' "\001${red}\002#\001${rst}\002" -# } - -PS1='$(prompt) ' +refresh_tmux() { + tmux refresh-client -S +} +PROMPT_COMMAND=refresh_tmux +PS1='▲ ' PS2="> "
M
home/.tmux.conf
→
home/.tmux.conf
@@ -53,10 +53,6 @@ bind -n C-down set -q status off
bind -n C-up set -q status on bind P paste-buffer -bind E command-prompt -p "Command:" \ - "run \"tmux list-panes -a -F '##{session_name}:##{window_index}.##{pane_index}' \ - | xargs -I PANE tmux send-keys -t PANE '%1' Enter\"" - set-window-option -g allow-rename off set -g pane-border-style fg=colour11@@ -64,6 +60,7 @@ set -g pane-active-border-style fg=colour8
set -g status-justify right set -g status-right "" +set -g status-left "" set -g status-style "bg=colour0" set -ag status-style "fg=colour7"@@ -75,7 +72,9 @@ set -g status-left-length 100
set -g status-left "#[fg=colour8]time #[fg=colour15]#(date +"%H:%M") " set -ag status-left "#[fg=colour8]date #[fg=colour15]#(date.sh) " set -ag status-left "#[fg=colour8]bat #[fg=colour15]#(~/bin/bat -q) " -set -ag status-left "#[fg=colour8]#(~/bin/np.sh -a) #[fg=colour15]#(~/bin/np.sh -t)" +set -ag status-left "#[fg=colour8]cwd #[fg=colour15]#(prompt -p #{pane_current_path}) " +set -ag status-left "#[fg=colour15]#(prompt -gb #{pane_current_path}) " +set -ag status-left "#[fg=colour15]#(prompt -gs #{pane_current_path})" # dim inactive pane set -g window-style 'fg=color8,bg=default'
M
prompt/git.go
→
prompt/git.go
@@ -9,8 +9,7 @@ )
// Recursively traverse up until we find .git // and return the git repo path. -func getGitDir() string { - cwd, _ := os.Getwd() +func getGitDir(cwd string) string { for { dirs, _ := os.ReadDir(cwd) for _, d := range dirs {@@ -25,31 +24,31 @@ }
} // Returns the current git branch or current ref sha. -func getGitBranch(repo *git.Repository) { +func getGitBranch(repo *git.Repository, ch chan<- string) { ref, _ := repo.Head() // Quick hack to fix crash when ref is nil; // i.e., new repo with no commits. if ref == nil { - branchCh <- "no commit" + ch <- "no commit" } if ref.IsBranch() { name, _ := ref.Branch().Name() - branchCh <- name + ch <- name } else { - branchCh <- ref.Target().String()[:8] + ch <- ref.Target().String()[:8] } } // Returns • if clean, else ×. -func getGitStatus(repo *git.Repository) { +func getGitStatus(repo *git.Repository, ch chan<- string) { sl, _ := repo.StatusList(&git.StatusOptions{ Show: git.StatusShowIndexAndWorkdir, Flags: git.StatusOptIncludeUntracked, }) n, _ := sl.EntryCount() if n != 0 { - statusCh <- "×" + ch <- "×" } else { - statusCh <- "•" + ch <- "•" } }
M
prompt/prompt.go
→
prompt/prompt.go
@@ -9,10 +9,6 @@
git "github.com/libgit2/git2go/v33" ) -const ( - promptSym = "▲" -) - // Truncates the current working directory: // /home/icy/foo/bar -> ~/f/bar func trimPath(cwd, home string) string {@@ -38,46 +34,41 @@ }
return filepath.Join(truncItems...) } -var branchCh = make(chan string) -var statusCh = make(chan string) +func main() { -func makePrompt() string { - cwd, _ := os.Getwd() - home := os.Getenv("HOME") - gitDir := getGitDir() - - if len(gitDir) > 0 { - repo, _ := git.OpenRepository(getGitDir()) - - go getGitBranch(repo) - go getGitStatus(repo) - - select { - case status := <-statusCh: - return fmt.Sprintf( - "\n%s (%s %s)\n%s", - trimPath(cwd, home), - <-branchCh, - status, - promptSym, - ) - default: - return fmt.Sprintf( - "\n%s (%s %s)\n%s", - trimPath(cwd, home), - <-branchCh, - <-statusCh, - promptSym, - ) + help := `usage: prompt OPTIONS PATH + -p path to truncate + -gb git branch + -gs git status` + if len(os.Args) != 3 { + fmt.Println(help) + os.Exit(1) + } + gitDir := getGitDir(os.Args[2]) + switch os.Args[1] { + case "-p": + // current working directory + home := os.Getenv("HOME") + cwd := os.Args[2] + fmt.Print(trimPath(cwd, home)) + case "-gb": + branchCh := make(chan string) + if len(gitDir) > 0 { + repo, _ := git.OpenRepository(gitDir) + go getGitBranch(repo, branchCh) + fmt.Print(<-branchCh) + } + fmt.Print("") + case "-gs": + statusCh := make(chan string) // git info + if len(gitDir) > 0 { + repo, _ := git.OpenRepository(gitDir) + go getGitStatus(repo, statusCh) + fmt.Print(<-statusCh) } + fmt.Print("") + default: + fmt.Println(help) + os.Exit(1) } - return fmt.Sprintf( - "\n%s\n%s", - trimPath(cwd, home), - promptSym, - ) -} - -func main() { - fmt.Println(makePrompt()) }