Skip to content

Commit

Permalink
Merge pull request #26 from AlirezaKhadem/refactor/clean-up
Browse files Browse the repository at this point in the history
Refactoring for improvement code readability
  • Loading branch information
TheYahya authored Aug 5, 2023
2 parents b0b81f9 + 15e9912 commit d821c0e
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 71 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@

# Dependency directories (remove the comment below to include it)
vendor/

# IDE files
.idea/
.vscode/
60 changes: 42 additions & 18 deletions cmd/enola/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ import (
"github.com/charmbracelet/lipgloss"
)

const (
SelectedBorderStyleLightColor = "#F793FF"
SelectedBorderStyleDarkColor = "#AD58B4"
NormalDescStyleLightColor = "#A49FA5"
NormalDescStyleDarkColor = "#777777"
SelectedDescStyleLightColor = "#A49FA5"
SelectedDescStyleDarkColor = "#777777"
DimmedTitleStyleLightColor = "#A49FA5"
DimmedTitleStyleDarkColor = "#777777"
DimmedDescStyleLightColor = "#C2B8C2"
DimmedDescStyleDarkColor = "#4D4D4D"
)

func NewDelegate() list.DefaultDelegate {
delegate := list.NewDefaultDelegate()
delegate.ShowDescription = false
Expand All @@ -13,30 +26,41 @@ func NewDelegate() list.DefaultDelegate {
return delegate
}

func NewItemStyles() (s list.DefaultItemStyles) {
s.NormalTitle = lipgloss.NewStyle().
Padding(0, 0, 0, 1)
func NewItemStyles() (style list.DefaultItemStyles) {
style.NormalTitle = lipgloss.NewStyle().Padding(0, 0, 0, 1)

s.NormalDesc = s.NormalTitle.Copy().
Foreground(lipgloss.AdaptiveColor{Light: "#A49FA5", Dark: "#777777"}).
style.NormalDesc = style.NormalTitle.Copy().
Foreground(lipgloss.AdaptiveColor{
Light: NormalDescStyleLightColor,
Dark: NormalDescStyleDarkColor,
}).
Underline(true).
Padding(0, 0, 0, 2)

s.SelectedTitle = lipgloss.NewStyle().
style.SelectedTitle = lipgloss.NewStyle().
Border(lipgloss.ThickBorder(), false, false, false, true).
BorderForeground(lipgloss.AdaptiveColor{Light: "#F793FF", Dark: "#AD58B4"}).
Padding(0, 0, 0, 0)
BorderForeground(lipgloss.AdaptiveColor{
Light: SelectedBorderStyleLightColor,
Dark: SelectedBorderStyleDarkColor,
}).Padding(0, 0, 0, 0)

s.SelectedDesc = s.SelectedTitle.Copy().
Foreground(lipgloss.AdaptiveColor{Light: "#A49FA5", Dark: "#777777"}).
Underline(true).
Padding(0, 0, 0, 1)
style.SelectedDesc = style.SelectedTitle.Copy().
Foreground(lipgloss.AdaptiveColor{
Light: SelectedDescStyleLightColor,
Dark: SelectedDescStyleDarkColor,
}).Underline(true).Padding(0, 0, 0, 1)

style.DimmedTitle = lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{
Light: DimmedTitleStyleLightColor,
Dark: DimmedTitleStyleDarkColor,
}).Padding(0, 0, 0, 1)

s.DimmedTitle = lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#A49FA5", Dark: "#777777"}).
Padding(0, 0, 0, 1)
style.DimmedDesc = style.DimmedTitle.Copy().
Foreground(lipgloss.AdaptiveColor{
Light: DimmedDescStyleLightColor,
Dark: DimmedDescStyleDarkColor,
})

s.DimmedDesc = s.DimmedTitle.Copy().
Foreground(lipgloss.AdaptiveColor{Light: "#C2B8C2", Dark: "#4D4D4D"})
return s
return style
}
33 changes: 21 additions & 12 deletions cmd/enola/enola.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ import (
var rootCmd = &cobra.Command{
Use: "enola {username}",
Short: "A command-line tool to find username on websites",
Args: func(_ *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("can't run without argument, give me a username")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
username := args[0]
siteFlag := cmd.Flag("site")
findAndShowResult(username, siteFlag.Value.String())
},
Args: validateArgs,
Run: runCommand,
}

func validateArgs(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("missing required argument: username")
}
return nil
}

func runCommand(cmd *cobra.Command, args []string) {
username := args[0]
siteFlag := cmd.Flag("site")
findAndShowResult(username, siteFlag.Value.String())
}

func main() {
Expand All @@ -36,5 +40,10 @@ func Execute() error {
}

func init() {
rootCmd.Flags().StringP("site", "s", "", "to only search an specific site")
rootCmd.Flags().StringP(
"site",
"s",
"",
"to only search an specific site",
)
}
13 changes: 9 additions & 4 deletions cmd/enola/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,25 @@ func findAndShowResult(username, site string) {

resChan, err := sh.SetSite(site).Check(username)
if err != nil {
fmt.Println("Error running program:", err)
fmt.Println("Error running program: ", err)
os.Exit(1)
}

m := model{
list: list.New([]list.Item{}, NewDelegate(), 0, 0),
res: resChan,
list: list.New(
[]list.Item{},
NewDelegate(),
0,
0,
),
res: resChan,
}

m.list.Title = "Socials"
p := tea.NewProgram(&m, tea.WithAltScreen())

if err := p.Start(); err != nil {
fmt.Println("Error running program:", err)
fmt.Println("Error running program: ", err)
os.Exit(1)
}
}
115 changes: 86 additions & 29 deletions cmd/enola/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,98 @@ import (
"github.com/theyahya/enola"
)

const (
CheckStyleLightColor = "#13A10E"
CheckStyleDarkColor = "#13A10E"
TitleStyleLightColor = "#1a1a1a"
TitleStyleDarkColor = "#dddddd"
DescStyleLightColor = "#13A10E"
DescStyleDarkColor = "#13A10E"
CloseStyleLightColor = "#ff0000"
CloseStyleDarkColor = "#ff0000"
TitleNotFoundedLightColor = "#A49FA5"
TitleNotFoundedDarkColor = "#777777"
NotFoundLightColor = "#ff8f5f"
NotFoundDarkColor = "#ffff00"
)

var docStyle = lipgloss.NewStyle().Margin(1, 2)

type item struct {
title, desc string
found bool
title string
desc string
found bool
}

func (i item) Title() string {
style := NewItemStyles().NormalTitle.Copy()
if i.found {
checkStyle := style.Copy().
Foreground(lipgloss.AdaptiveColor{Light: "#13A10E", Dark: "#13A10E"})

titleStyle := style.Copy().
Foreground(lipgloss.AdaptiveColor{Light: "#1a1a1a", Dark: "#dddddd"}).
Bold(true).
Padding(0, 0, 0, 0)

descStyle := NewItemStyles().NormalDesc.Copy().
Foreground(lipgloss.AdaptiveColor{Light: "#13A10E", Dark: "#13A10E"}).
Padding(0, 0, 0, 0)

check := checkStyle.Render("✓")
title := titleStyle.Render(i.title)
desc := descStyle.Render(i.desc)
status, title, desc := i.renderItem(NewItemStyles().NormalTitle.Copy())
return fmt.Sprintf("%s %s: %s", status, title, desc)
}

return fmt.Sprintf("%s %s: %s", check, title, desc)
func (i item) renderItem(style lipgloss.Style) (string, string, string) {
var status, title, desc string
if i.found {
status, title, desc = i.renderFoundedItem(style)
} else {
status, title, desc = i.renderNotFoundedItem(style)
}
return status, title, desc
}

func (i item) renderNotFoundedItem(style lipgloss.Style) (string, string, string) {
closeStyle := style.Copy().
Foreground(lipgloss.AdaptiveColor{Light: "#ff0000", Dark: "#ff0000"})
Foreground(lipgloss.AdaptiveColor{
Light: CloseStyleLightColor,
Dark: CloseStyleDarkColor,
})

titleStyle := style.Copy().
Foreground(lipgloss.AdaptiveColor{Light: "#A49FA5", Dark: "#777777"}).
Padding(0, 0, 0, 0)
Foreground(lipgloss.AdaptiveColor{
Light: TitleNotFoundedLightColor,
Dark: TitleNotFoundedDarkColor,
}).Padding(0, 0, 0, 0)

notFoundStyle := style.Copy().
Foreground(lipgloss.AdaptiveColor{Light: "#ff8f5f", Dark: "#ffff00"}).
Padding(0, 0, 0, 0)
Foreground(lipgloss.AdaptiveColor{
Light: NotFoundLightColor,
Dark: NotFoundDarkColor,
}).Padding(0, 0, 0, 0)

close := closeStyle.Render("✗")
status := closeStyle.Render("✗")
title := titleStyle.Render(i.title)
notFound := notFoundStyle.Render("Not found!")
desc := notFoundStyle.Render("Not found!")

return fmt.Sprintf("%s %s: %s", close, title, notFound)
return status, title, desc
}

func (i item) Description() string { return "" }
func (i item) renderFoundedItem(style lipgloss.Style) (string, string, string) {
checkStyle := style.Copy().
Foreground(lipgloss.AdaptiveColor{
Light: CheckStyleLightColor,
Dark: CheckStyleDarkColor,
})

titleStyle := style.Copy().
Foreground(lipgloss.AdaptiveColor{
Light: TitleStyleLightColor,
Dark: TitleStyleDarkColor,
}).Bold(true).Padding(0, 0, 0, 0)

descStyle := NewItemStyles().NormalDesc.Copy().
Foreground(lipgloss.AdaptiveColor{
Light: DescStyleLightColor,
Dark: DescStyleDarkColor,
}).Padding(0, 0, 0, 0)

check := checkStyle.Render("✓")
title := titleStyle.Render(i.title)
desc := descStyle.Render(i.desc)

return check, title, desc
}

func (i item) Description() string { return i.desc }

func (i item) FilterValue() string { return i.title }

type model struct {
Expand All @@ -79,6 +124,8 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.WindowSizeMsg:
h, v := docStyle.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)


case responseMsg:
m.resCount++
if msg.Found {
Expand All @@ -94,6 +141,16 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, cmd
}

func (m *model) updateList(msg responseMsg) (tea.Model, tea.Cmd) {
m.resCount++
m.list.InsertItem(m.resCount, item{
title: msg.Name,
desc: msg.URL,
found: msg.Found,
})
return m, waitForActivity(m.res)
}

func (m *model) View() string {
return docStyle.Render(m.list.View())
}
Expand Down
8 changes: 3 additions & 5 deletions enola.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
_ "embed"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -41,11 +40,10 @@ type Result struct {
var d []byte

func New(ctx context.Context) (Enola, error) {
var err error
var data map[string]Website
err = json.Unmarshal(d, &data)
err := json.Unmarshal(d, &data)
if err != nil {
return Enola{}, errors.New(ErrDataFileIsNotAValidJson)
return Enola{}, fmt.Errorf("error: %v", ErrDataFileIsNotAValidJson)
}

return Enola{
Expand Down Expand Up @@ -75,7 +73,7 @@ func (s *Enola) Check(username string) (<-chan Result, error) {

// if site is not found in the list
if len(data) == 0 {
return nil, errors.New(ErrSiteNotFound)
return nil, fmt.Errorf("error: %v", ErrSiteNotFound)
}
}

Expand Down
7 changes: 4 additions & 3 deletions error.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package enola

const ErrNoDataFileExists = "err no data file exists"
const ErrDataFileIsNotAValidJson = "err data file is not a valid json"
const ErrSiteNotFound = "err requested site is not supported"
const (
ErrDataFileIsNotAValidJson = "the data file cannot be read due to invalid JSON format"
ErrSiteNotFound = "the requested site is not supported"
)

0 comments on commit d821c0e

Please sign in to comment.