diff --git a/go.mod b/go.mod index f547596..fdf7f32 100644 --- a/go.mod +++ b/go.mod @@ -8,9 +8,10 @@ require ( github.com/charmbracelet/charm v0.5.1 github.com/charmbracelet/glamour v0.2.0 github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac + github.com/mattn/go-runewidth v0.0.9 + github.com/meowgorithm/babyenv v1.2.1 github.com/muesli/reflow v0.1.0 github.com/muesli/termenv v0.6.0 - github.com/mattn/go-runewidth v0.0.9 github.com/spf13/cobra v0.0.7 golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2 // indirect diff --git a/go.sum b/go.sum index 950e177..5892e69 100644 --- a/go.sum +++ b/go.sum @@ -20,7 +20,6 @@ github.com/charmbracelet/bubbles v0.2.0 h1:2tGD++FxmPUZ+DLjVcpx6HvuJ5HwlmUw2vLF9 github.com/charmbracelet/bubbles v0.2.0/go.mod h1:RA4uIEHJqv5AJuFGbjB+5YrdNJk4rsi3qxBpdHScBLY= github.com/charmbracelet/bubbletea v0.6.4-0.20200525234836-3b8b011b5a26/go.mod h1:BTzHOUvUlKecQz7ZB8NgPRWi2Z8NRCV04qwyFOfO1Kk= github.com/charmbracelet/bubbletea v0.7.0/go.mod h1:BTzHOUvUlKecQz7ZB8NgPRWi2Z8NRCV04qwyFOfO1Kk= -github.com/charmbracelet/bubbletea v0.8.0 h1:ruZFaFF+2kgCI1IwNG40KTYDW5ZvE2+hPy4odlBdUko= github.com/charmbracelet/bubbletea v0.8.0/go.mod h1:DzNhKkQQJI30eb+kBcaOs1+z86zTSqcMgSHoFY+uCsg= github.com/charmbracelet/bubbletea v0.8.1 h1:Q7qdKOl4LJa9QV/LDwFWa9DFlZwwJkQipXrrfu/+dJM= github.com/charmbracelet/bubbletea v0.8.1/go.mod h1:DzNhKkQQJI30eb+kBcaOs1+z86zTSqcMgSHoFY+uCsg= @@ -153,7 +152,6 @@ go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/ go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 h1:cg5LA/zNPRzIXIWSCxQW10Rvpy94aQh3LT/ShoCpkHw= golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 h1:vEg9joUBmeBcK9iSJftGNf3coIG4HqZElCPehJsfAYM= golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= @@ -183,7 +181,6 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200413165638-669c56c373c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 h1:OjiUf46hAmXblsZdnoSXsEUSKU8r1UEzcL5RVZ4gO9Y= golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 h1:ogLJMz+qpzav7lGMh10LMvAkM/fAoGlaiiHYiFYdm80= golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/main.go b/main.go index 3896d9c..5f2e4b9 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "path/filepath" "strings" + "github.com/meowgorithm/babyenv" "github.com/spf13/cobra" "golang.org/x/crypto/ssh/terminal" @@ -168,22 +169,29 @@ func executeArg(cmd *cobra.Command, arg string, w io.Writer) error { // Only run TUI if there are no arguments (excluding flags) if arg == "" { - // Log to a file. For debugging. - logToFilePath := os.Getenv("GLOW_LOG_TO_FILE") - if logToFilePath != "" { - f, err := tea.LogToFile(logToFilePath, "glow") + // Read environment to get debugging stuff + var cfg ui.UIConfig + if err := babyenv.Parse(&cfg); err != nil { + return fmt.Errorf("error parsing config: %v", err) + } + + // Log to file, if set + if cfg.Logfile != "" { + f, err := tea.LogToFile(cfg.Logfile, "glow") if err != nil { return err } defer f.Close() } + // Run Bubble Tea program tea.AltScreen() - if err := ui.NewProgram(style).Start(); err != nil { + if err := ui.NewProgram(style, cfg).Start(); err != nil { return err } tea.ExitAltScreen() + // Exit message fmt.Printf("\n Thanks for using Glow!\n\n") return nil } diff --git a/ui/pager.go b/ui/pager.go index 5b0acbd..7ed50cc 100644 --- a/ui/pager.go +++ b/ui/pager.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "math" - "os" "strings" "github.com/charmbracelet/bubbles/textinput" @@ -28,9 +27,6 @@ const ( ) var ( - // Whether or not to use fast scroll-based rendering for the viewport - highPerformanceRendering = true - pagerHelpHeight = strings.Count(pagerHelpView(0), "\n") noteHeading = te.String(noteHeadingText). @@ -93,6 +89,13 @@ type pagerModel struct { } func newPagerModel(glamourStyle string) pagerModel { + + // Init viewport + vp := viewport.Model{} + vp.YPosition = 0 + vp.HighPerformanceRendering = config.HighPerformancePager + + // Init text input UI for notes/memos ti := textinput.NewModel() ti.Prompt = te.String(notePromptText). Foreground(te.ColorProfile().Color(gray)). @@ -104,10 +107,6 @@ func newPagerModel(glamourStyle string) pagerModel { ti.CharLimit = noteCharacterLimit ti.Focus() - vp := viewport.Model{} - vp.YPosition = 0 - vp.HighPerformanceRendering = highPerformanceRendering - return pagerModel{ state: pagerStateBrowse, glamourStyle: glamourStyle, @@ -350,7 +349,7 @@ func saveDocumentNote(cc *charm.Client, id int, note string) tea.Cmd { // This is where the magic happens func glamourRender(m pagerModel, markdown string) (string, error) { - if os.Getenv("GLOW_DISABLE_GLAMOUR") != "" { + if !config.GlamourEnabled { return markdown, nil } diff --git a/ui/ui.go b/ui/ui.go index aa417e7..d41face 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -3,6 +3,7 @@ package ui import ( "errors" "fmt" + "log" "strings" "github.com/charmbracelet/bubbles/spinner" @@ -17,12 +18,27 @@ const ( noteCharacterLimit = 256 // totally arbitrary ) +// UIConfig contains flags for debugging the TUI. +type UIConfig struct { + Logfile string `env:"GLOW_UI_LOGFILE"` + HighPerformancePager bool `env:"GLOW_UI_HIGH_PERFORMANCE_PAGER" default:"true"` + GlamourEnabled bool `env:"GLOW_UI_ENABLE_GLAMOUR" default:"true"` +} + var ( + config UIConfig glowLogoTextColor = common.Color("#ECFD65") ) // NewProgram returns a new Tea program -func NewProgram(style string) *tea.Program { +func NewProgram(style string, cfg UIConfig) *tea.Program { + config = cfg + if config.Logfile != "" { + log.Println("-- Starting Glow ----------------") + log.Printf("High performance pager: %v", cfg.HighPerformancePager) + log.Printf("Render with Glamour: %v", cfg.GlamourEnabled) + log.Println("Bubble Tea now initializing...") + } return tea.NewProgram(initialize(style), update, view) }