mirror of
				https://github.com/taigrr/wtf
				synced 2025-01-18 04:03:14 -08:00 
			
		
		
		
	Add basic textfile widget.
This commit is contained in:
		
							parent
							
								
									693a04b5e1
								
							
						
					
					
						commit
						c3abb76612
					
				
							
								
								
									
										64
									
								
								textfile/widget.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								textfile/widget.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,64 @@
 | 
			
		||||
package textfile
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/olebedev/config"
 | 
			
		||||
	"github.com/senorprogrammer/wtf/homedir"
 | 
			
		||||
	"github.com/senorprogrammer/wtf/wtf"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var Config *config.Config
 | 
			
		||||
 | 
			
		||||
type Widget struct {
 | 
			
		||||
	wtf.TextWidget
 | 
			
		||||
 | 
			
		||||
	FilePath string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewWidget() *Widget {
 | 
			
		||||
	widget := Widget{
 | 
			
		||||
		TextWidget: wtf.NewTextWidget("TextFile", "textfile"),
 | 
			
		||||
		FilePath:   Config.UString("wtf.mods.textfile.filepath"),
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	widget.View.SetWrap(true)
 | 
			
		||||
 | 
			
		||||
	return &widget
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* -------------------- Exported Functions -------------------- */
 | 
			
		||||
 | 
			
		||||
func (widget *Widget) Refresh() {
 | 
			
		||||
	if widget.Disabled() {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	widget.View.SetTitle(fmt.Sprintf(" %s ", widget.FilePath))
 | 
			
		||||
	widget.RefreshedAt = time.Now()
 | 
			
		||||
 | 
			
		||||
	widget.View.Clear()
 | 
			
		||||
 | 
			
		||||
	fileData, err := widget.readFile()
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Fprintf(widget.View, "%s", err)
 | 
			
		||||
	} else {
 | 
			
		||||
		fmt.Fprintf(widget.View, "%s", fileData)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* -------------------- Uneported Functions -------------------- */
 | 
			
		||||
 | 
			
		||||
func (widget *Widget) readFile() (string, error) {
 | 
			
		||||
	absPath, _ := homedir.Expand(widget.FilePath)
 | 
			
		||||
 | 
			
		||||
	bytes, err := ioutil.ReadFile(absPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return string(bytes), nil
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										3
									
								
								wtf.go
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								wtf.go
									
									
									
									
									
								
							@ -15,6 +15,7 @@ import (
 | 
			
		||||
	"github.com/senorprogrammer/wtf/opsgenie"
 | 
			
		||||
	"github.com/senorprogrammer/wtf/security"
 | 
			
		||||
	"github.com/senorprogrammer/wtf/status"
 | 
			
		||||
	"github.com/senorprogrammer/wtf/textfile"
 | 
			
		||||
	"github.com/senorprogrammer/wtf/weather"
 | 
			
		||||
	"github.com/senorprogrammer/wtf/wtf"
 | 
			
		||||
)
 | 
			
		||||
@ -98,6 +99,7 @@ func main() {
 | 
			
		||||
	opsgenie.Config = Config
 | 
			
		||||
	security.Config = Config
 | 
			
		||||
	status.Config = Config
 | 
			
		||||
	textfile.Config = Config
 | 
			
		||||
	weather.Config = Config
 | 
			
		||||
 | 
			
		||||
	Modules = []wtf.TextViewer{
 | 
			
		||||
@ -110,6 +112,7 @@ func main() {
 | 
			
		||||
		opsgenie.NewWidget(),
 | 
			
		||||
		security.NewWidget(),
 | 
			
		||||
		status.NewWidget(),
 | 
			
		||||
		textfile.NewWidget(),
 | 
			
		||||
		weather.NewWidget(),
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -23,9 +23,9 @@ func CreateConfigDir() bool {
 | 
			
		||||
 | 
			
		||||
// LoadConfigFile loads the config.yml file to configure the app
 | 
			
		||||
func LoadConfigFile() *config.Config {
 | 
			
		||||
	configPath, _ := homedir.Expand("~/.wtf/config.yml")
 | 
			
		||||
	absPath, _ := homedir.Expand("~/.wtf/config.yml")
 | 
			
		||||
 | 
			
		||||
	cfg, err := config.ParseYamlFile(configPath)
 | 
			
		||||
	cfg, err := config.ParseYamlFile(absPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println("\n\n\033[1m ERROR:\033[0m Could not load '\033[0;33mconfig.yml\033[0m'.\n Please add a \033[0;33mconfig.yml\033[0m file to your \033[0;33m~/.wtf\033[0m directory.\n See \033[1;34mhttps://github.com/senorprogrammer/wtf\033[0m for details.\n\n")
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user