shithub: hell

ref: b343e7124f74b60d9ca6595dad1678177a95308d
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
}