Compare commits

...

21 Commits

Author SHA1 Message Date
Jesse Duffield
f91adf026b fix lbl scrolling 2021-06-05 13:54:05 +10:00
Jesse Duffield
6d91661d5e prevent closure issue 2021-06-05 13:54:05 +10:00
Jesse Duffield
90983aae65 not importing regexp 2021-06-05 13:53:25 +10:00
Jesse Duffield
f71b23b890 more explicit 2021-06-05 13:53:25 +10:00
Cristian Betivu
05a23f0e1e Discard value after END marker 2021-06-05 13:53:25 +10:00
Cristian Betivu
fd38ad8096 More generic merge conflict detection 2021-06-05 13:53:25 +10:00
Jesse Duffield
d502c43ae8 fix tests 2021-06-05 10:58:36 +10:00
Jesse Duffield
0df02dacc2 minor changes 2021-06-05 10:58:09 +10:00
caojoshua
3258c24fb3 Better english for Configuring File Editing. 2021-06-05 10:58:09 +10:00
caojoshua
e7c657fba0 Docs for EditCommand. 2021-06-05 10:58:09 +10:00
caojoshua
60468d2e17 Edit command as user OS config option 2021-06-05 10:58:09 +10:00
Robert Verst
cb78cf7de4 Simplify sorting of git tags by using git's functions 2021-06-05 10:56:46 +10:00
Robert Verst
94b52af661 Remove config, make default sort order descending 2021-06-05 10:56:46 +10:00
Robert Verst
472288c81b Add user config to change the sort order of tags 2021-06-05 10:56:46 +10:00
Jesse Duffield
258eedb38c refactor 2021-06-02 20:33:52 +10:00
Jérémy Pagé
bc044c64b2 Remove origin prefix when creating local branch based from origin 2021-05-30 15:29:56 +10:00
Harrison Jones
e478c254d4 Handle alternate merge conflict format; add tests 2021-05-30 13:50:42 +10:00
Liberatys
44f7fc6f7c Add global binding to open recent repos 2021-05-30 13:25:44 +10:00
Dawid Dziurla
f92fcfbb47 cd: remove ppa job
Deprecated
2021-05-07 00:08:22 +02:00
Dawid Dziurla
6ccf58c224 README: deprecate Ubuntu PPA 2021-05-07 00:06:38 +02:00
Petróczi Zoltán
9190e9beac Fix englishIntroPopupMessage typo in english.go 2021-04-20 21:08:29 +10:00
33 changed files with 1142 additions and 775 deletions

View File

@@ -29,26 +29,3 @@ jobs:
with:
token: ${{secrets.GITHUB_API_TOKEN}}
formula: lazygit
ppa:
runs-on: ubuntu-20.04
steps:
- name: Checkout PPA repo
uses: actions/checkout@v2
with:
repository: dawidd6/lazygit-debian
token: ${{secrets.GITHUB_API_TOKEN}}
fetch-depth: 0
- name: Setup git
uses: dawidd6/action-git-user-config@v1
- name: Update PPA repo
run: |
version="$(echo "$GITHUB_REF" | sed 's@refs/tags/v@@')"
sudo apt update
sudo apt install -y git-buildpackage
git fetch --tags https://github.com/$GITHUB_REPOSITORY
gbp import-ref -u "$version"
gbp dch -D xenial -N "$version"-1
git add debian/changelog
git commit -m "d/changelog: dch $version"
gbp tag
git push --tags origin master

View File

@@ -21,7 +21,6 @@ If you're a mere mortal like me and you're tired of hearing how powerful git is
- [Binary releases](#binary-releases)
- [Homebrew](#homebrew)
- [MacPorts](#macports)
- [Ubuntu](#ubuntu)
- [Void Linux](#void-linux)
- [Scoop (Windows)](#scoop-windows)
- [Arch Linux](#arch-linux)
@@ -82,6 +81,8 @@ sudo port install lazygit
### Ubuntu
**Deprecated**: will no longer receive updates.
Packages for Ubuntu are available via [Launchpad PPA](https://launchpad.net/~lazygit-team).
```sh

View File

@@ -61,6 +61,9 @@ git:
allBranchesLogCmd: 'git log --graph --all --color=always --abbrev-commit --decorate --date=relative --pretty=medium'
overrideGpg: false # prevents lazygit from spawning a separate process when using GPG
disableForcePushing: false
os:
editCommand: '' # see 'Configuring File Editing' section
openCommand: ''
refresher:
refreshInterval: 10 # file/submodule refresh interval in seconds
fetchInterval: 60 # re-fetch interval in seconds
@@ -98,6 +101,7 @@ keybinding:
optionMenu-alt1: '?' # show help menu
select: '<space>'
goInto: '<enter>'
openRecentRepos: '<c-r>'
confirm: '<enter>'
confirm-alt1: 'y'
remove: 'd'
@@ -213,6 +217,25 @@ os:
openCommand: 'open {{filename}}'
```
### Configuring File Editing
Lazygit will edit a file with the first set editor in the following:
1. config.yaml
```yaml
os:
editCommand: 'vim' # as an example
```
2. \$(git config core.editor)
3. \$GIT_EDITOR
4. \$VISUAL
5. \$EDITOR
6. \$(which vi)
Lazygit will log an error if none of these options are set.
### Recommended Config Values
for users of VSCode

View File

@@ -318,7 +318,11 @@ func (c *GitCommand) ResetAndClean() error {
}
func (c *GitCommand) EditFileCmdStr(filename string) (string, error) {
editor := c.GetConfigValue("core.editor")
editor := c.Config.GetUserConfig().OS.EditCommand
if editor == "" {
editor = c.GetConfigValue("core.editor")
}
if editor == "" {
editor = c.OSCommand.Getenv("GIT_EDITOR")
@@ -335,7 +339,7 @@ func (c *GitCommand) EditFileCmdStr(filename string) (string, error) {
}
}
if editor == "" {
return "", errors.New("No editor defined in $GIT_EDITOR, $VISUAL, $EDITOR, or git config")
return "", errors.New("No editor defined in config file, $GIT_EDITOR, $VISUAL, $EDITOR, or git config")
}
return fmt.Sprintf("%s %s", editor, c.OSCommand.Quote(filename)), nil

View File

@@ -720,6 +720,7 @@ func TestGitCommandRemoveUntrackedFiles(t *testing.T) {
func TestEditFileCmdStr(t *testing.T) {
type scenario struct {
filename string
configEditCommand string
command func(string, ...string) *exec.Cmd
getenv func(string) string
getGitConfigValue func(string) (string, error)
@@ -729,6 +730,7 @@ func TestEditFileCmdStr(t *testing.T) {
scenarios := []scenario{
{
"test",
"",
func(name string, arg ...string) *exec.Cmd {
return secureexec.Command("exit", "1")
},
@@ -739,11 +741,30 @@ func TestEditFileCmdStr(t *testing.T) {
return "", nil
},
func(cmdStr string, err error) {
assert.EqualError(t, err, "No editor defined in $GIT_EDITOR, $VISUAL, $EDITOR, or git config")
assert.EqualError(t, err, "No editor defined in config file, $GIT_EDITOR, $VISUAL, $EDITOR, or git config")
},
},
{
"test",
"nano",
func(name string, args ...string) *exec.Cmd {
assert.Equal(t, "which", name)
return secureexec.Command("echo")
},
func(env string) string {
return ""
},
func(cf string) (string, error) {
return "", nil
},
func(cmdStr string, err error) {
assert.NoError(t, err)
assert.Equal(t, "nano \"test\"", cmdStr)
},
},
{
"test",
"",
func(name string, arg ...string) *exec.Cmd {
assert.Equal(t, "which", name)
return secureexec.Command("exit", "1")
@@ -761,6 +782,7 @@ func TestEditFileCmdStr(t *testing.T) {
},
{
"test",
"",
func(name string, arg ...string) *exec.Cmd {
assert.Equal(t, "which", name)
return secureexec.Command("exit", "1")
@@ -781,6 +803,7 @@ func TestEditFileCmdStr(t *testing.T) {
},
{
"test",
"",
func(name string, arg ...string) *exec.Cmd {
assert.Equal(t, "which", name)
return secureexec.Command("exit", "1")
@@ -802,6 +825,7 @@ func TestEditFileCmdStr(t *testing.T) {
},
{
"test",
"",
func(name string, arg ...string) *exec.Cmd {
assert.Equal(t, "which", name)
return secureexec.Command("echo")
@@ -819,6 +843,7 @@ func TestEditFileCmdStr(t *testing.T) {
},
{
"file/with space",
"",
func(name string, args ...string) *exec.Cmd {
assert.Equal(t, "which", name)
return secureexec.Command("echo")
@@ -838,6 +863,7 @@ func TestEditFileCmdStr(t *testing.T) {
for _, s := range scenarios {
gitCmd := NewDummyGitCommand()
gitCmd.Config.GetUserConfig().OS.EditCommand = s.configEditCommand
gitCmd.OSCommand.Command = s.command
gitCmd.OSCommand.Getenv = s.getenv
gitCmd.getGitConfigValue = s.getGitConfigValue

View File

@@ -1,28 +1,16 @@
package commands
import (
"regexp"
"sort"
"strconv"
"strings"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/utils"
)
const semverRegex = `v?((\d+\.?)+)([^\d]?.*)`
func convertToInt(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
return 0
}
return i
}
func (c *GitCommand) GetTags() ([]*models.Tag, error) {
// get remote branches
remoteBranchesStr, err := c.OSCommand.RunCommandWithOutput(`git tag --list`)
// get remote branches, sorted by creation date (descending)
// see: https://git-scm.com/docs/git-tag#Documentation/git-tag.txt---sortltkeygt
remoteBranchesStr, err := c.OSCommand.RunCommandWithOutput(`git tag --list --sort=-creatordate`)
if err != nil {
return nil, err
}
@@ -37,52 +25,10 @@ func (c *GitCommand) GetTags() ([]*models.Tag, error) {
// first step is to get our remotes from go-git
tags := make([]*models.Tag, len(split))
for i, tagName := range split {
tags[i] = &models.Tag{
Name: tagName,
}
}
// now lets sort our tags by name numerically
re := regexp.MustCompile(semverRegex)
// the reason this is complicated is because we're both sorting alphabetically
// and when we're dealing with semver strings
sort.Slice(tags, func(i, j int) bool {
a := tags[i].Name
b := tags[j].Name
matchA := re.FindStringSubmatch(a)
matchB := re.FindStringSubmatch(b)
if len(matchA) > 0 && len(matchB) > 0 {
numbersA := strings.Split(matchA[1], ".")
numbersB := strings.Split(matchB[1], ".")
k := 0
for {
if len(numbersA) == k && len(numbersB) == k {
break
}
if len(numbersA) == k {
return true
}
if len(numbersB) == k {
return false
}
if convertToInt(numbersA[k]) < convertToInt(numbersB[k]) {
return true
}
if convertToInt(numbersA[k]) > convertToInt(numbersB[k]) {
return false
}
k++
}
return strings.ToLower(matchA[3]) < strings.ToLower(matchB[3])
}
return strings.ToLower(a) < strings.ToLower(b)
})
return tags, nil
}

View File

@@ -5,6 +5,7 @@ package config
// GetPlatformDefaultConfig gets the defaults for the platform
func GetPlatformDefaultConfig() OSConfig {
return OSConfig{
EditCommand: ``,
OpenCommand: "open {{filename}}",
OpenLinkCommand: "open {{link}}",
}

View File

@@ -3,6 +3,7 @@ package config
// GetPlatformDefaultConfig gets the defaults for the platform
func GetPlatformDefaultConfig() OSConfig {
return OSConfig{
EditCommand: ``,
OpenCommand: `sh -c "xdg-open {{filename}} >/dev/null"`,
OpenLinkCommand: `sh -c "xdg-open {{link}} >/dev/null"`,
}

View File

@@ -3,6 +3,7 @@ package config
// GetPlatformDefaultConfig gets the defaults for the platform
func GetPlatformDefaultConfig() OSConfig {
return OSConfig{
EditCommand: ``,
OpenCommand: `cmd /c "start "" {{filename}}"`,
OpenLinkCommand: `cmd /c "start "" {{link}}"`,
}

View File

@@ -160,6 +160,7 @@ type KeybindingUniversalConfig struct {
DiffingMenu string `yaml:"diffingMenu"`
DiffingMenuAlt string `yaml:"diffingMenu-alt"`
CopyToClipboard string `yaml:"copyToClipboard"`
OpenRecentRepos string `yaml:"openRecentRepos"`
SubmitEditorText string `yaml:"submitEditorText"`
AppendNewline string `yaml:"appendNewline"`
ExtrasMenu string `yaml:"extrasMenu"`
@@ -247,6 +248,9 @@ type KeybindingSubmodulesConfig struct {
// OSConfig contains config on the level of the os
type OSConfig struct {
// EditCommand is the command for editing a file
EditCommand string `yaml:"editCommand,omitempty"`
// OpenCommand is the command for opening a file
OpenCommand string `yaml:"openCommand,omitempty"`
@@ -372,6 +376,7 @@ func GetDefaultConfig() *UserConfig {
New: "n",
Edit: "e",
OpenFile: "o",
OpenRecentRepos: "<c-r>",
ScrollUpMain: "<pgup>",
ScrollDownMain: "<pgdown>",
ScrollUpMainAlt1: "K",

View File

@@ -505,8 +505,8 @@ func (gui *Gui) handleNewBranchOffCurrentItem() error {
prefilledName := ""
if context.GetKey() == REMOTE_BRANCHES_CONTEXT_KEY {
// will set to the remote's existing name
prefilledName = item.ID()
// will set to the remote's branch name without the remote name
prefilledName = strings.SplitAfterN(item.ID(), "/", 2)[1]
}
return gui.prompt(promptOpts{

View File

@@ -231,6 +231,13 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
Modifier: gocui.ModNone,
Handler: gui.handleTopLevelReturn,
},
{
ViewName: "",
Key: gui.getKey(config.Universal.OpenRecentRepos),
Handler: gui.handleCreateRecentReposMenu,
Alternative: "<c-r>",
Description: gui.Tr.SwitchRepo,
},
{
ViewName: "",
Key: gui.getKey(config.Universal.ScrollUpMain),

52
pkg/gui/lbl/focus.go Normal file
View File

@@ -0,0 +1,52 @@
package lbl
import "github.com/jesseduffield/lazygit/pkg/utils"
func calculateOrigin(currentOrigin int, bufferHeight int, firstLineIdx int, lastLineIdx int, selectedLineIdx int, mode selectMode) int {
needToSeeIdx, wantToSeeIdx := getNeedAndWantLineIdx(firstLineIdx, lastLineIdx, selectedLineIdx, mode)
return calculateNewOriginWithNeededAndWantedIdx(currentOrigin, bufferHeight, needToSeeIdx, wantToSeeIdx)
}
// we want to scroll our origin so that the index we need to see is in view
// and the other index we want to see (e.g. the other side of a line range)
// is in as close to being in view as possible.
func calculateNewOriginWithNeededAndWantedIdx(currentOrigin int, bufferHeight int, needToSeeIdx int, wantToSeeIdx int) int {
origin := currentOrigin
if needToSeeIdx < currentOrigin {
origin = needToSeeIdx
} else if needToSeeIdx > currentOrigin+bufferHeight {
origin = needToSeeIdx - bufferHeight
}
bottom := origin + bufferHeight
if wantToSeeIdx < origin {
requiredChange := origin - wantToSeeIdx
allowedChange := bottom - needToSeeIdx
return origin - utils.Min(requiredChange, allowedChange)
} else if wantToSeeIdx > origin+bufferHeight {
requiredChange := wantToSeeIdx - bottom
allowedChange := needToSeeIdx - origin
return origin + utils.Min(requiredChange, allowedChange)
} else {
return origin
}
}
func getNeedAndWantLineIdx(firstLineIdx int, lastLineIdx int, selectedLineIdx int, mode selectMode) (int, int) {
switch mode {
case LINE:
return selectedLineIdx, selectedLineIdx
case RANGE:
if selectedLineIdx == firstLineIdx {
return firstLineIdx, lastLineIdx
} else {
return lastLineIdx, firstLineIdx
}
case HUNK:
return firstLineIdx, lastLineIdx
default:
panic("unknown mode")
}
}

100
pkg/gui/lbl/focus_test.go Normal file
View File

@@ -0,0 +1,100 @@
package lbl
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewOrigin(t *testing.T) {
type scenario struct {
name string
origin int
bufferHeight int
firstLineIdx int
lastLineIdx int
selectedLineIdx int
selectMode selectMode
expected int
}
scenarios := []scenario{
{
name: "selection above scroll window",
origin: 50,
bufferHeight: 100,
firstLineIdx: 10,
lastLineIdx: 10,
selectedLineIdx: 10,
selectMode: LINE,
expected: 10,
},
{
name: "selection below scroll window",
origin: 0,
bufferHeight: 100,
firstLineIdx: 150,
lastLineIdx: 150,
selectedLineIdx: 150,
selectMode: LINE,
expected: 50,
},
{
name: "selection within scroll window",
origin: 0,
bufferHeight: 100,
firstLineIdx: 50,
lastLineIdx: 50,
selectedLineIdx: 50,
selectMode: LINE,
expected: 0,
},
{
name: "range ending below scroll window with selection at end of range",
origin: 0,
bufferHeight: 100,
firstLineIdx: 40,
lastLineIdx: 150,
selectedLineIdx: 150,
selectMode: RANGE,
expected: 50,
},
{
name: "range ending below scroll window with selection at beginning of range",
origin: 0,
bufferHeight: 100,
firstLineIdx: 40,
lastLineIdx: 150,
selectedLineIdx: 40,
selectMode: RANGE,
expected: 40,
},
{
name: "range starting above scroll window with selection at beginning of range",
origin: 50,
bufferHeight: 100,
firstLineIdx: 40,
lastLineIdx: 150,
selectedLineIdx: 40,
selectMode: RANGE,
expected: 40,
},
{
name: "hunk extending beyond both bounds of scroll window",
origin: 50,
bufferHeight: 100,
firstLineIdx: 40,
lastLineIdx: 200,
selectedLineIdx: 70,
selectMode: HUNK,
expected: 40,
},
}
for _, s := range scenarios {
s := s
t.Run(s.name, func(t *testing.T) {
assert.EqualValues(t, s.expected, calculateOrigin(s.origin, s.bufferHeight, s.firstLineIdx, s.lastLineIdx, s.selectedLineIdx, s.selectMode))
})
}
}

View File

@@ -189,3 +189,9 @@ func (s *State) SelectTop() {
s.SetLineSelectMode()
s.SelectLine(0)
}
func (s *State) CalculateOrigin(currentOrigin int, bufferHeight int) int {
firstLineIdx, lastLineIdx := s.SelectedRange()
return calculateOrigin(currentOrigin, bufferHeight, firstLineIdx, lastLineIdx, s.GetSelectedLineIdx(), s.selectMode)
}

View File

@@ -155,25 +155,16 @@ func (gui *Gui) focusSelection(state *LblPanelState) error {
bufferHeight := viewHeight - 1
_, origin := stagingView.Origin()
firstLineIdx, lastLineIdx := state.SelectedRange()
selectedLineIdx := state.GetSelectedLineIdx()
margin := 0 // we may want to have a margin in place to show context but right now I'm thinking we keep this at zero
var newOrigin int
if firstLineIdx-origin < margin {
newOrigin = firstLineIdx - margin
} else if lastLineIdx-origin > bufferHeight-margin {
newOrigin = lastLineIdx - bufferHeight + margin
} else {
newOrigin = origin
}
newOrigin := state.CalculateOrigin(origin, bufferHeight)
gui.g.Update(func(*gocui.Gui) error {
if err := stagingView.SetOrigin(0, newOrigin); err != nil {
return err
}
return stagingView.SetCursor(0, state.GetSelectedLineIdx()-newOrigin)
return stagingView.SetCursor(0, selectedLineIdx-newOrigin)
})
return nil

View File

@@ -0,0 +1,60 @@
package mergeconflicts
import (
"strings"
"github.com/jesseduffield/lazygit/pkg/utils"
)
// LineType tells us whether a given line is a start/middle/end marker of a conflict,
// or if it's not a marker at all
type LineType int
const (
START LineType = iota
MIDDLE
END
NOT_A_MARKER
)
func findConflicts(content string) []*mergeConflict {
conflicts := make([]*mergeConflict, 0)
if content == "" {
return conflicts
}
var newConflict *mergeConflict
for i, line := range utils.SplitLines(content) {
switch determineLineType(line) {
case START:
newConflict = &mergeConflict{start: i}
case MIDDLE:
newConflict.middle = i
case END:
newConflict.end = i
conflicts = append(conflicts, newConflict)
// reset value to avoid any possible silent mutations in further iterations
newConflict = nil
default:
// line isn't a merge conflict marker so we just continue
}
}
return conflicts
}
func determineLineType(line string) LineType {
trimmedLine := strings.TrimPrefix(line, "++")
switch {
case strings.HasPrefix(trimmedLine, "<<<<<<< "):
return START
case trimmedLine == "=======":
return MIDDLE
case strings.HasPrefix(trimmedLine, ">>>>>>> "):
return END
default:
return NOT_A_MARKER
}
}

View File

@@ -0,0 +1,57 @@
package mergeconflicts
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDetermineLineType(t *testing.T) {
type scenario struct {
line string
expected LineType
}
scenarios := []scenario{
{
line: "",
expected: NOT_A_MARKER,
},
{
line: "blah",
expected: NOT_A_MARKER,
},
{
line: "<<<<<<< HEAD",
expected: START,
},
{
line: "<<<<<<< HEAD:my_branch",
expected: START,
},
{
line: "<<<<<<< MERGE_HEAD:my_branch",
expected: START,
},
{
line: "<<<<<<< Updated upstream:my_branch",
expected: START,
},
{
line: "<<<<<<< ours:my_branch",
expected: START,
},
{
line: "=======",
expected: MIDDLE,
},
{
line: ">>>>>>> blah",
expected: END,
},
}
for _, s := range scenarios {
assert.EqualValues(t, s.expected, determineLineType(s.line))
}
}

View File

@@ -1,9 +1,6 @@
package mergeconflicts
import (
"bufio"
"os"
"strings"
"sync"
"github.com/golang-collections/collections/stack"
@@ -19,7 +16,7 @@ const (
)
// mergeConflict : A git conflict with a start middle and end corresponding to line
// numbers in the file where the conflict bars appear
// numbers in the file where the conflict markers appear
type mergeConflict struct {
start int
middle int
@@ -88,32 +85,6 @@ func (s *State) SetConflictsFromCat(cat string) {
s.setConflicts(findConflicts(cat))
}
func findConflicts(content string) []*mergeConflict {
conflicts := make([]*mergeConflict, 0)
if content == "" {
return conflicts
}
var newConflict *mergeConflict
for i, line := range utils.SplitLines(content) {
trimmedLine := strings.TrimPrefix(line, "++")
switch trimmedLine {
case "<<<<<<< HEAD", "<<<<<<< MERGE_HEAD", "<<<<<<< Updated upstream", "<<<<<<< ours":
newConflict = &mergeConflict{start: i}
case "=======":
newConflict.middle = i
default:
if strings.HasPrefix(trimmedLine, ">>>>>>> ") {
newConflict.end = i
conflicts = append(conflicts, newConflict)
}
}
}
return conflicts
}
func (s *State) setConflicts(conflicts []*mergeConflict) {
s.conflicts = conflicts
@@ -154,32 +125,29 @@ func (s *State) ContentAfterConflictResolve(path string, selection Selection) (b
return false, "", nil
}
file, err := os.Open(path)
if err != nil {
return false, "", err
}
defer file.Close()
reader := bufio.NewReader(file)
content := ""
for i := 0; true; i++ {
line, err := reader.ReadString('\n')
if err != nil {
break
}
err := utils.ForEachLineInFile(path, func(line string, i int) {
if !isIndexToDelete(i, conflict, selection) {
content += line
}
})
if err != nil {
return false, "", err
}
return true, content, nil
}
func isIndexToDelete(i int, conflict *mergeConflict, selection Selection) bool {
return i == conflict.middle ||
i == conflict.start ||
i == conflict.end ||
selection != BOTH &&
(selection == BOTTOM && i > conflict.start && i < conflict.middle) ||
(selection == TOP && i > conflict.middle && i < conflict.end)
isMarkerLine :=
i == conflict.middle ||
i == conflict.start ||
i == conflict.end
isUnwantedContent :=
(selection == BOTTOM && conflict.start < i && i < conflict.middle) ||
(selection == TOP && conflict.middle < i && i < conflict.end)
return isMarkerLine || isUnwantedContent
}

View File

@@ -0,0 +1,103 @@
package mergeconflicts
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestFindConflicts(t *testing.T) {
type scenario struct {
name string
content string
expected []*mergeConflict
}
scenarios := []scenario{
{
name: "empty",
content: "",
expected: []*mergeConflict{},
},
{
name: "various conflicts",
content: `++<<<<<<< HEAD
foo
++=======
bar
++>>>>>>> branch
<<<<<<< HEAD: foo/bar/baz.go
foo
bar
=======
baz
>>>>>>> branch
++<<<<<<< MERGE_HEAD
foo
++=======
bar
++>>>>>>> branch
++<<<<<<< Updated upstream
foo
++=======
bar
++>>>>>>> branch
++<<<<<<< ours
foo
++=======
bar
++>>>>>>> branch
<<<<<<< Updated upstream: foo/bar/baz.go
foo
bar
=======
baz
>>>>>>> branch
`,
expected: []*mergeConflict{
{
start: 0,
middle: 2,
end: 4,
},
{
start: 6,
middle: 9,
end: 11,
},
{
start: 13,
middle: 15,
end: 17,
},
{
start: 19,
middle: 21,
end: 23,
},
{
start: 25,
middle: 27,
end: 29,
},
{
start: 31,
middle: 34,
end: 36,
},
},
},
}
for _, s := range scenarios {
s := s
t.Run(s.name, func(t *testing.T) {
assert.EqualValues(t, s.expected, findConflicts(s.content))
})
}
}

View File

@@ -539,7 +539,7 @@ Thanks for using lazygit! Seriously you rock. Three things to share with you:
1) If you want to learn about lazygit's features, watch this vid:
https://youtu.be/CPLdltN7wgE
3) Be sure to read the latest release notes at:
2) Be sure to read the latest release notes at:
https://github.com/jesseduffield/lazygit/releases
3) If you're using git, that makes you a programmer! With your help we can make

49
pkg/utils/color.go Normal file
View File

@@ -0,0 +1,49 @@
package utils
import (
"fmt"
"regexp"
"github.com/fatih/color"
)
// ColoredString takes a string and a colour attribute and returns a colored
// string with that attribute
func ColoredString(str string, colorAttributes ...color.Attribute) string {
colour := color.New(colorAttributes...)
return ColoredStringDirect(str, colour)
}
// ColoredStringDirect used for aggregating a few color attributes rather than
// just sending a single one
func ColoredStringDirect(str string, colour *color.Color) string {
return colour.SprintFunc()(fmt.Sprint(str))
}
// Decolorise strips a string of color
func Decolorise(str string) string {
re := regexp.MustCompile(`\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]`)
return re.ReplaceAllString(str, "")
}
func getPadWidths(stringArrays [][]string) []int {
maxWidth := 0
for _, stringArray := range stringArrays {
if len(stringArray) > maxWidth {
maxWidth = len(stringArray)
}
}
if maxWidth-1 < 0 {
return []int{}
}
padWidths := make([]int, maxWidth-1)
for i := range padWidths {
for _, strings := range stringArrays {
uncoloredString := Decolorise(strings[i])
if len(uncoloredString) > padWidths[i] {
padWidths[i] = len(uncoloredString)
}
}
}
return padWidths
}

39
pkg/utils/formatting.go Normal file
View File

@@ -0,0 +1,39 @@
package utils
import "strings"
// WithPadding pads a string as much as you want
func WithPadding(str string, padding int) string {
uncoloredStr := Decolorise(str)
if padding < len(uncoloredStr) {
return str
}
return str + strings.Repeat(" ", padding-len(uncoloredStr))
}
func RenderDisplayStrings(displayStringsArr [][]string) string {
padWidths := getPadWidths(displayStringsArr)
paddedDisplayStrings := getPaddedDisplayStrings(displayStringsArr, padWidths)
return strings.Join(paddedDisplayStrings, "\n")
}
func getPaddedDisplayStrings(stringArrays [][]string, padWidths []int) []string {
paddedDisplayStrings := make([]string, len(stringArrays))
for i, stringArray := range stringArrays {
if len(stringArray) == 0 {
continue
}
for j, padWidth := range padWidths {
if len(stringArray)-1 < j {
continue
}
paddedDisplayStrings[i] += WithPadding(stringArray[j], padWidth) + " "
}
if len(stringArray)-1 < len(padWidths) {
continue
}
paddedDisplayStrings[i] += stringArray[len(padWidths)]
}
return paddedDisplayStrings
}

View File

@@ -0,0 +1,81 @@
package utils
import (
"testing"
"github.com/stretchr/testify/assert"
)
// TestWithPadding is a function.
func TestWithPadding(t *testing.T) {
type scenario struct {
str string
padding int
expected string
}
scenarios := []scenario{
{
"hello world !",
1,
"hello world !",
},
{
"hello world !",
14,
"hello world ! ",
},
}
for _, s := range scenarios {
assert.EqualValues(t, s.expected, WithPadding(s.str, s.padding))
}
}
// TestGetPaddedDisplayStrings is a function.
func TestGetPaddedDisplayStrings(t *testing.T) {
type scenario struct {
stringArrays [][]string
padWidths []int
expected []string
}
scenarios := []scenario{
{
[][]string{{"a", "b"}, {"c", "d"}},
[]int{1},
[]string{"a b", "c d"},
},
}
for _, s := range scenarios {
assert.EqualValues(t, s.expected, getPaddedDisplayStrings(s.stringArrays, s.padWidths))
}
}
// TestGetPadWidths is a function.
func TestGetPadWidths(t *testing.T) {
type scenario struct {
stringArrays [][]string
expected []int
}
scenarios := []scenario{
{
[][]string{{""}, {""}},
[]int{},
},
{
[][]string{{"a"}, {""}},
[]int{},
},
{
[][]string{{"aa", "b", "ccc"}, {"c", "d", "e"}},
[]int{2, 1},
},
}
for _, s := range scenarios {
assert.EqualValues(t, s.expected, getPadWidths(s.stringArrays))
}
}

25
pkg/utils/io.go Normal file
View File

@@ -0,0 +1,25 @@
package utils
import (
"bufio"
"os"
)
func ForEachLineInFile(path string, f func(string, int)) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
reader := bufio.NewReader(file)
for i := 0; true; i++ {
line, err := reader.ReadString('\n')
if err != nil {
break
}
f(line, i)
}
return nil
}

34
pkg/utils/lines.go Normal file
View File

@@ -0,0 +1,34 @@
package utils
import "strings"
// SplitLines takes a multiline string and splits it on newlines
// currently we are also stripping \r's which may have adverse effects for
// windows users (but no issues have been raised yet)
func SplitLines(multilineString string) []string {
multilineString = strings.Replace(multilineString, "\r", "", -1)
if multilineString == "" || multilineString == "\n" {
return make([]string, 0)
}
lines := strings.Split(multilineString, "\n")
if lines[len(lines)-1] == "" {
return lines[:len(lines)-1]
}
return lines
}
// TrimTrailingNewline - Trims the trailing newline
// TODO: replace with `chomp` after refactor
func TrimTrailingNewline(str string) string {
if strings.HasSuffix(str, "\n") {
return str[:len(str)-1]
}
return str
}
// NormalizeLinefeeds - Removes all Windows and Mac style line feeds
func NormalizeLinefeeds(str string) string {
str = strings.Replace(str, "\r\n", "\n", -1)
str = strings.Replace(str, "\r", "", -1)
return str
}

94
pkg/utils/lines_test.go Normal file
View File

@@ -0,0 +1,94 @@
package utils
import (
"testing"
"github.com/stretchr/testify/assert"
)
// TestSplitLines is a function.
func TestSplitLines(t *testing.T) {
type scenario struct {
multilineString string
expected []string
}
scenarios := []scenario{
{
"",
[]string{},
},
{
"\n",
[]string{},
},
{
"hello world !\nhello universe !\n",
[]string{
"hello world !",
"hello universe !",
},
},
}
for _, s := range scenarios {
assert.EqualValues(t, s.expected, SplitLines(s.multilineString))
}
}
// TestTrimTrailingNewline is a function.
func TestTrimTrailingNewline(t *testing.T) {
type scenario struct {
str string
expected string
}
scenarios := []scenario{
{
"hello world !\n",
"hello world !",
},
{
"hello world !",
"hello world !",
},
}
for _, s := range scenarios {
assert.EqualValues(t, s.expected, TrimTrailingNewline(s.str))
}
}
// TestNormalizeLinefeeds is a function.
func TestNormalizeLinefeeds(t *testing.T) {
type scenario struct {
byteArray []byte
expected []byte
}
var scenarios = []scenario{
{
// \r\n
[]byte{97, 115, 100, 102, 13, 10},
[]byte{97, 115, 100, 102, 10},
},
{
// bash\r\nblah
[]byte{97, 115, 100, 102, 13, 10, 97, 115, 100, 102},
[]byte{97, 115, 100, 102, 10, 97, 115, 100, 102},
},
{
// \r
[]byte{97, 115, 100, 102, 13},
[]byte{97, 115, 100, 102},
},
{
// \n
[]byte{97, 115, 100, 102, 10},
[]byte{97, 115, 100, 102, 10},
},
}
for _, s := range scenarios {
assert.EqualValues(t, string(s.expected), NormalizeLinefeeds(string(s.byteArray)))
}
}

118
pkg/utils/slice.go Normal file
View File

@@ -0,0 +1,118 @@
package utils
// IncludesString if the list contains the string
func IncludesString(list []string, a string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
// IncludesInt if the list contains the Int
func IncludesInt(list []int, a int) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
// NextIndex returns the index of the element that comes after the given number
func NextIndex(numbers []int, currentNumber int) int {
for index, number := range numbers {
if number > currentNumber {
return index
}
}
return len(numbers) - 1
}
// PrevIndex returns the index that comes before the given number, cycling if we reach the end
func PrevIndex(numbers []int, currentNumber int) int {
end := len(numbers) - 1
for i := end; i >= 0; i-- {
if numbers[i] < currentNumber {
return i
}
}
return 0
}
// UnionInt returns the union of two int arrays
func UnionInt(a, b []int) []int {
m := make(map[int]bool)
for _, item := range a {
m[item] = true
}
for _, item := range b {
if _, ok := m[item]; !ok {
// this does not mutate the original a slice
// though it does mutate the backing array I believe
// but that doesn't matter because if you later want to append to the
// original a it must see that the backing array has been changed
// and create a new one
a = append(a, item)
}
}
return a
}
// DifferenceInt returns the difference of two int arrays
func DifferenceInt(a, b []int) []int {
result := []int{}
m := make(map[int]bool)
for _, item := range b {
m[item] = true
}
for _, item := range a {
if _, ok := m[item]; !ok {
result = append(result, item)
}
}
return result
}
// NextIntInCycle returns the next int in a slice, returning to the first index if we've reached the end
func NextIntInCycle(sl []int, current int) int {
for i, val := range sl {
if val == current {
if i == len(sl)-1 {
return sl[0]
}
return sl[i+1]
}
}
return sl[0]
}
// PrevIntInCycle returns the prev int in a slice, returning to the first index if we've reached the end
func PrevIntInCycle(sl []int, current int) int {
for i, val := range sl {
if val == current {
if i > 0 {
return sl[i-1]
}
return sl[len(sl)-1]
}
}
return sl[len(sl)-1]
}
func StringArraysOverlap(strArrA []string, strArrB []string) bool {
for _, first := range strArrA {
for _, second := range strArrB {
if first == second {
return true
}
}
}
return false
}

135
pkg/utils/slice_test.go Normal file
View File

@@ -0,0 +1,135 @@
package utils
import (
"testing"
"github.com/stretchr/testify/assert"
)
// TestIncludesString is a function.
func TestIncludesString(t *testing.T) {
type scenario struct {
list []string
element string
expected bool
}
scenarios := []scenario{
{
[]string{"a", "b"},
"a",
true,
},
{
[]string{"a", "b"},
"c",
false,
},
{
[]string{"a", "b"},
"",
false,
},
{
[]string{""},
"",
true,
},
}
for _, s := range scenarios {
assert.EqualValues(t, s.expected, IncludesString(s.list, s.element))
}
}
func TestNextIndex(t *testing.T) {
type scenario struct {
testName string
list []int
element int
expected int
}
scenarios := []scenario{
{
// I'm not really fussed about how it behaves here
"no elements",
[]int{},
1,
-1,
},
{
"one element",
[]int{1},
1,
0,
},
{
"two elements",
[]int{1, 2},
1,
1,
},
{
"two elements, giving second one",
[]int{1, 2},
2,
1,
},
{
"three elements, giving second one",
[]int{1, 2, 3},
2,
2,
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
assert.EqualValues(t, s.expected, NextIndex(s.list, s.element))
})
}
}
func TestPrevIndex(t *testing.T) {
type scenario struct {
testName string
list []int
element int
expected int
}
scenarios := []scenario{
{
// I'm not really fussed about how it behaves here
"no elements",
[]int{},
1,
0,
},
{
"one element",
[]int{1},
1,
0,
},
{
"two elements",
[]int{1, 2},
1,
0,
},
{
"three elements, giving second one",
[]int{1, 2, 3},
2,
0,
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
assert.EqualValues(t, s.expected, PrevIndex(s.list, s.element))
})
}
}

30
pkg/utils/template.go Normal file
View File

@@ -0,0 +1,30 @@
package utils
import (
"bytes"
"strings"
"text/template"
)
func ResolveTemplate(templateStr string, object interface{}) (string, error) {
tmpl, err := template.New("template").Parse(templateStr)
if err != nil {
return "", err
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, object); err != nil {
return "", err
}
return buf.String(), nil
}
// ResolvePlaceholderString populates a template with values
func ResolvePlaceholderString(str string, arguments map[string]string) string {
for key, value := range arguments {
str = strings.Replace(str, "{{"+key+"}}", value, -1)
str = strings.Replace(str, "{{."+key+"}}", value, -1)
}
return str
}

View File

@@ -0,0 +1,61 @@
package utils
import (
"testing"
"github.com/stretchr/testify/assert"
)
// TestResolvePlaceholderString is a function.
func TestResolvePlaceholderString(t *testing.T) {
type scenario struct {
templateString string
arguments map[string]string
expected string
}
scenarios := []scenario{
{
"",
map[string]string{},
"",
},
{
"hello",
map[string]string{},
"hello",
},
{
"hello {{arg}}",
map[string]string{},
"hello {{arg}}",
},
{
"hello {{arg}}",
map[string]string{"arg": "there"},
"hello there",
},
{
"hello",
map[string]string{"arg": "there"},
"hello",
},
{
"{{nothing}}",
map[string]string{"nothing": ""},
"",
},
{
"{{}} {{ this }} { should not throw}} an {{{{}}}} error",
map[string]string{
"blah": "blah",
"this": "won't match",
},
"{{}} {{ this }} { should not throw}} an {{{{}}}} error",
},
}
for _, s := range scenarios {
assert.EqualValues(t, s.expected, ResolvePlaceholderString(s.templateString, s.arguments))
}
}

View File

@@ -1,7 +1,6 @@
package utils
import (
"bytes"
"encoding/json"
"fmt"
"log"
@@ -11,50 +10,11 @@ import (
"runtime"
"strconv"
"strings"
"text/template"
"time"
"github.com/fatih/color"
"github.com/jesseduffield/gocui"
)
// SplitLines takes a multiline string and splits it on newlines
// currently we are also stripping \r's which may have adverse effects for
// windows users (but no issues have been raised yet)
func SplitLines(multilineString string) []string {
multilineString = strings.Replace(multilineString, "\r", "", -1)
if multilineString == "" || multilineString == "\n" {
return make([]string, 0)
}
lines := strings.Split(multilineString, "\n")
if lines[len(lines)-1] == "" {
return lines[:len(lines)-1]
}
return lines
}
// WithPadding pads a string as much as you want
func WithPadding(str string, padding int) string {
uncoloredStr := Decolorise(str)
if padding < len(uncoloredStr) {
return str
}
return str + strings.Repeat(" ", padding-len(uncoloredStr))
}
// ColoredString takes a string and a colour attribute and returns a colored
// string with that attribute
func ColoredString(str string, colorAttributes ...color.Attribute) string {
colour := color.New(colorAttributes...)
return ColoredStringDirect(str, colour)
}
// ColoredStringDirect used for aggregating a few color attributes rather than
// just sending a single one
func ColoredStringDirect(str string, colour *color.Color) string {
return colour.SprintFunc()(fmt.Sprint(str))
}
// GetCurrentRepoName gets the repo's base name
func GetCurrentRepoName() string {
pwd, err := os.Getwd()
@@ -64,22 +24,6 @@ func GetCurrentRepoName() string {
return filepath.Base(pwd)
}
// TrimTrailingNewline - Trims the trailing newline
// TODO: replace with `chomp` after refactor
func TrimTrailingNewline(str string) string {
if strings.HasSuffix(str, "\n") {
return str[:len(str)-1]
}
return str
}
// NormalizeLinefeeds - Removes all Windows and Mac style line feeds
func NormalizeLinefeeds(str string) string {
str = strings.Replace(str, "\r\n", "\n", -1)
str = strings.Replace(str, "\r", "", -1)
return str
}
// GetProjectRoot returns the path to the root of the project. Only to be used
// in testing contexts, as with binaries it's unlikely this path will exist on
// the machine
@@ -100,15 +44,6 @@ func Loader() string {
return characters[index : index+1]
}
// ResolvePlaceholderString populates a template with values
func ResolvePlaceholderString(str string, arguments map[string]string) string {
for key, value := range arguments {
str = strings.Replace(str, "{{"+key+"}}", value, -1)
str = strings.Replace(str, "{{."+key+"}}", value, -1)
}
return str
}
// Min returns the minimum of two integers
func Min(x, y int) int {
if x < y {
@@ -117,156 +52,11 @@ func Min(x, y int) int {
return y
}
func RenderDisplayStrings(displayStringsArr [][]string) string {
padWidths := getPadWidths(displayStringsArr)
paddedDisplayStrings := getPaddedDisplayStrings(displayStringsArr, padWidths)
return strings.Join(paddedDisplayStrings, "\n")
}
// Decolorise strips a string of color
func Decolorise(str string) string {
re := regexp.MustCompile(`\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]`)
return re.ReplaceAllString(str, "")
}
func getPadWidths(stringArrays [][]string) []int {
maxWidth := 0
for _, stringArray := range stringArrays {
if len(stringArray) > maxWidth {
maxWidth = len(stringArray)
}
}
if maxWidth-1 < 0 {
return []int{}
}
padWidths := make([]int, maxWidth-1)
for i := range padWidths {
for _, strings := range stringArrays {
uncoloredString := Decolorise(strings[i])
if len(uncoloredString) > padWidths[i] {
padWidths[i] = len(uncoloredString)
}
}
}
return padWidths
}
func getPaddedDisplayStrings(stringArrays [][]string, padWidths []int) []string {
paddedDisplayStrings := make([]string, len(stringArrays))
for i, stringArray := range stringArrays {
if len(stringArray) == 0 {
continue
}
for j, padWidth := range padWidths {
if len(stringArray)-1 < j {
continue
}
paddedDisplayStrings[i] += WithPadding(stringArray[j], padWidth) + " "
}
if len(stringArray)-1 < len(padWidths) {
continue
}
paddedDisplayStrings[i] += stringArray[len(padWidths)]
}
return paddedDisplayStrings
}
// displayArraysAligned returns true if every string array returned from our
// list of displayables has the same length
func displayArraysAligned(stringArrays [][]string) bool {
for _, strings := range stringArrays {
if len(strings) != len(stringArrays[0]) {
return false
}
}
return true
}
// IncludesString if the list contains the string
func IncludesString(list []string, a string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
// IncludesInt if the list contains the Int
func IncludesInt(list []int, a int) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
// NextIndex returns the index of the element that comes after the given number
func NextIndex(numbers []int, currentNumber int) int {
for index, number := range numbers {
if number > currentNumber {
return index
}
}
return len(numbers) - 1
}
// PrevIndex returns the index that comes before the given number, cycling if we reach the end
func PrevIndex(numbers []int, currentNumber int) int {
end := len(numbers) - 1
for i := end; i >= 0; i-- {
if numbers[i] < currentNumber {
return i
}
}
return 0
}
func AsJson(i interface{}) string {
bytes, _ := json.MarshalIndent(i, "", " ")
return string(bytes)
}
// UnionInt returns the union of two int arrays
func UnionInt(a, b []int) []int {
m := make(map[int]bool)
for _, item := range a {
m[item] = true
}
for _, item := range b {
if _, ok := m[item]; !ok {
// this does not mutate the original a slice
// though it does mutate the backing array I believe
// but that doesn't matter because if you later want to append to the
// original a it must see that the backing array has been changed
// and create a new one
a = append(a, item)
}
}
return a
}
// DifferenceInt returns the difference of two int arrays
func DifferenceInt(a, b []int) []int {
result := []int{}
m := make(map[int]bool)
for _, item := range b {
m[item] = true
}
for _, item := range a {
if _, ok := m[item]; !ok {
result = append(result, item)
}
}
return result
}
// used to keep a number n between 0 and max, allowing for wraparounds
func ModuloWithWrap(n, max int) int {
if n >= max {
@@ -278,32 +68,6 @@ func ModuloWithWrap(n, max int) int {
}
}
// NextIntInCycle returns the next int in a slice, returning to the first index if we've reached the end
func NextIntInCycle(sl []int, current int) int {
for i, val := range sl {
if val == current {
if i == len(sl)-1 {
return sl[0]
}
return sl[i+1]
}
}
return sl[0]
}
// PrevIntInCycle returns the prev int in a slice, returning to the first index if we've reached the end
func PrevIntInCycle(sl []int, current int) int {
for i, val := range sl {
if val == current {
if i > 0 {
return sl[i-1]
}
return sl[len(sl)-1]
}
}
return sl[len(sl)-1]
}
// TruncateWithEllipsis returns a string, truncated to a certain length, with an ellipsis
func TruncateWithEllipsis(str string, limit int) string {
if limit == 1 && len(str) > 1 {
@@ -329,18 +93,6 @@ func FindStringSubmatch(str string, regexpStr string) (bool, []string) {
return len(match) > 0, match
}
func StringArraysOverlap(strArrA []string, strArrB []string) bool {
for _, first := range strArrA {
for _, second := range strArrB {
if first == second {
return true
}
}
}
return false
}
func MustConvertToInt(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
@@ -349,20 +101,6 @@ func MustConvertToInt(s string) int {
return i
}
func ResolveTemplate(templateStr string, object interface{}) (string, error) {
tmpl, err := template.New("template").Parse(templateStr)
if err != nil {
return "", err
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, object); err != nil {
return "", err
}
return buf.String(), nil
}
// Safe will close tcell if a panic occurs so that we don't end up in a malformed
// terminal state
func Safe(f func()) {

View File

@@ -6,244 +6,6 @@ import (
"github.com/stretchr/testify/assert"
)
// TestSplitLines is a function.
func TestSplitLines(t *testing.T) {
type scenario struct {
multilineString string
expected []string
}
scenarios := []scenario{
{
"",
[]string{},
},
{
"\n",
[]string{},
},
{
"hello world !\nhello universe !\n",
[]string{
"hello world !",
"hello universe !",
},
},
}
for _, s := range scenarios {
assert.EqualValues(t, s.expected, SplitLines(s.multilineString))
}
}
// TestWithPadding is a function.
func TestWithPadding(t *testing.T) {
type scenario struct {
str string
padding int
expected string
}
scenarios := []scenario{
{
"hello world !",
1,
"hello world !",
},
{
"hello world !",
14,
"hello world ! ",
},
}
for _, s := range scenarios {
assert.EqualValues(t, s.expected, WithPadding(s.str, s.padding))
}
}
// TestTrimTrailingNewline is a function.
func TestTrimTrailingNewline(t *testing.T) {
type scenario struct {
str string
expected string
}
scenarios := []scenario{
{
"hello world !\n",
"hello world !",
},
{
"hello world !",
"hello world !",
},
}
for _, s := range scenarios {
assert.EqualValues(t, s.expected, TrimTrailingNewline(s.str))
}
}
// TestNormalizeLinefeeds is a function.
func TestNormalizeLinefeeds(t *testing.T) {
type scenario struct {
byteArray []byte
expected []byte
}
var scenarios = []scenario{
{
// \r\n
[]byte{97, 115, 100, 102, 13, 10},
[]byte{97, 115, 100, 102, 10},
},
{
// bash\r\nblah
[]byte{97, 115, 100, 102, 13, 10, 97, 115, 100, 102},
[]byte{97, 115, 100, 102, 10, 97, 115, 100, 102},
},
{
// \r
[]byte{97, 115, 100, 102, 13},
[]byte{97, 115, 100, 102},
},
{
// \n
[]byte{97, 115, 100, 102, 10},
[]byte{97, 115, 100, 102, 10},
},
}
for _, s := range scenarios {
assert.EqualValues(t, string(s.expected), NormalizeLinefeeds(string(s.byteArray)))
}
}
// TestResolvePlaceholderString is a function.
func TestResolvePlaceholderString(t *testing.T) {
type scenario struct {
templateString string
arguments map[string]string
expected string
}
scenarios := []scenario{
{
"",
map[string]string{},
"",
},
{
"hello",
map[string]string{},
"hello",
},
{
"hello {{arg}}",
map[string]string{},
"hello {{arg}}",
},
{
"hello {{arg}}",
map[string]string{"arg": "there"},
"hello there",
},
{
"hello",
map[string]string{"arg": "there"},
"hello",
},
{
"{{nothing}}",
map[string]string{"nothing": ""},
"",
},
{
"{{}} {{ this }} { should not throw}} an {{{{}}}} error",
map[string]string{
"blah": "blah",
"this": "won't match",
},
"{{}} {{ this }} { should not throw}} an {{{{}}}} error",
},
}
for _, s := range scenarios {
assert.EqualValues(t, s.expected, ResolvePlaceholderString(s.templateString, s.arguments))
}
}
// TestDisplayArraysAligned is a function.
func TestDisplayArraysAligned(t *testing.T) {
type scenario struct {
input [][]string
expected bool
}
scenarios := []scenario{
{
[][]string{{"", ""}, {"", ""}},
true,
},
{
[][]string{{""}, {"", ""}},
false,
},
}
for _, s := range scenarios {
assert.EqualValues(t, s.expected, displayArraysAligned(s.input))
}
}
// TestGetPaddedDisplayStrings is a function.
func TestGetPaddedDisplayStrings(t *testing.T) {
type scenario struct {
stringArrays [][]string
padWidths []int
expected []string
}
scenarios := []scenario{
{
[][]string{{"a", "b"}, {"c", "d"}},
[]int{1},
[]string{"a b", "c d"},
},
}
for _, s := range scenarios {
assert.EqualValues(t, s.expected, getPaddedDisplayStrings(s.stringArrays, s.padWidths))
}
}
// TestGetPadWidths is a function.
func TestGetPadWidths(t *testing.T) {
type scenario struct {
stringArrays [][]string
expected []int
}
scenarios := []scenario{
{
[][]string{{""}, {""}},
[]int{},
},
{
[][]string{{"a"}, {""}},
[]int{},
},
{
[][]string{{"aa", "b", "ccc"}, {"c", "d", "e"}},
[]int{2, 1},
},
}
for _, s := range scenarios {
assert.EqualValues(t, s.expected, getPadWidths(s.stringArrays))
}
}
// TestMin is a function.
func TestMin(t *testing.T) {
type scenario struct {
@@ -275,134 +37,6 @@ func TestMin(t *testing.T) {
}
}
// TestIncludesString is a function.
func TestIncludesString(t *testing.T) {
type scenario struct {
list []string
element string
expected bool
}
scenarios := []scenario{
{
[]string{"a", "b"},
"a",
true,
},
{
[]string{"a", "b"},
"c",
false,
},
{
[]string{"a", "b"},
"",
false,
},
{
[]string{""},
"",
true,
},
}
for _, s := range scenarios {
assert.EqualValues(t, s.expected, IncludesString(s.list, s.element))
}
}
func TestNextIndex(t *testing.T) {
type scenario struct {
testName string
list []int
element int
expected int
}
scenarios := []scenario{
{
// I'm not really fussed about how it behaves here
"no elements",
[]int{},
1,
-1,
},
{
"one element",
[]int{1},
1,
0,
},
{
"two elements",
[]int{1, 2},
1,
1,
},
{
"two elements, giving second one",
[]int{1, 2},
2,
1,
},
{
"three elements, giving second one",
[]int{1, 2, 3},
2,
2,
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
assert.EqualValues(t, s.expected, NextIndex(s.list, s.element))
})
}
}
func TestPrevIndex(t *testing.T) {
type scenario struct {
testName string
list []int
element int
expected int
}
scenarios := []scenario{
{
// I'm not really fussed about how it behaves here
"no elements",
[]int{},
1,
0,
},
{
"one element",
[]int{1},
1,
0,
},
{
"two elements",
[]int{1, 2},
1,
0,
},
{
"three elements, giving second one",
[]int{1, 2, 3},
2,
0,
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
assert.EqualValues(t, s.expected, PrevIndex(s.list, s.element))
})
}
}
func TestAsJson(t *testing.T) {
type myStruct struct {
a string