Skip to content

Commit

Permalink
Add --file-size option.
Browse files Browse the repository at this point in the history
  • Loading branch information
gwsw committed Apr 30, 2021
1 parent 0e822fc commit a4caa78
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.VER
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
* If XDG_DATA_HOME is set, find and store history file
in $XDG_DATA_HOME/lesshst rather than $HOME/.lesshst.

* Add the --file-size option.

* Fix bug which could leave terminal in mouse-reporting mode
after exiting less.

Expand Down
4 changes: 3 additions & 1 deletion edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ extern int force_open;
extern int is_tty;
extern int sigs;
extern int hshift;
extern int want_filesize;
extern IFILE curr_ifile;
extern IFILE old_ifile;
extern struct scrpos initial_scrpos;
Expand Down Expand Up @@ -480,7 +481,8 @@ edit_ifile(ifile)
cmd_addhist(ml_examine, qfilename, 1);
free(qfilename);
}

if (want_filesize)
scan_eof();
}
free(filename);
return (0);
Expand Down
2 changes: 2 additions & 0 deletions less.hlp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@
Don't display tildes after end of file.
-# [_N] .... --shift=[_N]
Set horizontal scroll amount (0 = one half screen width).
--file-size
Automatically determine the size of the input file.
--follow-name
The F command changes files if the input file is renamed.
--incsearch
Expand Down
6 changes: 6 additions & 0 deletions less.nro.VER
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,12 @@ If the number is specified as a fraction, the actual number of
scroll positions is recalculated if the terminal window is resized,
so that the actual scroll remains at the specified fraction
of the screen width.
.IP "\-\-file-size"
If \-\-file-size is specified,
.I less
will determine the size of the file
immediately after opening the file.
Normally this is not done, because it can be slow if the input file is large.
.IP "\-\-follow-name"
Normally, if the input file is renamed while an F command is executing,
.I less
Expand Down
23 changes: 23 additions & 0 deletions linenum.c
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,26 @@ currline(where)
linenum--;
return (linenum);
}

/*
* Scan entire file, counting line numbers.
*/
public void
scan_eof(VOID_PARAM)
{
POSITION pos = 0;
LINENUM linenum = 0;

if (ch_seek(0))
return;
ierror("Determining length of file", NULL_PARG);
while (pos != NULL_POSITION)
{
/* For efficiency, only add one every 256 line numbers. */
if ((linenum++ % 256) == 0)
add_lnum(linenum, pos);
pos = forw_raw_line(pos, (char **)NULL, (int *)NULL);
if (ABORT_SIGS())
break;
}
}
22 changes: 22 additions & 0 deletions optfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ extern int less_is_more;
extern int linenum_width;
extern int status_col_width;
extern int use_color;
extern int want_filesize;
#if LOGFILE
extern char *namelogfile;
extern int force_logfile;
Expand Down Expand Up @@ -955,6 +956,27 @@ opt_status_col_width(type, s)
}
}

/*
* Handler for the --file-size option.
*/
/*ARGSUSED*/
public void
opt_filesize(type, s)
int type;
char *s;
{
switch (type)
{
case INIT:
case TOGGLE:
if (want_filesize && curr_ifile != NULL && ch_length() == NULL_POSITION)
scan_eof();
break;
case QUERY:
break;
}
}

#if LESSTEST
/*
* Handler for the --tty option.
Expand Down
10 changes: 10 additions & 0 deletions opttbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public int linenum_width; /* Width of line numbers */
public int status_col_width; /* Width of status column */
public int incr_search; /* Incremental search */
public int use_color; /* Use UI color */
public int want_filesize; /* */
#if HILITE_SEARCH
public int hilite_search; /* Highlight matched search patterns? */
#endif
Expand Down Expand Up @@ -130,6 +131,7 @@ static struct optname linenum_width_optname = { "line-num-width", NULL };
static struct optname status_col_width_optname = { "status-col-width", NULL };
static struct optname incr_search_optname = { "incsearch", NULL };
static struct optname use_color_optname = { "use-color", NULL };
static struct optname want_filesize_optname = { "file-size", NULL };
#if LESSTEST
static struct optname ttyin_name_optname = { "tty", NULL };
static struct optname rstat_optname = { "rstat", NULL };
Expand Down Expand Up @@ -545,6 +547,14 @@ static struct loption option[] =
NULL
}
},
{ OLETTER_NONE, &want_filesize_optname,
BOOL|REPAINT, OPT_OFF, &want_filesize, opt_filesize,
{
"Don't get size of each file",
"Get size of each file",
NULL
}
},
#if LESSTEST
{ OLETTER_NONE, &ttyin_name_optname,
STRING|NO_TOGGLE, 0, NULL, opt_ttyin_name,
Expand Down

0 comments on commit a4caa78

Please sign in to comment.