Skip to content

Commit

Permalink
Added promptstring command + updated custom prompt installation (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrus2281 authored Oct 24, 2024
1 parent f37296c commit 7de4707
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 59 deletions.
3 changes: 3 additions & 0 deletions CHANGE_LOGS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Version Change Logs

### Version 3.0.6
> Add prompt string command
### Version 3.0.5
> Added a check to ensure branch exists before adding an alias to it
Expand Down
19 changes: 4 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
- [MacOS ZSH](#macos-zsh)
- [Windows PowerShell](#windows-powershell-1)
- [Custom Prompt](#custom-prompt)
- [Linux/Unix Bash](#linuxunix-bash)
- [Linux/Unix ZSH](#linuxunix-zsh)
- [Commands](#commands)
- [Examples](#examples)
- [Contributing](#contributing)
Expand Down Expand Up @@ -135,23 +133,14 @@ echo "g completion powershell | Out-String | Invoke-Expression" >> $PROFILE

## Custom Prompt

### Linux/Unix Bash

Download and load the [`gCustomPrompt.sh`](./gCustomPrompt.sh) file in your `.bashrc` or `.bash_profile`
Run the following command to your terminal profile to add the gitBranchTool custom prompt.
- Change `.bashrc` with `.zshrc` if you use ZSH, or the profile file you use if it's different.

```bash
curl -o ~/.gCustomPrompt.sh https://raw.githubusercontent.com/cyrus2281/gitBranchTool/refs/heads/main/gCustomPrompt.sh
echo "\nsource ~/.gCustomPrompt.sh\n" >> ~/.bashrc
echo -e "\nPROMPT_COMMAND='export PS1=\"\$(g _ps)\"'\nprecmd() { eval \"\$PROMPT_COMMAND\"; }" >> ~/.bashrc
```

### Linux/Unix ZSH

Download and load the [`gCustomPrompt.sh`](./gCustomPrompt.sh) file in your `.zshrc`

```bash
curl -o ~/.gCustomPrompt.sh https://raw.githubusercontent.com/cyrus2281/gitBranchTool/refs/heads/main/gCustomPrompt.sh
echo "\nsource ~/.gCustomPrompt.sh\n" >> ~/.zshrc
```
- Custom prompt is not supported in Windows PowerShell yet.


## Commands
Expand Down
77 changes: 77 additions & 0 deletions cmd/promptString.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright © 2024 Cyrus Mobini
*/
package cmd

import (
"fmt"
"os"
"os/user"
"strings"

"github.com/cyrus2281/gitBranchTool/internal"
"github.com/spf13/cobra"
)

// promptStringCmd represents the promptString command
var promptStringCmd = &cobra.Command{
Use: "_ps",
Short: "Returns the prompt string - used for the custom prompt",
Long: `Returns the prompt string - used for the custom prompt`,
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
username := os.Getenv("LOGNAME")
currentUser, err := user.Current()
if err == nil {
username = currentUser.Username
}
prompt := fmt.Sprintf("%s ➤ %s ❖ ", username, getPrompt())
fmt.Print(prompt)
},
}

// Build the custom prompt string
func getPrompt() string {
workingDirectory, err := os.Getwd()
if err != nil {
return "$"
}

git := internal.Git{}
// Repository name
repo, err := git.GetRepositoryName()
if err != nil || repo == "" {
return workingDirectory
}

// Branch Name
currentBranch, err := git.GetCurrentBranch()
if err != nil || currentBranch == "" {
return workingDirectory
}

// Alias Name
repoBranches := internal.GetRepositoryBranches()
branch, ok := repoBranches.GetBranchByName(currentBranch)
if ok && branch.Alias != "" {
currentBranch = fmt.Sprintf("%s (%s)", currentBranch, branch.Alias)
}

// Subpath
subpath := ""
index := strings.Index(workingDirectory, repo)
if index >= 0 {
subpath = workingDirectory[index+len(repo):]
subpath = strings.TrimPrefix(subpath, "/")
if subpath != "" {
subpath = fmt.Sprintf(" [%s]", subpath)
}
}

// Custom Prompt String
return fmt.Sprintf("%s%s ⌥ %s", repo, subpath, currentBranch)
}

func init() {
rootCmd.AddCommand(promptStringCmd)
}
43 changes: 0 additions & 43 deletions gCustomPrompt.sh

This file was deleted.

2 changes: 1 addition & 1 deletion internal/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package internal

const VERSION = "3.0.5"
const VERSION = "3.0.6"

0 comments on commit 7de4707

Please sign in to comment.