Blame src/ColumnOutput.cc

Packit 8f70b4
/*
Packit 8f70b4
 * lftp - file transfer program
Packit 8f70b4
 *
Packit 8f70b4
 * Copyright (c) 1996-2012 by Alexander V. Lukyanov (lav@yars.free.net)
Packit 8f70b4
 *
Packit 8f70b4
 * This program is free software; you can redistribute it and/or modify
Packit 8f70b4
 * it under the terms of the GNU General Public License as published by
Packit 8f70b4
 * the Free Software Foundation; either version 3 of the License, or
Packit 8f70b4
 * (at your option) any later version.
Packit 8f70b4
 *
Packit 8f70b4
 * This program is distributed in the hope that it will be useful,
Packit 8f70b4
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8f70b4
 * GNU General Public License for more details.
Packit 8f70b4
 *
Packit 8f70b4
 * You should have received a copy of the GNU General Public License
Packit 8f70b4
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 8f70b4
 */
Packit 8f70b4
Packit 8f70b4
#include <config.h>
Packit 8f70b4
#include <sys/types.h>
Packit 8f70b4
#include <sys/stat.h>
Packit 8f70b4
#include <fcntl.h>
Packit 8f70b4
#ifdef HAVE_UNISTD_H
Packit 8f70b4
#include <unistd.h>
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
#include <mbswidth.h>
Packit 8f70b4
Packit 8f70b4
#include "SMTask.h"
Packit 8f70b4
#include "ColumnOutput.h"
Packit 8f70b4
#include "ResMgr.h"
Packit 8f70b4
#include "misc.h"
Packit 8f70b4
#include "DirColors.h"
Packit 8f70b4
Packit 8f70b4
ResDecl res_color	("color:use-color",		"auto",ResMgr::TriBoolValidate,ResMgr::NoClosure);
Packit 8f70b4
Packit 8f70b4
#define lst_cnt lst.count()
Packit 8f70b4
Packit 8f70b4
void ColumnOutput::add(const char *name, const char *color)
Packit 8f70b4
{
Packit 8f70b4
   lst[lst_cnt-1]->append(name, color);
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
void ColumnOutput::addf(const char *fmt, const char *color, ...)
Packit 8f70b4
{
Packit 8f70b4
   va_list v;
Packit 8f70b4
   va_start(v, color);
Packit 8f70b4
   add(xstring::vformat(fmt,v), color);
Packit 8f70b4
   va_end(v);
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
void ColumnOutput::append()
Packit 8f70b4
{
Packit 8f70b4
   lst.append(new datum);
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
/* The minimum width of a colum is 3: 1 character for the name and 2
Packit 8f70b4
 * for the separating white space.  */
Packit 8f70b4
#define MIN_COLUMN_WIDTH        3
Packit 8f70b4
Packit 8f70b4
/* Assuming cursor is at position FROM, indent up to position TO.
Packit 8f70b4
 * Use a TAB character instead of two or more spaces whenever possible.  */
Packit 8f70b4
static void
Packit 8f70b4
indent (int from, int to, const JobRef<OutputJob>& o)
Packit 8f70b4
{
Packit 8f70b4
   // TODO
Packit 8f70b4
#define tabsize 8
Packit 8f70b4
   while (from < to) {
Packit 8f70b4
      if (tabsize > 0 && to / tabsize > (from + 1) / tabsize) {
Packit 8f70b4
	 o->Put("\t");
Packit 8f70b4
	 from += tabsize - from % tabsize;
Packit 8f70b4
      } else {
Packit 8f70b4
	 o->Put(" ");
Packit 8f70b4
	 from++;
Packit 8f70b4
      }
Packit 8f70b4
   }
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
void ColumnOutput::get_print_info(unsigned width, xarray<int> &col_arr, xarray<int> &ws_arr, int &cols) const
Packit 8f70b4
{
Packit 8f70b4
   /* Maximum number of columns ever possible for this display.  */
Packit 8f70b4
   int max_idx = width / MIN_COLUMN_WIDTH;
Packit 8f70b4
   if (max_idx == 0) max_idx = 1;
Packit 8f70b4
Packit 8f70b4
   /* Normally the maximum number of columns is determined by the
Packit 8f70b4
    * screen width.  But if few files are available this might limit it
Packit 8f70b4
    * as well. */
Packit 8f70b4
   int max_cols = max_idx > lst_cnt ? lst_cnt : max_idx;
Packit 8f70b4
   if(max_cols < 1) max_cols = 1;
Packit 8f70b4
Packit 8f70b4
   /* Compute the maximum number of possible columns.  */
Packit 8f70b4
   for (cols = max_cols; cols >= 1; cols--) {
Packit 8f70b4
      col_arr.truncate();
Packit 8f70b4
      ws_arr.truncate();
Packit 8f70b4
      for (int j = 0; j < max_idx; ++j) {
Packit 8f70b4
	 col_arr.append(MIN_COLUMN_WIDTH);
Packit 8f70b4
	 ws_arr.append(99999999);
Packit 8f70b4
      }
Packit 8f70b4
Packit 8f70b4
      int filesno;
Packit 8f70b4
      /* Find the amount of whitespace shared by every entry in the column. */
Packit 8f70b4
      for (filesno = 0; filesno < lst_cnt; ++filesno) {
Packit 8f70b4
	 int idx = filesno / ((lst_cnt + cols - 1) / cols);
Packit 8f70b4
	 int ws = lst[filesno]->whitespace();
Packit 8f70b4
	 if(ws < ws_arr[idx]) ws_arr[idx] = ws;
Packit 8f70b4
      }
Packit 8f70b4
Packit 8f70b4
      /* Strip as much whitespace off the left as possible, but strip
Packit 8f70b4
       * the same amount from each entry (per column) to keep each
Packit 8f70b4
       * column aligned with itself. */
Packit 8f70b4
      unsigned line_len = cols * MIN_COLUMN_WIDTH;
Packit 8f70b4
      for (filesno = 0; filesno < lst_cnt; ++filesno) {
Packit 8f70b4
	 int idx = filesno / ((lst_cnt + cols - 1) / cols);
Packit 8f70b4
	 int name_length = lst[filesno]->width();
Packit 8f70b4
Packit 8f70b4
	 /* all but the last column get 2 spaces of padding */
Packit 8f70b4
	 int real_length = name_length + (idx == cols-1 ? 0 : 2) - ws_arr[idx];
Packit 8f70b4
Packit 8f70b4
	 if (real_length <= col_arr[idx]) continue;
Packit 8f70b4
Packit 8f70b4
	 line_len += (real_length - col_arr[idx]);
Packit 8f70b4
	 col_arr[idx] = real_length;
Packit 8f70b4
      }
Packit 8f70b4
      if(line_len < width)
Packit 8f70b4
	 break; /* found it */
Packit 8f70b4
   }
Packit 8f70b4
   if(cols == 0)
Packit 8f70b4
      cols = 1;
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
void ColumnOutput::print(const JobRef<OutputJob>& o, unsigned width, bool color) const
Packit 8f70b4
{
Packit 8f70b4
   if(!lst_cnt) return; /* we have nothing to display */
Packit 8f70b4
Packit 8f70b4
   int cols;
Packit 8f70b4
   xarray<int> col_arr;
Packit 8f70b4
   xarray<int> ws_arr;
Packit 8f70b4
Packit 8f70b4
   get_print_info(width, col_arr, ws_arr, cols);
Packit 8f70b4
Packit 8f70b4
   /* Calculate the number of rows that will be in each column except possibly
Packit 8f70b4
    * for a short column on the right. */
Packit 8f70b4
   int rows = lst_cnt / cols + (lst_cnt % cols != 0);
Packit 8f70b4
Packit 8f70b4
   DirColors *dc=DirColors::GetInstance();
Packit 8f70b4
   const char *color_pref =dc->Lookup(".lc");
Packit 8f70b4
   const char *color_suf  =dc->Lookup(".rc");
Packit 8f70b4
   const char *color_reset=dc->Lookup(".ec");
Packit 8f70b4
Packit 8f70b4
   for (int row = 0; row < rows; row++) {
Packit 8f70b4
      int col = 0;
Packit 8f70b4
      int filesno = row;
Packit 8f70b4
      int pos = 0;                      /* Current character column. */
Packit 8f70b4
      /* Print the next row.  */
Packit 8f70b4
      while (1) {
Packit 8f70b4
	 lst[filesno]->print(o, color, ws_arr[col], color_pref, color_suf, color_reset);
Packit 8f70b4
	 int name_length = lst[filesno]->width() - ws_arr[col];
Packit 8f70b4
	 int max_name_length = col_arr[col++];
Packit 8f70b4
Packit 8f70b4
	 filesno += rows;
Packit 8f70b4
	 if (filesno >= lst_cnt)
Packit 8f70b4
	    break;
Packit 8f70b4
Packit 8f70b4
	 indent (pos + name_length, pos + max_name_length, o);
Packit 8f70b4
	 pos += max_name_length;
Packit 8f70b4
      }
Packit 8f70b4
      o->Put("\n");
Packit 8f70b4
   }
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
void datum::append(const char *name, const char *color)
Packit 8f70b4
{
Packit 8f70b4
   names.Append(name);
Packit 8f70b4
   colors.Append(color);
Packit 8f70b4
   if(names.Count() == 1) {
Packit 8f70b4
      ws = 0;
Packit 8f70b4
      for(int c = 0; name[c]; c++) {
Packit 8f70b4
	 if(name[c] != ' ') break;
Packit 8f70b4
	 ws++;
Packit 8f70b4
      }
Packit 8f70b4
   }
Packit 8f70b4
   curwidth += mbswidth(name, 0);
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
void datum::print(const JobRef<OutputJob>& o, bool color, int skip,
Packit 8f70b4
		const char *color_pref, const char *color_suf, const char *color_reset) const
Packit 8f70b4
{
Packit 8f70b4
   const char *cur_color = 0;
Packit 8f70b4
Packit 8f70b4
   for(int i = 0; i < names.Count(); i++) {
Packit 8f70b4
      int len = strlen(names[i]);
Packit 8f70b4
      if(len < skip) {
Packit 8f70b4
	 skip -= len;
Packit 8f70b4
	 continue;
Packit 8f70b4
      }
Packit 8f70b4
Packit 8f70b4
      if(color) {
Packit 8f70b4
	 if(colors[i][0]) {
Packit 8f70b4
	    /* if it's the same color, don't bother */
Packit 8f70b4
	    if(!cur_color || !strcmp(cur_color, colors[i])) {
Packit 8f70b4
	       o->Put(color_pref);
Packit 8f70b4
	       o->Put(colors[i]);
Packit 8f70b4
	       o->Put(color_suf);
Packit 8f70b4
Packit 8f70b4
	       cur_color = colors[i];
Packit 8f70b4
	    }
Packit 8f70b4
	 } else {
Packit 8f70b4
	    /* reset color, if we have one */
Packit 8f70b4
	    if(cur_color) {
Packit 8f70b4
	       o->Put(color_reset);
Packit 8f70b4
	       cur_color = 0;
Packit 8f70b4
	    }
Packit 8f70b4
	 }
Packit 8f70b4
      }
Packit 8f70b4
      o->Put(names[i]+skip);
Packit 8f70b4
      skip = 0;
Packit 8f70b4
   }
Packit 8f70b4
Packit 8f70b4
   if(cur_color)
Packit 8f70b4
      o->Put(color_reset);
Packit 8f70b4
}