|
1 | | -https://github.com/user-attachments/assets/d22fadbd-bf6e-412e-80be-f2218536642c |
2 | | - |
3 | 1 | # git-switch |
4 | 2 |
|
5 | 3 | A fast, interactive terminal UI for switching between git branches. Built with [tcell](https://github.com/gdamore/tcell) for a smooth, cross-platform experience. |
|
46 | 44 | - Use **Up/Down** arrows to select. |
47 | 45 | - Press **Enter** to checkout the selected branch. |
48 | 46 | - Press **Esc** or **Ctrl+C** to exit. |
| 47 | +- Press **CTRL+D** to pin the currently selected branch. |
| 48 | +- Press **CTRL+U** to unpin the currently selected branch. |
49 | 49 |
|
50 | 50 | ### Git Checkout Override |
51 | 51 |
|
@@ -128,31 +128,67 @@ The interactive branch selector is exposed in the [`pkg`](./pkg) package. |
128 | 128 | package main |
129 | 129 |
|
130 | 130 | import ( |
| 131 | + "fmt" |
131 | 132 | sw "github.com/nathan-fiscaletti/git-switch/pkg" |
132 | 133 | ) |
133 | 134 |
|
134 | 135 | func main() { |
| 136 | + // Your branch data |
| 137 | + currentBranch := "main" |
| 138 | + branches := []string{"main", "develop", "feature/new-api", "bugfix/issue-123"} |
| 139 | + pinnedBranches := []string{"main", "develop"} |
| 140 | + |
135 | 141 | branchSelector, err := sw.NewBranchSelector(sw.BranchSelectorArguments{ |
136 | 142 | CurrentBranch: currentBranch, |
137 | 143 | Branches: branches, |
138 | | - PinnedBranches: pinnedBranches, |
| 144 | + PinnedBranches: &pinnedBranches, // Pass as pointer for shared state |
139 | 145 | PinnedBranchPrefix: "★", |
140 | 146 | WindowSize: 10, |
141 | 147 | SearchLabel: "search branch", |
| 148 | + |
| 149 | + // Optional: Handle pin/unpin operations |
| 150 | + OnPinBranch: func(branch string) error { |
| 151 | + // Implement your own storage logic |
| 152 | + fmt.Printf("Pinning branch: %s\n", branch) |
| 153 | + // Save to database, file, etc. |
| 154 | + return nil |
| 155 | + }, |
| 156 | + OnUnpinBranch: func(branch string) error { |
| 157 | + // Implement your own storage logic |
| 158 | + fmt.Printf("Unpinning branch: %s\n", branch) |
| 159 | + // Remove from database, file, etc. |
| 160 | + return nil |
| 161 | + }, |
142 | 162 | }) |
143 | 163 | if err != nil { |
144 | 164 | panic(err) |
145 | 165 | } |
146 | 166 |
|
147 | | - b, err := branchSelector.PickBranch() |
| 167 | + // Show the interactive selector |
| 168 | + selectedBranch, err := branchSelector.PickBranch() |
148 | 169 | if err != nil { |
149 | 170 | panic(err) |
150 | 171 | } |
151 | 172 |
|
152 | | - // ... use b ... |
| 173 | + if selectedBranch != "" { |
| 174 | + fmt.Printf("Selected branch: %s\n", selectedBranch) |
| 175 | + // Process the selected branch... |
| 176 | + } |
153 | 177 | } |
154 | 178 | ``` |
155 | 179 |
|
| 180 | +#### Interactive Features |
| 181 | + |
| 182 | +The branch selector includes interactive hotkeys: |
| 183 | + |
| 184 | +- **CTRL+D**: Pin the currently selected branch |
| 185 | +- **CTRL+U**: Unpin the currently selected branch |
| 186 | +- **Up/Down**: Navigate branches |
| 187 | +- **Enter**: Select branch |
| 188 | +- **Esc/Ctrl+C**: Exit |
| 189 | + |
| 190 | +Pin/unpin operations automatically call your `OnPinBranch` and `OnUnpinBranch` callbacks, allowing you to integrate with any storage backend (database, files, APIs, etc.). |
| 191 | + |
156 | 192 | ## Configuration |
157 | 193 |
|
158 | 194 | Config File Location: |
|
0 commit comments