mirror of
				https://github.com/taigrr/wtf
				synced 2025-01-18 04:03:14 -08:00 
			
		
		
		
	Merge pull request #441 from Seanstoppable/splituplogger
Split up logger and widget
This commit is contained in:
		
						commit
						bebc8362e8
					
				| @ -7,7 +7,7 @@ import ( | |||||||
| 
 | 
 | ||||||
| 	goFlags "github.com/jessevdk/go-flags" | 	goFlags "github.com/jessevdk/go-flags" | ||||||
| 	"github.com/wtfutil/wtf/help" | 	"github.com/wtfutil/wtf/help" | ||||||
| 	"github.com/wtfutil/wtf/wtf" | 	"github.com/wtfutil/wtf/utils" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| type Flags struct { | type Flags struct { | ||||||
| @ -63,7 +63,7 @@ func (flags *Flags) Parse() { | |||||||
| 	// If no config file is explicitly passed in as a param, | 	// If no config file is explicitly passed in as a param, | ||||||
| 	// set the flag to the default config file | 	// set the flag to the default config file | ||||||
| 	if !flags.HasConfig() { | 	if !flags.HasConfig() { | ||||||
| 		homeDir, err := wtf.Home() | 		homeDir, err := utils.Home() | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			fmt.Printf("Error: %v\n", err) | 			fmt.Printf("Error: %v\n", err) | ||||||
| 			os.Exit(1) | 			os.Exit(1) | ||||||
|  | |||||||
							
								
								
									
										111
									
								
								logger/log.go
									
									
									
									
									
								
							
							
						
						
									
										111
									
								
								logger/log.go
									
									
									
									
									
								
							| @ -1,46 +1,21 @@ | |||||||
| package logger | package logger | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"fmt" |  | ||||||
| 	"log" | 	"log" | ||||||
| 	"os" | 	"os" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
| 	"strings" |  | ||||||
| 
 | 
 | ||||||
| 	"github.com/rivo/tview" | 	"github.com/wtfutil/wtf/utils" | ||||||
| 	"github.com/wtfutil/wtf/wtf" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| const maxBufferSize int64 = 1024 |  | ||||||
| 
 |  | ||||||
| type Widget struct { |  | ||||||
| 	wtf.TextWidget |  | ||||||
| 
 |  | ||||||
| 	app      *tview.Application |  | ||||||
| 	filePath string |  | ||||||
| 	settings *Settings |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| func NewWidget(app *tview.Application, settings *Settings) *Widget { |  | ||||||
| 	widget := Widget{ |  | ||||||
| 		TextWidget: wtf.NewTextWidget(app, settings.common, true), |  | ||||||
| 
 |  | ||||||
| 		app:      app, |  | ||||||
| 		filePath: logFilePath(), |  | ||||||
| 		settings: settings, |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	return &widget |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| /* -------------------- Exported Functions -------------------- */ | /* -------------------- Exported Functions -------------------- */ | ||||||
| 
 | 
 | ||||||
| func Log(msg string) { | func Log(msg string) { | ||||||
| 	if logFileMissing() { | 	if LogFileMissing() { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	f, err := os.OpenFile(logFilePath(), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600) | 	f, err := os.OpenFile(LogFilePath(), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		log.Fatalf("error opening file: %v", err) | 		log.Fatalf("error opening file: %v", err) | ||||||
| 	} | 	} | ||||||
| @ -50,87 +25,15 @@ func Log(msg string) { | |||||||
| 	log.Println(msg) | 	log.Println(msg) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Refresh updates the onscreen contents of the widget | func LogFileMissing() bool { | ||||||
| func (widget *Widget) Refresh() { | 	return LogFilePath() == "" | ||||||
| 	if logFileMissing() { |  | ||||||
| 		return |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 	logLines := widget.tailFile() | func LogFilePath() string { | ||||||
| 
 | 	dir, err := utils.Home() | ||||||
| 	widget.app.QueueUpdateDraw(func() { |  | ||||||
| 		widget.View.SetTitle(widget.CommonSettings.Title) |  | ||||||
| 		widget.View.SetText(widget.contentFrom(logLines)) |  | ||||||
| 	}) |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| /* -------------------- Unexported Functions -------------------- */ |  | ||||||
| 
 |  | ||||||
| func (widget *Widget) contentFrom(logLines []string) string { |  | ||||||
| 	str := "" |  | ||||||
| 
 |  | ||||||
| 	for _, line := range logLines { |  | ||||||
| 		chunks := strings.Split(line, " ") |  | ||||||
| 
 |  | ||||||
| 		if len(chunks) >= 4 { |  | ||||||
| 			str = str + fmt.Sprintf( |  | ||||||
| 				"[green]%s[white] [yellow]%s[white] %s\n", |  | ||||||
| 				chunks[0], |  | ||||||
| 				chunks[1], |  | ||||||
| 				strings.Join(chunks[3:], " "), |  | ||||||
| 			) |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	return str |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| func logFileMissing() bool { |  | ||||||
| 	return logFilePath() == "" |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| func logFilePath() string { |  | ||||||
| 	dir, err := wtf.Home() |  | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return "" | 		return "" | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	return filepath.Join(dir, ".config", "wtf", "log.txt") | 	return filepath.Join(dir, ".config", "wtf", "log.txt") | ||||||
| } | } | ||||||
| 
 |  | ||||||
| func (widget *Widget) tailFile() []string { |  | ||||||
| 	file, err := os.Open(widget.filePath) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return []string{} |  | ||||||
| 	} |  | ||||||
| 	defer file.Close() |  | ||||||
| 
 |  | ||||||
| 	stat, err := file.Stat() |  | ||||||
| 	if err != nil { |  | ||||||
| 		return []string{} |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	bufferSize := maxBufferSize |  | ||||||
| 	if maxBufferSize > stat.Size() { |  | ||||||
| 		bufferSize = stat.Size() |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	startPos := stat.Size() - bufferSize |  | ||||||
| 
 |  | ||||||
| 	buff := make([]byte, bufferSize) |  | ||||||
| 	_, err = file.ReadAt(buff, startPos) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return []string{} |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	logLines := strings.Split(string(buff), "\n") |  | ||||||
| 
 |  | ||||||
| 	// Reverse the array of lines |  | ||||||
| 	// Offset by two to account for the blank line at the end |  | ||||||
| 	last := len(logLines) - 2 |  | ||||||
| 	for i := 0; i < len(logLines)/2; i++ { |  | ||||||
| 		logLines[i], logLines[last-i] = logLines[last-i], logLines[i] |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	return logLines |  | ||||||
| } |  | ||||||
|  | |||||||
							
								
								
									
										3
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								main.go
									
									
									
									
									
								
							| @ -18,6 +18,7 @@ import ( | |||||||
| 	"github.com/wtfutil/wtf/cfg" | 	"github.com/wtfutil/wtf/cfg" | ||||||
| 	"github.com/wtfutil/wtf/flags" | 	"github.com/wtfutil/wtf/flags" | ||||||
| 	"github.com/wtfutil/wtf/maker" | 	"github.com/wtfutil/wtf/maker" | ||||||
|  | 	"github.com/wtfutil/wtf/utils" | ||||||
| 	"github.com/wtfutil/wtf/wtf" | 	"github.com/wtfutil/wtf/wtf" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| @ -65,7 +66,7 @@ func refreshAllWidgets(widgets []wtf.Wtfable) { | |||||||
| 
 | 
 | ||||||
| func watchForConfigChanges(app *tview.Application, configFilePath string, grid *tview.Grid, pages *tview.Pages) { | func watchForConfigChanges(app *tview.Application, configFilePath string, grid *tview.Grid, pages *tview.Pages) { | ||||||
| 	watch := watcher.New() | 	watch := watcher.New() | ||||||
| 	absPath, _ := wtf.ExpandHomeDir(configFilePath) | 	absPath, _ := utils.ExpandHomeDir(configFilePath) | ||||||
| 
 | 
 | ||||||
| 	// Notify write events | 	// Notify write events | ||||||
| 	watch.FilterOps(watcher.Write) | 	watch.FilterOps(watcher.Write) | ||||||
|  | |||||||
| @ -3,7 +3,6 @@ package maker | |||||||
| import ( | import ( | ||||||
| 	"github.com/olebedev/config" | 	"github.com/olebedev/config" | ||||||
| 	"github.com/rivo/tview" | 	"github.com/rivo/tview" | ||||||
| 	"github.com/wtfutil/wtf/logger" |  | ||||||
| 	"github.com/wtfutil/wtf/modules/bamboohr" | 	"github.com/wtfutil/wtf/modules/bamboohr" | ||||||
| 	"github.com/wtfutil/wtf/modules/bargraph" | 	"github.com/wtfutil/wtf/modules/bargraph" | ||||||
| 	"github.com/wtfutil/wtf/modules/circleci" | 	"github.com/wtfutil/wtf/modules/circleci" | ||||||
| @ -25,6 +24,7 @@ import ( | |||||||
| 	"github.com/wtfutil/wtf/modules/ipaddresses/ipinfo" | 	"github.com/wtfutil/wtf/modules/ipaddresses/ipinfo" | ||||||
| 	"github.com/wtfutil/wtf/modules/jenkins" | 	"github.com/wtfutil/wtf/modules/jenkins" | ||||||
| 	"github.com/wtfutil/wtf/modules/jira" | 	"github.com/wtfutil/wtf/modules/jira" | ||||||
|  | 	"github.com/wtfutil/wtf/modules/logger" | ||||||
| 	"github.com/wtfutil/wtf/modules/mercurial" | 	"github.com/wtfutil/wtf/modules/mercurial" | ||||||
| 	"github.com/wtfutil/wtf/modules/nbascore" | 	"github.com/wtfutil/wtf/modules/nbascore" | ||||||
| 	"github.com/wtfutil/wtf/modules/newrelic" | 	"github.com/wtfutil/wtf/modules/newrelic" | ||||||
|  | |||||||
| @ -19,7 +19,7 @@ import ( | |||||||
| 	"time" | 	"time" | ||||||
| 
 | 
 | ||||||
| 	"github.com/wtfutil/wtf/cfg" | 	"github.com/wtfutil/wtf/cfg" | ||||||
| 	"github.com/wtfutil/wtf/wtf" | 	"github.com/wtfutil/wtf/utils" | ||||||
| 	"golang.org/x/oauth2" | 	"golang.org/x/oauth2" | ||||||
| 	"golang.org/x/oauth2/google" | 	"golang.org/x/oauth2/google" | ||||||
| 	"google.golang.org/api/calendar/v3" | 	"google.golang.org/api/calendar/v3" | ||||||
| @ -30,7 +30,7 @@ import ( | |||||||
| func (widget *Widget) Fetch() ([]*CalEvent, error) { | func (widget *Widget) Fetch() ([]*CalEvent, error) { | ||||||
| 	ctx := context.Background() | 	ctx := context.Background() | ||||||
| 
 | 
 | ||||||
| 	secretPath, _ := wtf.ExpandHomeDir(widget.settings.secretFile) | 	secretPath, _ := utils.ExpandHomeDir(widget.settings.secretFile) | ||||||
| 
 | 
 | ||||||
| 	b, err := ioutil.ReadFile(secretPath) | 	b, err := ioutil.ReadFile(secretPath) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| @ -123,7 +123,7 @@ func isAuthenticated() bool { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (widget *Widget) authenticate() { | func (widget *Widget) authenticate() { | ||||||
| 	secretPath, _ := wtf.ExpandHomeDir(widget.settings.secretFile) | 	secretPath, _ := utils.ExpandHomeDir(widget.settings.secretFile) | ||||||
| 
 | 
 | ||||||
| 	b, err := ioutil.ReadFile(secretPath) | 	b, err := ioutil.ReadFile(secretPath) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | |||||||
| @ -18,6 +18,7 @@ import ( | |||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
| 	"strings" | 	"strings" | ||||||
| 
 | 
 | ||||||
|  | 	"github.com/wtfutil/wtf/utils" | ||||||
| 	"github.com/wtfutil/wtf/wtf" | 	"github.com/wtfutil/wtf/wtf" | ||||||
| 	"golang.org/x/oauth2" | 	"golang.org/x/oauth2" | ||||||
| 	"golang.org/x/oauth2/google" | 	"golang.org/x/oauth2/google" | ||||||
| @ -29,7 +30,7 @@ import ( | |||||||
| func (widget *Widget) Fetch() ([]*sheets.ValueRange, error) { | func (widget *Widget) Fetch() ([]*sheets.ValueRange, error) { | ||||||
| 	ctx := context.Background() | 	ctx := context.Background() | ||||||
| 
 | 
 | ||||||
| 	secretPath, _ := wtf.ExpandHomeDir(widget.settings.secretFile) | 	secretPath, _ := utils.ExpandHomeDir(widget.settings.secretFile) | ||||||
| 
 | 
 | ||||||
| 	b, err := ioutil.ReadFile(secretPath) | 	b, err := ioutil.ReadFile(secretPath) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | |||||||
							
								
								
									
										105
									
								
								modules/logger/widget.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										105
									
								
								modules/logger/widget.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,105 @@ | |||||||
|  | package logger | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 	"os" | ||||||
|  | 	"strings" | ||||||
|  | 
 | ||||||
|  | 	"github.com/rivo/tview" | ||||||
|  | 	log "github.com/wtfutil/wtf/logger" | ||||||
|  | 	"github.com/wtfutil/wtf/wtf" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | const maxBufferSize int64 = 1024 | ||||||
|  | 
 | ||||||
|  | type Widget struct { | ||||||
|  | 	wtf.TextWidget | ||||||
|  | 
 | ||||||
|  | 	app      *tview.Application | ||||||
|  | 	filePath string | ||||||
|  | 	settings *Settings | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func NewWidget(app *tview.Application, settings *Settings) *Widget { | ||||||
|  | 	widget := Widget{ | ||||||
|  | 		TextWidget: wtf.NewTextWidget(app, settings.common, true), | ||||||
|  | 
 | ||||||
|  | 		app:      app, | ||||||
|  | 		filePath: log.LogFilePath(), | ||||||
|  | 		settings: settings, | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return &widget | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Refresh updates the onscreen contents of the widget | ||||||
|  | func (widget *Widget) Refresh() { | ||||||
|  | 	if log.LogFileMissing() { | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	logLines := widget.tailFile() | ||||||
|  | 
 | ||||||
|  | 	widget.app.QueueUpdateDraw(func() { | ||||||
|  | 		widget.View.SetTitle(widget.CommonSettings.Title) | ||||||
|  | 		widget.View.SetText(widget.contentFrom(logLines)) | ||||||
|  | 	}) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | /* -------------------- Unexported Functions -------------------- */ | ||||||
|  | 
 | ||||||
|  | func (widget *Widget) contentFrom(logLines []string) string { | ||||||
|  | 	str := "" | ||||||
|  | 
 | ||||||
|  | 	for _, line := range logLines { | ||||||
|  | 		chunks := strings.Split(line, " ") | ||||||
|  | 
 | ||||||
|  | 		if len(chunks) >= 4 { | ||||||
|  | 			str = str + fmt.Sprintf( | ||||||
|  | 				"[green]%s[white] [yellow]%s[white] %s\n", | ||||||
|  | 				chunks[0], | ||||||
|  | 				chunks[1], | ||||||
|  | 				strings.Join(chunks[3:], " "), | ||||||
|  | 			) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return str | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (widget *Widget) tailFile() []string { | ||||||
|  | 	file, err := os.Open(widget.filePath) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return []string{} | ||||||
|  | 	} | ||||||
|  | 	defer file.Close() | ||||||
|  | 
 | ||||||
|  | 	stat, err := file.Stat() | ||||||
|  | 	if err != nil { | ||||||
|  | 		return []string{} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	bufferSize := maxBufferSize | ||||||
|  | 	if maxBufferSize > stat.Size() { | ||||||
|  | 		bufferSize = stat.Size() | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	startPos := stat.Size() - bufferSize | ||||||
|  | 
 | ||||||
|  | 	buff := make([]byte, bufferSize) | ||||||
|  | 	_, err = file.ReadAt(buff, startPos) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return []string{} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	logLines := strings.Split(string(buff), "\n") | ||||||
|  | 
 | ||||||
|  | 	// Reverse the array of lines | ||||||
|  | 	// Offset by two to account for the blank line at the end | ||||||
|  | 	last := len(logLines) - 2 | ||||||
|  | 	for i := 0; i < len(logLines)/2; i++ { | ||||||
|  | 		logLines[i], logLines[last-i] = logLines[last-i], logLines[i] | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return logLines | ||||||
|  | } | ||||||
| @ -14,6 +14,7 @@ import ( | |||||||
| 	"github.com/alecthomas/chroma/styles" | 	"github.com/alecthomas/chroma/styles" | ||||||
| 	"github.com/radovskyb/watcher" | 	"github.com/radovskyb/watcher" | ||||||
| 	"github.com/rivo/tview" | 	"github.com/rivo/tview" | ||||||
|  | 	"github.com/wtfutil/wtf/utils" | ||||||
| 	"github.com/wtfutil/wtf/wtf" | 	"github.com/wtfutil/wtf/wtf" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| @ -98,7 +99,7 @@ func (widget *Widget) fileName() string { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (widget *Widget) formattedText() string { | func (widget *Widget) formattedText() string { | ||||||
| 	filePath, _ := wtf.ExpandHomeDir(widget.CurrentSource()) | 	filePath, _ := utils.ExpandHomeDir(widget.CurrentSource()) | ||||||
| 
 | 
 | ||||||
| 	file, err := os.Open(filePath) | 	file, err := os.Open(filePath) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| @ -129,7 +130,7 @@ func (widget *Widget) formattedText() string { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (widget *Widget) plainText() string { | func (widget *Widget) plainText() string { | ||||||
| 	filePath, _ := wtf.ExpandHomeDir(widget.CurrentSource()) | 	filePath, _ := utils.ExpandHomeDir(widget.CurrentSource()) | ||||||
| 
 | 
 | ||||||
| 	fmt.Println(filePath) | 	fmt.Println(filePath) | ||||||
| 
 | 
 | ||||||
| @ -159,7 +160,7 @@ func (widget *Widget) watchForFileChanges() { | |||||||
| 
 | 
 | ||||||
| 	// Watch each textfile for changes | 	// Watch each textfile for changes | ||||||
| 	for _, source := range widget.Sources { | 	for _, source := range widget.Sources { | ||||||
| 		fullPath, err := wtf.ExpandHomeDir(source) | 		fullPath, err := utils.ExpandHomeDir(source) | ||||||
| 		if err == nil { | 		if err == nil { | ||||||
| 			if err := watch.Add(fullPath); err != nil { | 			if err := watch.Add(fullPath); err != nil { | ||||||
| 				log.Fatalln(err) | 				log.Fatalln(err) | ||||||
|  | |||||||
| @ -2,7 +2,7 @@ | |||||||
| 
 | 
 | ||||||
| // Copied (mostly) verbatim from https://github.com/Atrox/homedir | // Copied (mostly) verbatim from https://github.com/Atrox/homedir | ||||||
| 
 | 
 | ||||||
| package wtf | package utils | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"errors" | 	"errors" | ||||||
| @ -7,6 +7,8 @@ import ( | |||||||
| 	"regexp" | 	"regexp" | ||||||
| 	"runtime" | 	"runtime" | ||||||
| 	"strings" | 	"strings" | ||||||
|  | 
 | ||||||
|  | 	"github.com/wtfutil/wtf/utils" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| const SimpleDateFormat = "Jan 2" | const SimpleDateFormat = "Jan 2" | ||||||
| @ -92,7 +94,7 @@ func OpenFile(path string) { | |||||||
| 		default: | 		default: | ||||||
| 		} | 		} | ||||||
| 	} else { | 	} else { | ||||||
| 		filePath, _ := ExpandHomeDir(path) | 		filePath, _ := utils.ExpandHomeDir(path) | ||||||
| 		cmd := exec.Command(OpenFileUtil, filePath) | 		cmd := exec.Command(OpenFileUtil, filePath) | ||||||
| 		ExecuteCommand(cmd) | 		ExecuteCommand(cmd) | ||||||
| 	} | 	} | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user