Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Viddy as an importable package. #77

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 4 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"fmt"
"os"

"github.com/adrg/xdg"
"github.com/fatih/color"
"github.com/rivo/tview"
"github.com/spf13/viper"
"github.com/sachaos/viddy/viddy"
"github.com/tcnksm/go-latest"
)

Expand All @@ -32,31 +30,16 @@ func printVersion() {
}

func main() {
v := viper.New()
v.SetConfigType("toml")
v.SetConfigName("viddy")
v.AddConfigPath(xdg.ConfigHome)

_ = v.ReadInConfig()

conf, err := newConfig(v, os.Args[1:])
if conf.runtime.help {
if os.Args[1] == "help" || os.Args[1] == "--help" || os.Args[1] == "-h" {
help()
os.Exit(0)
}

if conf.runtime.version {
if os.Args[1] == "version" || os.Args[1] == "--version" || os.Args[1] == "-v" {
printVersion()
}

if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

tview.Styles = conf.theme.Theme

app := NewViddy(conf)
app := viddy.NewPreconfigedViddy(os.Args[1:])

if err := app.Run(); err != nil {
fmt.Fprintln(os.Stderr, err)
Expand Down
4 changes: 2 additions & 2 deletions config.go → viddy/config.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package main
package viddy

import (
"errors"
"fmt"
"github.com/gdamore/tcell/v2"
"os"
"strconv"
"strings"
"time"
"unicode"

"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/spf13/cast"
"github.com/spf13/pflag"
Expand Down
2 changes: 1 addition & 1 deletion config_test.go → viddy/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package viddy

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion generator.go → viddy/generator.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package viddy

import "time"

Expand Down
2 changes: 1 addition & 1 deletion run.go → viddy/run.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build !windows

package main
package viddy

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion run_windows.go → viddy/run_windows.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build windows

package main
package viddy

import (
"bytes"
Expand Down
5 changes: 3 additions & 2 deletions snapshot.go → viddy/snapshot.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package viddy

import (
"bytes"
Expand Down Expand Up @@ -47,8 +47,9 @@ type Snapshot struct {
finish chan<- struct{}
}

//nolint:lll
// NewSnapshot returns Snapshot object.
//
//nolint:lll
func NewSnapshot(id int64, command string, args []string, shell string, shellOpts string, before *Snapshot, finish chan<- struct{}) *Snapshot {
return &Snapshot{
id: id,
Expand Down
26 changes: 24 additions & 2 deletions viddy.go → viddy/viddy.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package viddy

import (
"bytes"
Expand All @@ -14,9 +14,11 @@ import (
"sync/atomic"
"time"

"github.com/adrg/xdg"
"github.com/gdamore/tcell/v2"
"github.com/moby/term"
"github.com/rivo/tview"
"github.com/spf13/viper"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To reduce the dependency of the viddy lib, I think it will be great if we can remove dependency of the viper from lib.
What do you think?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on removing viper from the lib. assume it will stay in the TUI ... ?

)

type HistoryRow struct {
Expand Down Expand Up @@ -141,6 +143,26 @@ func NewViddy(conf *config) *Viddy {
}
}

func NewPreconfigedViddy(args []string) *Viddy {
v := viper.New()
v.SetConfigType("toml")
v.SetConfigName("viddy")
v.AddConfigPath(xdg.ConfigHome)

_ = v.ReadInConfig()

conf, err := newConfig(v, args)

if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

tview.Styles = conf.theme.Theme
preConfigedViddy := NewViddy(conf)
return preConfigedViddy
}

func (v *Viddy) ShowLogView(b bool) {
v.showLogView = b
v.arrange()
Expand Down Expand Up @@ -412,7 +434,7 @@ func (v *Viddy) arrange() {
}

// Run is entry point to run viddy.
//nolint: funlen,gocognit,cyclop
// nolint: funlen,gocognit,cyclop
func (v *Viddy) Run() error {
b := tview.NewTextView()
b.SetDynamicColors(true)
Expand Down