Skip to content

Commit 5fc6ef8

Browse files
committed
panel: implement extension and basename display for user-defined formats
The extension panel field used string_file_name as its display callback, showing the full filename instead of just the extension. There was already a TODO comment acknowledging this (// TODO: string_file_ext). Add string_file_ext() which returns only the part after the last dot for regular files, or an empty string for directories and extensionless files. Add string_file_basename() which returns the filename without its extension (without the trailing dot), complementing the extension field. Both fields are documented in the man page. Example user_format: half basename | extension | size | mtime Signed-off-by: Ilia Karpov <iakarpov@edu.hse.ru> Signed-off-by: ki <karpovilia@gmail.com>
1 parent 1796996 commit 5fc6ef8

2 files changed

Lines changed: 49 additions & 3 deletions

File tree

doc/man/mc.1.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,14 @@ specifier. This are the available fields you may display:
798798
.B name
799799
displays the file name.
800800
.TP
801+
.B basename
802+
displays the file name without extension. For directories and files
803+
without an extension, this is identical to the full name.
804+
.TP
805+
.B extension
806+
displays the file extension (the part after the last dot).
807+
For directories and files without an extension, this field is empty.
808+
.TP
801809
.B size
802810
displays the file size.
803811
.TP

src/filemanager/panel.c

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ static const char *string_file_group (const file_entry_t *fe, int len);
159159
static const char *string_marked (const file_entry_t *fe, int len);
160160
static const char *string_space (const file_entry_t *fe, int len);
161161
static const char *string_dot (const file_entry_t *fe, int len);
162+
static const char *string_file_ext (const file_entry_t *fe, int len);
163+
static const char *string_file_basename (const file_entry_t *fe, int len);
162164

163165
/*** file scope variables ************************************************************************/
164166

@@ -178,9 +180,9 @@ static panel_field_t panel_fields[] = {
178180
{ "extension", 12, TRUE, J_LEFT_FIT,
179181
// TRANSLATORS: one single character to represent 'extension' sort mode
180182
// TRANSLATORS: no need to translate 'sort', it's just a context prefix
181-
N_ ("sort|e"), N_ ("E&xtension"), TRUE, FALSE,
182-
string_file_name, // TODO: string_file_ext
183-
(GCompareFunc) sort_ext },
183+
N_ ("sort|e"), N_ ("E&xtension"), TRUE, FALSE, string_file_ext, (GCompareFunc) sort_ext },
184+
{ "basename", 12, TRUE, J_LEFT_FIT, N_ ("sort|n"), N_ ("&Basename"), TRUE, FALSE,
185+
string_file_basename, (GCompareFunc) sort_name },
184186
{ "size", 7, FALSE, J_RIGHT,
185187
// TRANSLATORS: one single character to represent 'size' sort mode
186188
// TRANSLATORS: no need to translate 'sort', it's just a context prefix
@@ -355,6 +357,42 @@ string_file_name (const file_entry_t *fe, int len)
355357

356358
/* --------------------------------------------------------------------------------------------- */
357359

360+
static const char *
361+
string_file_ext (const file_entry_t *fe, MC_UNUSED int len)
362+
{
363+
const char *ext = extension (fe->fname->str);
364+
365+
if (ext[0] == '\0' || S_ISDIR (fe->st.st_mode))
366+
g_string_truncate (string_file_name_buffer, 0);
367+
else
368+
g_string_assign (string_file_name_buffer, ext);
369+
370+
return string_file_name_buffer->str;
371+
}
372+
373+
/* --------------------------------------------------------------------------------------------- */
374+
375+
static const char *
376+
string_file_basename (const file_entry_t *fe, MC_UNUSED int len)
377+
{
378+
const char *ext = extension (fe->fname->str);
379+
380+
mc_g_string_copy (string_file_name_buffer, fe->fname);
381+
382+
/* Remove the extension (including the dot) from the name */
383+
if (ext[0] != '\0' && !S_ISDIR (fe->st.st_mode))
384+
{
385+
size_t name_len = (size_t) (ext - fe->fname->str);
386+
387+
if (name_len > 0)
388+
g_string_truncate (string_file_name_buffer, name_len - 1);
389+
}
390+
391+
return string_file_name_buffer->str;
392+
}
393+
394+
/* --------------------------------------------------------------------------------------------- */
395+
358396
static unsigned int
359397
ilog10 (dev_t n)
360398
{

0 commit comments

Comments
 (0)