-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: rename 'bar' package to 'ui', move AskPassword func
- Loading branch information
Showing
3 changed files
with
92 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package ui | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"path/filepath" | ||
"slices" | ||
"syscall" | ||
|
||
"github.com/70sh1/eddy/core" | ||
"github.com/70sh1/eddy/format" | ||
"github.com/70sh1/eddy/pathutils" | ||
"github.com/cheggaaa/pb/v3" | ||
"github.com/fatih/color" | ||
"golang.org/x/term" | ||
) | ||
|
||
// Creates new progress bar pool. | ||
func NewBarPool(paths []string, noEmojiAndColor bool) (pool *pb.Pool, bars []*pb.ProgressBar) { | ||
barTmpl := `{{ string . "status" }} {{ string . "filename" }} {{ string . "filesize" }} {{ bar . "[" "-" ">" " " "]" }} {{ string . "error" }}` | ||
for _, path := range paths { | ||
bar := pb.New64(1).SetTemplateString(barTmpl).SetWidth(90) | ||
bar.Set("status", format.CondPrefix(" ", "", noEmojiAndColor)) | ||
bar.Set("filename", pathutils.FilenameOverflow(filepath.Base(path), 25)) | ||
bars = append(bars, bar) | ||
} | ||
return pb.NewPool(bars...), bars | ||
} | ||
|
||
func BarFail(bar *pb.ProgressBar, err error, noEmojiAndColor bool) { | ||
errText := err.Error() | ||
if !noEmojiAndColor { | ||
errText = color.RedString(errText) | ||
} | ||
bar.Set("status", format.CondPrefix("❌", "", noEmojiAndColor)) | ||
bar.Set("error", errText) | ||
} | ||
|
||
func AskPassword(mode core.Mode, noEmojiAndColor bool) (string, error) { | ||
fmt.Print(format.CondPrefix("🔑 ", "Password: ", noEmojiAndColor)) | ||
password, err := term.ReadPassword(int(syscall.Stdin)) | ||
if err != nil { | ||
return "", err | ||
} | ||
fmt.Print("\r") | ||
if mode == core.Encryption { | ||
fmt.Print(format.CondPrefix("🔑 ", "Confirm password: ", noEmojiAndColor)) | ||
password2, err := term.ReadPassword(int(syscall.Stdin)) | ||
if err != nil { | ||
return "", err | ||
} | ||
if !slices.Equal(password, password2) { | ||
fmt.Print("\r") | ||
return "", errors.New("passwords do not match") | ||
} | ||
} | ||
|
||
return string(password), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package ui | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewPool(t *testing.T) { | ||
cases := [][]string{ | ||
{"file1", "path/file2.dat", "home/user/docs/file2"}, | ||
{"C:/some/dir/file1.txt", "path/file3", "home/user/docs/file2"}, | ||
{"file5"}, | ||
} | ||
for _, tCase := range cases { | ||
barPool, bars := NewBarPool(tCase, false) | ||
require.Len(t, bars, len(tCase)) | ||
require.NotNil(t, barPool) | ||
for i := 0; i < len(tCase); i++ { | ||
require.Contains(t, bars[i].String(), filepath.Base(tCase[i])) | ||
} | ||
} | ||
} |