shithub: hell

ref: 416169ab2ea220f6396a557bc3dad0f1566552cf
dir: /prompt.go/

View raw version
package main

import (
	"context"
	"fmt"
	"strings"

	"github.com/ergochat/readline"
)

// Returns text to add to the status bar
type PromptItem interface {
	Update() (string, bool, error)
}

// Renders a status bar with a prompt and PromptItems
type PromptBar struct {
	prompt string
	items  []PromptItem
	rl     *readline.Instance
}

// Render the prompt and update the readline prompt
func (pb *PromptBar) UpdatePrompt() {
	var sb strings.Builder

	for item := range pb.items {
		status, print, err := pb.items[item].Update()
		if err != nil {
			fmt.Printf("Error updating status item: %s\n", err)
		}
		if print {
			sb.WriteString(status)
			sb.WriteString(" ")
		}
	}
	sb.WriteString(pb.prompt)
	pb.rl.SetPrompt(sb.String())
}

type NotificationCounter struct {
	*Hellclient
}

func (nc *NotificationCounter) Update() (string, bool, error) {
	unread, err := nc.client.GetUnreadNotifications(context.Background(), nil, nil, 0)
	if err != nil {
		return "", false, err
	}
	if unread.Count == 0 {
		return "", false, nil
	}
	return fmt.Sprintf("ur:%d", unread.Count), true, nil
}

// MultiLineIndicator shows when multi-line input mode is active.
type MultiLineIndicator struct {
	*Hellclient
}

func (ml *MultiLineIndicator) Update() (string, bool, error) {
	if ml.multiLineMode {
		return "MULTILINE", true, nil
	}
	return "", false, nil
}

type PauseIndicator struct {
	*Hellclient
}

// Display whether the client is paused or not
func (pi *PauseIndicator) Update() (string, bool, error) {
	if pi.isPaused {
		return "STREAM PAUSED", true, nil
	}
	return "", false, nil
}

// Display when you have attachments ready to post
func (sam *StatusAttachmentHolder) Update() (string, bool, error) {
	if len(sam.attachments) < 1 {
		return "", false, nil
	}
	var sb strings.Builder

	sb.WriteString("ATTACHED:[")
	for item := range sam.attachments {
		sb.WriteString(sam.attachments[item].Type)
		if item != len(sam.attachments) {
			sb.WriteString(" ")
		}
	}
	sb.WriteString("]")
	return sb.String(), true, nil
}