home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume6 / less2 / part2 / prompt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  2.9 KB  |  170 lines

  1. /*
  2.  * Prompting and other messages.
  3.  * There are three flavors of prompts, SHORT, MEDIUM and LONG,
  4.  * selected by the -m/-M options.
  5.  * A prompt is either a colon or a message composed of various
  6.  * pieces, such as the name of the file being viewed, the percentage
  7.  * into the file, etc.
  8.  */
  9.  
  10. #include "less.h"
  11. #include "position.h"
  12.  
  13. extern int pr_type;
  14. extern int ispipe;
  15. extern int hit_eof;
  16. extern int new_file;
  17. extern int sc_width;
  18. extern char current_file[];
  19. extern int ac;
  20. extern char **av;
  21. extern int curr_ac;
  22.  
  23. static char message[500];
  24.  
  25. /*
  26.  * Append the name of the current file (to the message buffer).
  27.  */
  28.     static void
  29. ap_filename()
  30. {
  31.     if (!ispipe)
  32.         sprintf(message + strlen(message), 
  33.             "%s", current_file);
  34. }
  35.  
  36. /*
  37.  * Append the "file N of M" message.
  38.  */
  39.     static void
  40. ap_of()
  41. {
  42.     if (ac > 1)
  43.         sprintf(message + strlen(message), 
  44.             " (file %d of %d)", curr_ac+1, ac);
  45. }
  46.  
  47. /*
  48.  * Append the byte offset into the current file.
  49.  */
  50.     static void
  51. ap_byte()
  52. {
  53.     POSITION pos, len;
  54.  
  55.     pos = position(BOTTOM_PLUS_ONE);
  56.     if (pos != NULL_POSITION)
  57.     {
  58.         sprintf(message + strlen(message), 
  59.             " byte %ld", pos);
  60.         len = ch_length();
  61.         if (len > 0)
  62.             sprintf(message + strlen(message), 
  63.                 "/%ld", len);
  64.     }
  65. }
  66.  
  67. /*
  68.  * Append the percentage into the current file.
  69.  * If we cannot find the percentage and must_print is true,
  70.  * the use the byte offset.
  71.  */
  72.     static void
  73. ap_percent(must_print)
  74. {
  75.     POSITION pos,len;
  76.  
  77.     pos = position(BOTTOM_PLUS_ONE);
  78.     len = ch_length();
  79.     if (len > 0 && pos != NULL_POSITION)
  80.         sprintf(message + strlen(message),
  81.             " (%ld%%)", (100 * pos) / len);
  82.     else if (must_print)
  83.         ap_byte();
  84. }
  85.  
  86. /*
  87.  * Append the end-of-file message.
  88.  */
  89.     static void
  90. ap_eof()
  91. {
  92.     strcat(message, " END");
  93.     if (curr_ac + 1 < ac)
  94.         sprintf(message + strlen(message),
  95.             " - Next: %s", av[curr_ac+1]);
  96. }
  97.  
  98. /*
  99.  * Return a message suitable for printing by the "=" command.
  100.  */
  101.     public char *
  102. eq_message()
  103. {
  104.     message[0] = '\0';
  105.     ap_filename();
  106.     ap_of();
  107.     ap_byte();
  108.     ap_percent(0);
  109.     /*
  110.      * Truncate to the screen width.
  111.      * {{ This isn't very nice. }}
  112.      */
  113.     message[error_width()] = '\0';
  114.     return (message);
  115. }
  116.  
  117. /*
  118.  * Return a prompt.
  119.  * This depends on the prompt type (SHORT, MEDIUM, LONG), etc.
  120.  * If we can't come up with an appropriate prompt, return NULL
  121.  * and the caller will prompt with a colon.
  122.  */
  123.     public char *
  124. pr_string()
  125. {
  126.     message[0] = '\0';
  127.     switch (pr_type)
  128.     {
  129.     case PR_SHORT:
  130.         if (new_file)
  131.         {
  132.             ap_filename();
  133.             ap_of();
  134.         }
  135.         if (hit_eof)
  136.             ap_eof();
  137.         break;
  138.     case PR_MEDIUM:
  139.         if (new_file)
  140.         {
  141.             ap_filename();
  142.             ap_of();
  143.         }
  144.         if (hit_eof)
  145.             ap_eof();
  146.         else
  147.             ap_percent(1);
  148.         break;
  149.     case PR_LONG:
  150.         ap_filename();
  151.         if (new_file)
  152.             ap_of();
  153.         ap_byte();
  154.         if (hit_eof)
  155.             ap_eof();
  156.         else
  157.             ap_percent(0);
  158.         break;
  159.     }
  160.     new_file = 0;
  161.     if (message[0] == '\0')
  162.         return (NULL);
  163.     /*
  164.      * Truncate to the screen width.
  165.      * {{ This isn't very nice. }}
  166.      */
  167.     message[sc_width-2] = '\0';
  168.     return (message);
  169. }
  170.