/*************************************************************************
 ***                        prompt.c                     (JJB TEMPLAR) ***
 *** Date modifications begun: 7/8/89.                                 ***
 *** Last modified: 10/8/89.                                           ***
 ***                14/8/89 - prompt in titlebar. NEVER return NULL!   ***
 *************************************************************************/
/*** Needs to be tightened up.                                         ***
 *************************************************************************/

#include "less.h"
#include "position.h"
#include <workbench/startup.h>

extern int  pr_type;
extern int  hit_eof;
extern int  new_file;
extern int  sc_width;
extern char current_file[];
extern int  ac;
extern char **av;
extern struct WBArg *wbArgs;
extern int  curr_ac;
extern char errmsg[];

int     oldpr;
static char message[500];   /* Should be long enough */

static void ap_filename() /*=============================================*/
{                         /* Init prompt to filename :                   */
    strcpy(message,current_file);   strcat(message," :");
}

static void ap_of() /*===================================================*/
{                   /* Append file N of M to message.                    */
    if (ac > 1)
        sprintf(message+strlen(message)," (file %d of %d) ",curr_ac+1,ac);
}

static void ap_byte() /*=================================================*/
{                     /* Append byte position to message.                */
LONG    pos,len;

    pos = position(BOTTOM_PLUS_ONE);
    if (pos != NULL_POSITION) {
        sprintf(message + strlen(message), " byte %d ", pos);
        len = ch_length();
        if (len > 0)
            sprintf(message + strlen(message), "/%d ", len);
    }
}

static void ap_percent(must_print) /*====================================*/
{                                  /* Append percentage into file.       */
LONG    pos,len;
    pos = position(BOTTOM_PLUS_ONE);
    len = ch_length();
    if ((len > 0) && (pos != NULL_POSITION))
        sprintf(message + strlen(message), " (%d%%) ", (100 * pos) / len);
    else if (must_print) ap_byte();
}

static void ap_eof() /*==================================================*/
{                    /* Append end to message.                           */
    strcat(message, " END ");
    if (curr_ac + 1 < ac)
        sprintf(message+strlen(message)," - Next: %s ",wbArgs[curr_ac+1].wa_Name); /* av[curr_ac+1] */
}

char    *eq_message() /*=================================================*/
{                     /* Return message to '=' command.                  */
    message[0] = 0;     /* Initialize string. */
    ap_filename();      /* Add filename */
    ap_of();            /* Add N of M stuff */
    ap_byte();          /* Add byte position */
    ap_percent(0);      /* Add percent. must_print = FALSE */

    return(message);    /* SetWindowTitles clips automatically */
}

char    *pr_string() /*==================================================*/
{                    /* Return prompt. If NULL, caller uses ':'.         */
    message[0] = 0;
    switch (pr_type) {
        case (PR_SHORT):    ap_filename();
                            if (new_file) ap_of();
                            if (hit_eof) ap_eof();
                            break;
        case (PR_MEDIUM):   ap_filename();
                            if (new_file) ap_of();
                            if (hit_eof) ap_eof();
                            else ap_percent(1);
                            break;
        case (PR_LONG):     ap_filename();
                            if (new_file) ap_of();
                            ap_byte();
                            if (hit_eof) ap_eof();
                            else ap_percent(0);
                            break;
        case (PR_ERROR):    strcpy(message,errmsg);
                            pr_type = oldpr;
                            break;      /* Used by error() when rw = 0 */
    }
    new_file = 0;
    return(message);    /* SetWindowTitles clips automatically */
}
