summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-11-01 11:31:40 +0200
committerPaul Buetow <paul@buetow.org>2024-11-01 11:31:40 +0200
commit8e5def8c2e3e4ff347b1f60eb5f6007dae1bbd0a (patch)
tree29c97b7f340811cdb7e9e2d63a8554d66cdbdc17
parent9581ebeb48c48425f2f8f5160455f26dcdc0c550 (diff)
add the .soon tag, and some refactor
-rw-r--r--README.md2
-rw-r--r--internal/oi/oi.go8
-rw-r--r--internal/prompt/prompt.go16
-rw-r--r--internal/schedule/schedule.go54
4 files changed, 46 insertions, 34 deletions
diff --git a/README.md b/README.md
index be4f87d..55d9815 100644
--- a/README.md
+++ b/README.md
@@ -148,6 +148,8 @@ Normally, Gos randomly picks any queued message without any specific order or pr
* To explicitly share on both: `~/.gosdir/foopost.prio.share:linkedin:mastodon.txt`
* To explicitly share on only linkedin: `~/.gosdir/foopost.prio.share:linkedin:-mastodon.txt`
+There is more: you can also use the `soon` tag. It is almost the same as the `prio` tag, just with one lower priority.
+
### More tags
* A `.ask.` in the filename will prompt you to choose whether to queue, edit, or delete a file before queuing it.
diff --git a/internal/oi/oi.go b/internal/oi/oi.go
index 2aa4417..b2a0953 100644
--- a/internal/oi/oi.go
+++ b/internal/oi/oi.go
@@ -84,20 +84,20 @@ func ReadDir[T any](dir string, cb func(file os.DirEntry) (T, bool)) ([]T, error
return results, nil
}
-func ReadDirRandom[T any](dir string, cb func(file os.DirEntry) (T, bool)) (T, int, error) {
+func ReadDirRandom[T any](dir string, cb func(file os.DirEntry) (T, bool)) (T, error) {
results, err := ReadDir(dir, cb)
if err != nil {
var zero T
- return zero, 0, err
+ return zero, err
}
if len(results) == 0 {
var zero T
- return zero, 0, ErrNotFound
+ return zero, ErrNotFound
}
rand.Seed(uint64(time.Now().UnixNano()))
- return results[rand.Intn(len(results))], len(results), nil
+ return results[rand.Intn(len(results))], nil
}
func IsRegular(path string) bool {
diff --git a/internal/prompt/prompt.go b/internal/prompt/prompt.go
index ab09d4b..df0c256 100644
--- a/internal/prompt/prompt.go
+++ b/internal/prompt/prompt.go
@@ -11,14 +11,18 @@ import (
var (
Info1 = color.New(color.FgCyan, color.BgBlue, color.Bold).PrintfFunc()
Info2 = color.New(color.FgHiYellow, color.BgHiBlack, color.Bold).PrintfFunc()
- Ack = color.New(color.FgHiBlack, color.BgHiGreen, color.Bold).PrintfFunc()
- Warn = color.New(color.FgBlack, color.BgHiYellow, color.Bold).PrintfFunc()
+ INfo3 = color.New(color.FgHiBlack, color.BgHiGreen, color.Bold).PrintfFunc()
+ Ack = color.New(color.FgBlack, color.BgHiYellow, color.Bold).PrintfFunc()
)
-func Acknowledge(message, content string) error {
- Info1(content)
- fmt.Print("\n")
- Ack(message + " (press enter)")
+func Acknowledge(messages ...string) error {
+ if len(messages) > 1 {
+ for _, content := range messages[1:] {
+ Info1(content)
+ fmt.Print("\n")
+ }
+ }
+ Ack(messages[0] + " (press enter to acknowlege)")
reader := bufio.NewReader(os.Stdin)
if _, err := reader.ReadString('\n'); err != nil {
return err
diff --git a/internal/schedule/schedule.go b/internal/schedule/schedule.go
index b2c2702..ee3abc3 100644
--- a/internal/schedule/schedule.go
+++ b/internal/schedule/schedule.go
@@ -26,42 +26,48 @@ func Run(args config.Args, platform string) (entry.Entry, error) {
if err != nil {
return entry.Zero, err
}
+ if stats.queued < args.MinQueued {
+ _ = prompt.Acknowledge(
+ fmt.Sprintf("There are only %d messages queued for %s - time to fill it up!",
+ stats.queued, platform),
+ )
+ }
log.Println("For", platform, "stats:", stats)
- // Schedule random queued entry with "now" tag, ignoring the target hit stats.
- en, numQueued, err := selectRandomEntry(dir, "now")
+ en, err := selectEntry(dir)
if err != nil && !errors.Is(err, oi.ErrNotFound) {
- // Unknown error
- return en, nil
- }
- if err == nil {
- return en, nil
+ return en, nil // Unknown error
}
-
- if stats.targetHit(args.PauseDays) {
+ if !en.HasTag("now") && stats.targetHit(args.PauseDays) {
return entry.Zero, ErrNothingToSchedule
}
+ return en, nil
+}
+
+/**
+ * Select a random entry, but in this order:
+ * 1. Any antry with the now tag
+ * 2. Any entry with the prio tag
+ * 3. Any entry with the soon tag
+ * 4. Any other entry
+ */
+func selectEntry(dir string) (en entry.Entry, err error) {
+ tagsToTry := []string{"now", "prio", "soon", ""}
+ for _, tag := range tagsToTry {
+ if en, err = selectRandomEntry(dir, tag); err == nil {
+ return
+ }
+ if !errors.Is(err, oi.ErrNotFound) {
+ return
+ }
- // Schedule random qeued entry for platform. Find one with prio tag.
- en, _, err = selectRandomEntry(dir, "prio")
- if errors.Is(err, oi.ErrNotFound) {
- // No entry with priority tag found, select another one.
- en, numQueued, err = selectRandomEntry(dir, "")
- }
- if err != nil {
- return entry.Zero, fmt.Errorf("%w: %w", ErrNothingQueued, err)
- }
- // TODO: Fix this, it warns only when there are no now and prio tags used.
- if numQueued < args.MinQueued {
- prompt.Warn("Only %d items queued for %s, want to have %d", numQueued, platform, args.MinQueued)
- fmt.Print("\n")
}
- return en, nil
+ return
}
// Select a random queed entry with a given tag. If the tag is the empty string,
// then select any random qeued entry.
-func selectRandomEntry(dir, tag string) (entry.Entry, int, error) {
+func selectRandomEntry(dir, tag string) (entry.Entry, error) {
return oi.ReadDirRandom(dir, func(file os.DirEntry) (entry.Entry, bool) {
// Is there a ".TAG." in the file name?
if tag != "" && !slices.Contains(strings.Split(file.Name(), "."), tag) {