caret

[ACTIVE] a command line tool for browsing Lobsters in your terminal
git clone git://git.figbert.com/caret.git
Log | Files | Refs | README | LICENSE

cmds.go (1614B)


      1 package main
      2 
      3 import (
      4 	"os/exec"
      5 	"runtime"
      6 	"time"
      7 
      8 	"git.figbert.com/caret/api"
      9 	"git.figbert.com/caret/article"
     10 	"git.figbert.com/caret/utils"
     11 
     12 	"github.com/charmbracelet/bubbles/list"
     13 	"github.com/charmbracelet/bubbletea"
     14 )
     15 
     16 type returnedFromReading struct{}
     17 type platformUnsupported struct{}
     18 type msgExpired struct{}
     19 type loading struct{}
     20 
     21 func attemptToOpenPostURL(itm list.Item) tea.Cmd {
     22 	return func() tea.Msg {
     23 		i := itm.(item)
     24 
     25 		if i.post.URL == "" {
     26 			return utils.NoExternalURL{}
     27 		}
     28 
     29 		success := openURL(i.post.URL)
     30 		if !success {
     31 			return platformUnsupported{}
     32 		}
     33 
     34 		return nil
     35 	}
     36 }
     37 
     38 func attemptToOpenPostComments(itm list.Item) tea.Cmd {
     39 	return func() tea.Msg {
     40 		i := itm.(item)
     41 
     42 		success := openURL(i.post.CommentsURL)
     43 		if !success {
     44 			return platformUnsupported{}
     45 		}
     46 
     47 		return nil
     48 	}
     49 }
     50 
     51 func openURL(url string) bool {
     52 	switch runtime.GOOS {
     53 	case "darwin":
     54 		cmd := exec.Command("open", url)
     55 		_ = cmd.Start()
     56 	case "linux":
     57 		cmd := exec.Command("xdg-open", url)
     58 		_ = cmd.Start()
     59 	case "windows":
     60 		cmd := exec.Command("rundll32", "url.dll,FileProtocolHandler", url)
     61 		_ = cmd.Start()
     62 	default:
     63 		return false
     64 	}
     65 	return true
     66 }
     67 
     68 func clearMsg() tea.Msg {
     69 	time.Sleep(time.Second * 5)
     70 	return msgExpired{}
     71 }
     72 
     73 func startLoading() tea.Msg {
     74 	return loading{}
     75 }
     76 
     77 func displayLinkedArticle(i list.Item) tea.Cmd {
     78 	return article.LoadLinkedArticle(i.(item).post.URL)
     79 }
     80 
     81 func fetchComments(i list.Item) tea.Cmd {
     82 	return api.FetchPost(i.(item).post.ShortID)
     83 }
     84 
     85 func run(cmd *exec.Cmd) tea.Cmd {
     86 	return tea.ExecProcess(cmd, func(_ error) tea.Msg {
     87 		return returnedFromReading{}
     88 	})
     89 }