1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Textfile module now supports multiple text files

This commit is contained in:
Chris Cummer 2018-08-30 07:52:59 -07:00
parent ed75f9648a
commit 0e797fea3d
12 changed files with 168 additions and 48 deletions

View File

@ -28,7 +28,9 @@ wtf/textfile/
```yaml
textfile:
enabled: true
filePath: "~/Desktop/notes.md"
filePaths:
- "~/Desktop/notes.md"
- "~/.config/wtf/config.yml"
format: true
formatStyle: "dracula"
position:
@ -45,8 +47,8 @@ textfile:
Determines whether or not this module is executed and if its data displayed onscreen. <br />
Values: `true`, `false`.
`filePath` <br />
The path to the file to be displayed in the widget. <br />
`filePaths` <br />
An array of paths to the files to be displayed in the widget. <br />
`format` <br />
Whether or not to try and format and syntax highlight the displayedtext. <br />

2
docs/index.json vendored
View File

@ -249,7 +249,7 @@
"title": "Textfile",
"tags": [],
"description": "",
"content": " Displays the contents of the specified text file in the widget.\nSource Code wtf/textfile/ Keyboard Commands Key: / Action: Open/close the widget\u0026rsquo;s help window.\nKey: o Action: Opens the text file in whichever text editor is associated with that file type.\nConfiguration textfile:enabled:truefilePath:\u0026#34;~/Desktop/notes.md\u0026#34;format:trueformatStyle:\u0026#34;dracula\u0026#34;position:top:5left:4height:2width:1refreshInterval:15 Attributes enabled Determines whether or not this module is executed and if its data displayed onscreen. Values: true, false.\nfilePath The path to the file to be displayed in the widget. format Whether or not to try and format and syntax highlight the displayedtext. Values: true, false. Default: false.\nformatStyle The style of syntax highlighting to format the text with. Values: See Chroma styles for all valid options. Default: vim.\nposition Defines where in the grid this module\u0026rsquo;s widget will be displayed. refreshInterval How often, in seconds, this module will update its data. Values: A positive integer, 0..n.\n"
"content": " Displays the contents of the specified text file in the widget.\nSource Code wtf/textfile/ Keyboard Commands Key: / Action: Open/close the widget\u0026rsquo;s help window.\nKey: o Action: Opens the text file in whichever text editor is associated with that file type.\nConfiguration textfile:enabled:truefilePaths:-\u0026#34;~/Desktop/notes.md\u0026#34;-\u0026#34;~/.config/wtf/config.yml\u0026#34;format:trueformatStyle:\u0026#34;dracula\u0026#34;position:top:5left:4height:2width:1refreshInterval:15 Attributes enabled Determines whether or not this module is executed and if its data displayed onscreen. Values: true, false.\nfilePaths An array of paths to the files to be displayed in the widget. format Whether or not to try and format and syntax highlight the displayedtext. Values: true, false. Default: false.\nformatStyle The style of syntax highlighting to format the text with. Values: See Chroma styles for all valid options. Default: vim.\nposition Defines where in the grid this module\u0026rsquo;s widget will be displayed. refreshInterval How often, in seconds, this module will update its data. Values: A positive integer, 0..n.\n"
},
{
"uri": "https://wtfutil.com/modules/todo/",

4
docs/index.xml vendored
View File

@ -419,8 +419,8 @@ Wifi Network The name of the current network Whether or not the network uses enc
<description>Displays the contents of the specified text file in the widget.
Source Code wtf/textfile/ Keyboard Commands Key: / Action: Open/close the widget&amp;rsquo;s help window.
Key: o Action: Opens the text file in whichever text editor is associated with that file type.
Configuration textfile:enabled:truefilePath:&amp;#34;~/Desktop/notes.md&amp;#34;format:trueformatStyle:&amp;#34;dracula&amp;#34;position:top:5left:4height:2width:1refreshInterval:15 Attributes enabled Determines whether or not this module is executed and if its data displayed onscreen. Values: true, false.
filePath The path to the file to be displayed in the widget.</description>
Configuration textfile:enabled:truefilePaths:-&amp;#34;~/Desktop/notes.md&amp;#34;-&amp;#34;~/.config/wtf/config.yml&amp;#34;format:trueformatStyle:&amp;#34;dracula&amp;#34;position:top:5left:4height:2width:1refreshInterval:15 Attributes enabled Determines whether or not this module is executed and if its data displayed onscreen. Values: true, false.
filePaths An array of paths to the files to be displayed in the widget.</description>
</item>
<item>

View File

@ -411,13 +411,25 @@
<option value="/modules/circleci/" >- CircleCI</option>
<option value="/modules/clocks/" >- Clocks</option>
<option value="/modules/cmdrunner/" >- CmdRunner</option>
<option value="/modules/cryptocurrencies/" >
-
Crypo Currencies</option>
<option value="/modules/datadog/" >- Datadog</option>
<option value="/modules/gerrit/" >- Gerrit</option>
<option value="/modules/git/" >- Git</option>
<option value="/modules/github/" >- GitHub</option>
<option value="/modules/gitlab/" >- GitLab</option>
<option value="/modules/gitter/" >- Gitter</option>
<option value="/modules/google/" >
-
Google Apps</option>
<option value="/modules/google/gcal/" >-- Google Calendar</option>
<option value="/modules/google/gspreadsheet/" selected>-- Google Spreadsheets</option>
<option value="/modules/hackernews/" >- Hacker News</option>
<option value="/modules/ipaddress/" >
-
IP Addresses</option>
<option value="/modules/jenkins/" >- Jenkins</option>
<option value="/modules/jira/" >- Jira</option>
<option value="/modules/logger/" >- Logger</option>
@ -431,6 +443,9 @@
<option value="/modules/travisci/" >- TravisCI</option>
<option value="/modules/trello/" >- Trello</option>
<option value="/modules/twitter/" >- Twitter</option>
<option value="/modules/weather_services/" >
-
Weather Services</option>
<option value="/modules/zendesk/" >- Zendesk</option>

View File

@ -269,8 +269,8 @@ Wifi Network The name of the current network Whether or not the network uses enc
<description>Displays the contents of the specified text file in the widget.
Source Code wtf/textfile/ Keyboard Commands Key: / Action: Open/close the widget&amp;rsquo;s help window.
Key: o Action: Opens the text file in whichever text editor is associated with that file type.
Configuration textfile:enabled:truefilePath:&amp;#34;~/Desktop/notes.md&amp;#34;format:trueformatStyle:&amp;#34;dracula&amp;#34;position:top:5left:4height:2width:1refreshInterval:15 Attributes enabled Determines whether or not this module is executed and if its data displayed onscreen. Values: true, false.
filePath The path to the file to be displayed in the widget.</description>
Configuration textfile:enabled:truefilePaths:-&amp;#34;~/Desktop/notes.md&amp;#34;-&amp;#34;~/.config/wtf/config.yml&amp;#34;format:trueformatStyle:&amp;#34;dracula&amp;#34;position:top:5left:4height:2width:1refreshInterval:15 Attributes enabled Determines whether or not this module is executed and if its data displayed onscreen. Values: true, false.
filePaths An array of paths to the files to be displayed in the widget.</description>
</item>
<item>

View File

@ -489,7 +489,9 @@
<h2 id="configuration">Configuration</h2>
<div class="highlight"><pre class="chroma"><code class="language-yaml" data-lang="yaml">textfile<span class="p">:</span><span class="w">
</span><span class="w"> </span>enabled<span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
</span><span class="w"> </span>filePath<span class="p">:</span><span class="w"> </span><span class="s2">&#34;~/Desktop/notes.md&#34;</span><span class="w">
</span><span class="w"> </span>filePaths<span class="p">:</span><span class="w">
</span><span class="w"> </span>-<span class="w"> </span><span class="s2">&#34;~/Desktop/notes.md&#34;</span><span class="w">
</span><span class="w"> </span>-<span class="w"> </span><span class="s2">&#34;~/.config/wtf/config.yml&#34;</span><span class="w">
</span><span class="w"> </span>format<span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
</span><span class="w"> </span>formatStyle<span class="p">:</span><span class="w"> </span><span class="s2">&#34;dracula&#34;</span><span class="w">
</span><span class="w"> </span>position<span class="p">:</span><span class="w">
@ -504,8 +506,8 @@
Determines whether or not this module is executed and if its data displayed onscreen. <br />
Values: <code>true</code>, <code>false</code>.</p>
<p><code>filePath</code> <br />
The path to the file to be displayed in the widget. <br /></p>
<p><code>filePaths</code> <br />
An array of paths to the files to be displayed in the widget. <br /></p>
<p><code>format</code> <br />
Whether or not to try and format and syntax highlight the displayedtext. <br />

View File

@ -410,13 +410,22 @@
<option value="/modules/circleci/" >- CircleCI</option>
<option value="/modules/clocks/" >- Clocks</option>
<option value="/modules/cmdrunner/" >- CmdRunner</option>
<option value="/modules/cryptocurrencies/" >
-
Crypo Currencies</option>
<option value="/modules/datadog/" >- Datadog</option>
<option value="/modules/gerrit/" >- Gerrit</option>
<option value="/modules/git/" >- Git</option>
<option value="/modules/github/" >- GitHub</option>
<option value="/modules/gitlab/" >- GitLab</option>
<option value="/modules/gitter/" >- Gitter</option>
<option value="/modules/google/" >
-
Google Apps</option>
<option value="/modules/hackernews/" >- Hacker News</option>
<option value="/modules/ipaddress/" >
-
IP Addresses</option>
<option value="/modules/jenkins/" >- Jenkins</option>
<option value="/modules/jira/" >- Jira</option>
<option value="/modules/logger/" >- Logger</option>
@ -430,6 +439,9 @@
<option value="/modules/travisci/" >- TravisCI</option>
<option value="/modules/trello/" >- Trello</option>
<option value="/modules/twitter/" >- Twitter</option>
<option value="/modules/weather_services/" >
-
Weather Services</option>
<option value="/modules/zendesk/" >- Zendesk</option>

View File

@ -411,13 +411,22 @@
<option value="/modules/circleci/" >- CircleCI</option>
<option value="/modules/clocks/" >- Clocks</option>
<option value="/modules/cmdrunner/" >- CmdRunner</option>
<option value="/modules/cryptocurrencies/" >
-
Crypo Currencies</option>
<option value="/modules/datadog/" >- Datadog</option>
<option value="/modules/gerrit/" >- Gerrit</option>
<option value="/modules/git/" >- Git</option>
<option value="/modules/github/" >- GitHub</option>
<option value="/modules/gitlab/" >- GitLab</option>
<option value="/modules/gitter/" >- Gitter</option>
<option value="/modules/google/" >
-
Google Apps</option>
<option value="/modules/hackernews/" >- Hacker News</option>
<option value="/modules/ipaddress/" >
-
IP Addresses</option>
<option value="/modules/jenkins/" >- Jenkins</option>
<option value="/modules/jira/" >- Jira</option>
<option value="/modules/logger/" >- Logger</option>
@ -431,6 +440,12 @@
<option value="/modules/travisci/" >- TravisCI</option>
<option value="/modules/trello/" >- Trello</option>
<option value="/modules/twitter/" >- Twitter</option>
<option value="/modules/weather_services/" >
-
Weather Services</option>
<option value="/modules/weather_services/prettyweather/" >-- Pretty Weather</option>
<option value="/modules/weather_services/weather/" selected>-- Weather</option>
<option value="/modules/zendesk/" >- Zendesk</option>

View File

@ -10,10 +10,10 @@ const HelpText = `
Keyboard commands for Git:
/: Show/hide this help window
c: Checkout to branch
h: Previous git repository
l: Next git repository
p: Pull current git repository
c: Checkout to branch
arrow left: Previous git repository
arrow right: Next git repository
@ -51,12 +51,21 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
repoPaths := wtf.ToStrs(wtf.Config.UList("wtf.mods.git.repositories"))
func (widget *Widget) Checkout() {
form := widget.modalForm("Branch to checkout:", "")
widget.UpdateRefreshedAt()
widget.Data = widget.gitRepos(repoPaths)
checkoutFctn := func() {
text := form.GetFormItem(0).(*tview.InputField).GetText()
repoToCheckout := widget.Data[widget.Idx]
repoToCheckout.checkout(text)
widget.pages.RemovePage("modal")
widget.app.SetFocus(widget.View)
widget.display()
widget.Refresh()
}
widget.addButtons(form, checkoutFctn)
widget.modalFocus(form)
}
func (widget *Widget) Next() {
@ -76,38 +85,33 @@ func (widget *Widget) Prev() {
widget.display()
}
func (widget *Widget) Pull() {
repoToPull := widget.Data[widget.Idx]
repoToPull.pull()
widget.Refresh()
}
func (widget *Widget) Checkout() {
form := widget.modalForm("Branch to checkout:", "")
checkoutFctn := func() {
text := form.GetFormItem(0).(*tview.InputField).GetText()
repoToCheckout := widget.Data[widget.Idx]
repoToCheckout.checkout(text)
widget.pages.RemovePage("modal")
widget.app.SetFocus(widget.View)
func (widget *Widget) Refresh() {
repoPaths := wtf.ToStrs(wtf.Config.UList("wtf.mods.git.repositories"))
widget.UpdateRefreshedAt()
widget.Data = widget.gitRepos(repoPaths)
widget.display()
widget.Refresh()
}
widget.addButtons(form, checkoutFctn)
widget.modalFocus(form)
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) addCheckoutButton(form *tview.Form, fctn func()) {
form.AddButton("Checkout", fctn)
}
func (widget *Widget) addButtons(form *tview.Form, checkoutFctn func()) {
widget.addCheckoutButton(form, checkoutFctn)
widget.addCancelButton(form)
}
func (widget *Widget) addCancelButton(form *tview.Form) {
cancelFn := func() {
widget.pages.RemovePage("modal")
@ -118,6 +122,7 @@ func (widget *Widget) addCancelButton(form *tview.Form) {
form.AddButton("Cancel", cancelFn)
form.SetCancelFunc(cancelFn)
}
func (widget *Widget) modalFocus(form *tview.Form) {
frame := widget.modalFrame(form)
widget.pages.AddPage("modal", frame, false, true)
@ -133,6 +138,7 @@ func (widget *Widget) modalForm(lbl, text string) *tview.Form {
return form
}
func (widget *Widget) modalFrame(form *tview.Form) *tview.Frame {
frame := tview.NewFrame(form).SetBorders(0, 0, 0, 0, 0, 0)
frame.SetRect(offscreen, offscreen, modalWidth, modalHeight)

View File

@ -18,14 +18,20 @@ const HelpText = `
Keyboard commands for Textfile:
/: Show/hide this help window
h: Previous text file
l: Next text file
o: Open the text file in the operating system
arrow left: Previous text file
arrow right: Next text file
`
type Widget struct {
wtf.HelpfulWidget
wtf.TextWidget
filePath string
filePaths []string
idx int
}
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
@ -33,9 +39,11 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
TextWidget: wtf.NewTextWidget("TextFile", "textfile", true),
filePath: wtf.Config.UString("wtf.mods.textfile.filePath"),
idx: 0,
}
widget.loadFilePaths()
widget.HelpfulWidget.SetView(widget.View)
widget.View.SetWrap(true)
@ -47,28 +55,56 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Next() {
widget.idx = widget.idx + 1
if widget.idx == len(widget.filePaths) {
widget.idx = 0
}
widget.display()
}
func (widget *Widget) Prev() {
widget.idx = widget.idx - 1
if widget.idx < 0 {
widget.idx = len(widget.filePaths) - 1
}
widget.display()
}
func (widget *Widget) Refresh() {
widget.UpdateRefreshedAt()
widget.display()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) currentFilePath() string {
return widget.filePaths[widget.idx]
}
func (widget *Widget) display() {
widget.View.SetTitle(widget.ContextualTitle(widget.fileName()))
var text string
text := wtf.SigilStr(len(widget.filePaths), widget.idx, widget.View) + "\n"
if wtf.Config.UBool("wtf.mods.textfile.format", false) {
text = widget.formattedText()
text = text + widget.formattedText()
} else {
text = widget.plainText()
text = text + widget.plainText()
}
widget.View.SetText(text)
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) fileName() string {
return filepath.Base(widget.filePath)
return filepath.Base(widget.currentFilePath())
}
func (widget *Widget) formattedText() string {
filePath, _ := wtf.ExpandHomeDir(widget.filePath)
filePath, _ := wtf.ExpandHomeDir(widget.currentFilePath())
file, err := os.Open(filePath)
if err != nil {
@ -98,8 +134,24 @@ func (widget *Widget) formattedText() string {
return tview.TranslateANSI(buf.String())
}
// loadFilePaths parses file paths from the config and stores them in an array
// It is backwards-compatible, supporting both the original, singular filePath and
// the current plural filePaths
func (widget *Widget) loadFilePaths() {
var emptyArray []interface{}
filePath := wtf.Config.UString("wtf.mods.textfile.filePath", "")
filePaths := wtf.ToStrs(wtf.Config.UList("wtf.mods.textfile.filePaths", emptyArray))
if filePath != "" {
filePaths = append(filePaths, filePath)
}
widget.filePaths = filePaths
}
func (widget *Widget) plainText() string {
filePath, _ := wtf.ExpandHomeDir(widget.filePath)
filePath, _ := wtf.ExpandHomeDir(widget.currentFilePath())
text, err := ioutil.ReadFile(filePath) // just pass the file name
if err != nil {
@ -113,9 +165,26 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
case "/":
widget.ShowHelp()
return nil
case "o":
wtf.OpenFile(widget.filePath)
case "h":
widget.Prev()
return nil
case "l":
widget.Next()
return nil
case "o":
wtf.OpenFile(widget.currentFilePath())
return nil
}
switch event.Key() {
case tcell.KeyLeft:
widget.Prev()
return nil
case tcell.KeyRight:
widget.Next()
return nil
default:
return event
}
return event

View File

@ -29,12 +29,10 @@ func NewBillboardModal(text string, closeFunc func()) *tview.Frame {
}
textView := tview.NewTextView()
textView.SetInputCapture(keyboardIntercept)
textView.SetWrap(true)
textView.SetText(text)
textView.SetBackgroundColor(tview.Styles.ContrastBackgroundColor)
textView.SetInputCapture(keyboardIntercept)
frame := tview.NewFrame(textView)
frame.SetRect(offscreen, offscreen, modalWidth, modalHeight)
@ -44,7 +42,6 @@ func NewBillboardModal(text string, closeFunc func()) *tview.Frame {
return x, y, width, height
}
frame.SetBackgroundColor(tview.Styles.ContrastBackgroundColor)
frame.SetBorder(true)
frame.SetBorders(1, 1, 0, 0, 1, 1)
frame.SetDrawFunc(drawFunc)

View File

@ -156,6 +156,7 @@ func SigilStr(len, pos int, view *tview.TextView) string {
func ToInts(slice []interface{}) []int {
results := []int{}
for _, val := range slice {
results = append(results, val.(int))
}
@ -165,6 +166,7 @@ func ToInts(slice []interface{}) []int {
func ToStrs(slice []interface{}) []string {
results := []string{}
for _, val := range slice {
results = append(results, val.(string))
}