Skip to content

Commit

Permalink
Fixed code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mrusme committed Apr 4, 2021
1 parent 8194c52 commit 3fe966d
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 38 deletions.
41 changes: 28 additions & 13 deletions cli/authenticateCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,28 @@ var authenticateCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
server := args[0]

mastodonApp, err := mastodon.RegisterApp(context.Background(), &mastodon.AppConfig{
Server: server,
ClientName: "Gomphotherium",
Scopes: "read write follow",
Website: "https://github.com/mrusme/gomphotherium",
})
mastodonApp, err := mastodon.RegisterApp(
context.Background(),
&mastodon.AppConfig{
Server: server,
ClientName: "Gomphotherium",
Scopes: "read write follow",
Website: "https://github.com/mrusme/gomphotherium",
},
)
if err != nil {
log.Fatal(err)
}

fmt.Printf("Please open the following URL to authenticate: %s\n\n", mastodonApp.AuthURI)
fmt.Printf("Please open the following URL to authenticate: %s\n\n",
mastodonApp.AuthURI)
fmt.Printf("Paste authentication code here and press enter: ")

reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
fmt.Println("An error occured while reading input. Please try again", err)
fmt.Println("An error occured while reading input. Please try again",
err)
os.Exit(-1)
}
authCode := strings.TrimSuffix(input, "\n")
Expand All @@ -47,20 +52,30 @@ var authenticateCmd = &cobra.Command{
ClientSecret: mastodonApp.ClientSecret,
})

err = mastodonClient.AuthenticateToken(context.Background(), authCode, "urn:ietf:wg:oauth:2.0:oob")
err = mastodonClient.AuthenticateToken(
context.Background(),
authCode,
"urn:ietf:wg:oauth:2.0:oob",
)
if err != nil {
log.Fatal(err)
os.Exit(-1)
}

fmt.Printf("Success!\nPlease either export the following variables to your session:\n\n")
fmt.Printf("Success!\n")
fmt.Printf(
"Please either export the following variables to your session:\n\n")

fmt.Printf("export GOMPHOTHERIUM_SERVER='%s'", server)
fmt.Printf("export GOMPHOTHERIUM_ACCESS_TOKEN='%s'", mastodonClient.Config.AccessToken)
fmt.Printf("export GOMPHOTHERIUM_SERVER='%s'",
server)
fmt.Printf("export GOMPHOTHERIUM_ACCESS_TOKEN='%s'",
mastodonClient.Config.AccessToken)

fmt.Printf("\n... or call gomphotherium with the following flags:\n\n")

fmt.Printf("gomphotherium --server '%s' --access-token '%s' ...\n\n", server, mastodonClient.Config.AccessToken)
fmt.Printf("gomphotherium --server '%s' --access-token '%s' ...\n\n",
server,
mastodonClient.Config.AccessToken)

return
},
Expand Down
22 changes: 19 additions & 3 deletions cli/rootCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,25 @@ func Execute(embeddedHelp string) {
func init() {
cobra.OnInitialize(initConfig)

rootCmd.PersistentFlags().StringVar(&server, "server", LookupStrEnv("GOMPHOTHERIUM_SERVER",""), "Mastodon server")
rootCmd.PersistentFlags().StringVar(&accessToken, "access-token", LookupStrEnv("GOMPHOTHERIUM_ACCESS_TOKEN",""), "Mastodon access token")
rootCmd.PersistentFlags().BoolVarP(&flagShowImages, "show-images", "i", true, "Show images in timeline")
rootCmd.PersistentFlags().StringVar(
&server,
"server",
LookupStrEnv("GOMPHOTHERIUM_SERVER",""),
"Mastodon server",
)
rootCmd.PersistentFlags().StringVar(
&accessToken,
"access-token",
LookupStrEnv("GOMPHOTHERIUM_ACCESS_TOKEN",""),
"Mastodon access token",
)
rootCmd.PersistentFlags().BoolVarP(
&flagShowImages,
"show-images",
"i",
true,
"Show images in timeline",
)
}

func initConfig() {
Expand Down
18 changes: 15 additions & 3 deletions mast/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const (
CodeHelp = -2
)

var CmdContentRegex = regexp.MustCompile(`(?m)(( {0,1}~#| {0,1}~:)\[([^\[\]]*)\]| {0,1}~!!)`)
var CmdContentRegex =
regexp.MustCompile(`(?m)(( {0,1}~#| {0,1}~:)\[([^\[\]]*)\]| {0,1}~!!)`)

func CmdAvailable() ([]string) {
return []string{
Expand Down Expand Up @@ -264,7 +265,11 @@ func CmdHelperGetShareParams(args string) (int, error) {
return CmdHelperGetTootIDFromString(args)
}

func CmdToot(timeline *Timeline, content string, inReplyTo int, visibility string) (CmdReturnCode) {
func CmdToot(
timeline *Timeline,
content string,
inReplyTo int,
visibility string) (CmdReturnCode) {
var status string = ""
var spoiler string = ""
var sensitive bool = false
Expand All @@ -290,7 +295,14 @@ func CmdToot(timeline *Timeline, content string, inReplyTo int, visibility strin

status = CmdContentRegex.ReplaceAllString(content, "")

_, err := timeline.Toot(&status, inReplyTo, filesToUpload, &visibility, sensitive, &spoiler)
_, err := timeline.Toot(
&status,
inReplyTo,
filesToUpload,
&visibility,
sensitive,
&spoiler,
)
if err != nil {
return CodeNotOk
}
Expand Down
37 changes: 27 additions & 10 deletions mast/timeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,17 @@ func (timeline *Timeline) Load() (error) {

switch timeline.timelineType {
case TimelineHome:
statuses, err = timeline.client.GetTimelineHome(context.Background(), nil)
statuses, err =
timeline.client.GetTimelineHome(context.Background(), nil)
case TimelineLocal:
statuses, err = timeline.client.GetTimelinePublic(context.Background(), true, nil)
statuses, err =
timeline.client.GetTimelinePublic(context.Background(), true, nil)
case TimelinePublic:
statuses, err = timeline.client.GetTimelinePublic(context.Background(), false, nil)
statuses, err =
timeline.client.GetTimelinePublic(context.Background(), false, nil)
case TimelineNotifications:
notifications, err := timeline.client.GetNotifications(context.Background(), nil)
notifications, err :=
timeline.client.GetNotifications(context.Background(), nil)
if err != nil {
return err
}
Expand All @@ -93,7 +97,8 @@ func (timeline *Timeline) Load() (error) {
_, exists := timeline.TootIndexStatusIDMappings[id]
if exists == false {
tootIndex := len(timeline.Toots)
timeline.Toots = append(timeline.Toots, NewToot(timeline.client, status, tootIndex))
timeline.Toots =
append(timeline.Toots, NewToot(timeline.client, status, tootIndex))

knownUserExists := false
for _, knownUser := range timeline.KnownUsers {
Expand All @@ -102,7 +107,8 @@ func (timeline *Timeline) Load() (error) {
}
}
if knownUserExists == false {
timeline.KnownUsers = append(timeline.KnownUsers, status.Account.Acct)
timeline.KnownUsers =
append(timeline.KnownUsers, status.Account.Acct)
}

timeline.TootIndexStatusIDMappings[id] = tootIndex
Expand All @@ -112,7 +118,13 @@ func (timeline *Timeline) Load() (error) {
return nil
}

func (timeline *Timeline) Toot(status *string, inReplyTo int, filesToUpload []string, visibility *string, sensitive bool, spoiler *string) (*mastodon.Status, error) {
func (timeline *Timeline) Toot(
status *string,
inReplyTo int,
filesToUpload []string,
visibility *string,
sensitive bool,
spoiler *string) (*mastodon.Status, error) {
newToot := mastodon.Toot{
Status: *status,
Visibility: *visibility,
Expand All @@ -128,7 +140,8 @@ func (timeline *Timeline) Toot(status *string, inReplyTo int, filesToUpload []st
var mediaIDs []mastodon.ID

for _, fileToUpload := range filesToUpload {
attachment, err := timeline.client.UploadMedia(context.Background(), fileToUpload)
attachment, err :=
timeline.client.UploadMedia(context.Background(), fileToUpload)
if err != nil {
return nil, err
}
Expand All @@ -142,7 +155,9 @@ func (timeline *Timeline) Toot(status *string, inReplyTo int, filesToUpload []st
return timeline.client.PostStatus(context.Background(), &newToot)
}

func (timeline *Timeline) Boost(tootID int, shallBe bool) (*mastodon.Status, error) {
func (timeline *Timeline) Boost(
tootID int,
shallBe bool) (*mastodon.Status, error) {
id := timeline.Toots[tootID].Status.ID

if shallBe == true {
Expand All @@ -151,7 +166,9 @@ func (timeline *Timeline) Boost(tootID int, shallBe bool) (*mastodon.Status, err
return timeline.client.Unreblog(context.Background(), id)
}

func (timeline *Timeline) Fav(tootID int, shallBe bool) (*mastodon.Status, error) {
func (timeline *Timeline) Fav(
tootID int,
shallBe bool) (*mastodon.Status, error) {
id := timeline.Toots[tootID].Status.ID

if shallBe == true {
Expand Down
5 changes: 4 additions & 1 deletion mast/toot.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ type Toot struct {
Status mastodon.Status
}

func NewToot(mastodonClient *mastodon.Client, mastodonStatus *mastodon.Status , id int) Toot {
func NewToot(
mastodonClient *mastodon.Client,
mastodonStatus *mastodon.Status ,
id int) Toot {
toot := Toot{
client: mastodonClient,

Expand Down
5 changes: 4 additions & 1 deletion tui/timeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"github.com/mrusme/gomphotherium/mast"
)

func RenderTimeline(timeline *mast.Timeline, width int, showImages bool) (string, error) {
func RenderTimeline(
timeline *mast.Timeline,
width int,
showImages bool) (string, error) {
var output string = ""
var err error = nil

Expand Down
51 changes: 45 additions & 6 deletions tui/toot.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,64 @@ func RenderToot(toot *mast.Toot, width int, showImages bool) (string, error) {
}

inReplyTo := ""
inReplyToLen := 0
if status.InReplyToID != nil {
inReplyTo = " \xe2\x87\x9f"
inReplyToLen = 2
}

output = fmt.Sprintf("%s[blue]%s[-] [grey]%s[-][magenta]%s[-][grey]%*d[-]\n", output, status.Account.DisplayName, account, inReplyTo, (width - len(string(toot.ID)) - runewidth.StringWidth(status.Account.DisplayName) - len(account) - inReplyToLen), toot.ID)
output = fmt.Sprintf("%s%s\n", output, html.UnescapeString(strip.StripTags(status.Content)))
idPadding :=
width -
len(string(toot.ID)) -
runewidth.StringWidth(status.Account.DisplayName) -
len(account) -
// https://github.com/mattn/go-runewidth/issues/36
runewidth.StringWidth(inReplyTo)

output = fmt.Sprintf("%s[blue]%s[-] [grey]%s[-][magenta]%s[-][grey]%*d[-]\n",
output,
status.Account.DisplayName,
account,
inReplyTo,
idPadding,
toot.ID,
)
output = fmt.Sprintf("%s%s\n",
output,
html.UnescapeString(strip.StripTags(status.Content)),
)

if showImages == true {
for _, attachment := range status.MediaAttachments {
pix, err := ansimage.NewScaledFromURL(attachment.PreviewURL, int((float64(width) * 0.75)), width, color.Transparent, ansimage.ScaleModeResize, ansimage.NoDithering)
pix, err := ansimage.NewScaledFromURL(
attachment.PreviewURL,
int((float64(width) * 0.75)),
width,
color.Transparent,
ansimage.ScaleModeResize,
ansimage.NoDithering,
)
if err == nil {
output = fmt.Sprintf("%s\n%s\n", output, pix.RenderExt(false, false))
}
}
}

output = fmt.Sprintf("%s[magenta]\xe2\x86\xab %d[-] [green]\xe2\x86\xbb %d[-] [yellow]\xe2\x98\x85 %d[-] [grey]on %s at %s[-]\n", output, status.RepliesCount, status.ReblogsCount, status.FavouritesCount, createdAt.Format("Jan 2"), createdAt.Format("15:04"))
output = fmt.Sprintf("%s[magenta]\xe2\x86\xab %d[-] ",
output,
status.RepliesCount,
)
output = fmt.Sprintf("%s[green]\xe2\x86\xbb %d[-] ",
output,
status.ReblogsCount,
)
output = fmt.Sprintf("%s[yellow]\xe2\x98\x85 %d[-] ",
output,
status.FavouritesCount,
)
output = fmt.Sprintf("%s[grey]on %s at %s[-]\n",
output,
createdAt.Format("Jan 2"),
createdAt.Format("15:04"),
)

output = fmt.Sprintf("%s\n", output)
return output, err
Expand Down
21 changes: 20 additions & 1 deletion tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,22 @@ type TUICore struct {
Help string
}


func TUI(tuiCore TUICore) {
tview.Styles = tview.Theme{
PrimitiveBackgroundColor: tcell.ColorDefault,
ContrastBackgroundColor: tcell.ColorBlue,
MoreContrastBackgroundColor: tcell.ColorGreen,
BorderColor: tcell.ColorWhite,
TitleColor: tcell.ColorWhite,
GraphicsColor: tcell.ColorWhite,
PrimaryTextColor: tcell.ColorWhite,
SecondaryTextColor: tcell.ColorYellow,
TertiaryTextColor: tcell.ColorGreen,
InverseTextColor: tcell.ColorBlue,
ContrastSecondaryTextColor: tcell.ColorDarkCyan,
}

tuiCore.App = tview.NewApplication()

tuiCore.Timeline = mast.NewTimeline(tuiCore.Client)
Expand Down Expand Up @@ -171,7 +186,11 @@ func (tuiCore *TUICore) UpdateTimeline(scrollToEnd bool) bool {
return false
}

output, err := RenderTimeline(&tuiCore.Timeline, w, tuiCore.Options.ShowImages)
output, err := RenderTimeline(
&tuiCore.Timeline,
w,
tuiCore.Options.ShowImages,
)

if err != nil {
// TODO: Display errors somewhere
Expand Down

0 comments on commit 3fe966d

Please sign in to comment.