Skip to content

Commit 0b162e0

Browse files
committed
panel: implement string_file_ext() for extension column display
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 - Returns an empty string for directories and extensionless files Also document the extension field in the man page and NEWS. Signed-off-by: Ilia Karpov <iakarpov@edu.hse.ru> Signed-off-by: ki <karpovilia@gmail.com>
1 parent 1796996 commit 0b162e0

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

doc/NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Starting with this release, we will be using language features that require a C9
3232

3333
- Fixes
3434

35+
* Extension column in user-defined panel format now displays only the file extension instead of the full filename
3536
* Segfault if filter makes file panel empty (#4600)
3637
* Segfault in built-in help when going to the previous topic (#4627)
3738
* Incorrect handling of ext2 attributes of a directory (#4590)

doc/man/mc.1.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,10 @@ specifier. This are the available fields you may display:
798798
.B name
799799
displays the file name.
800800
.TP
801+
.B extension
802+
displays the file extension (the part after the last dot).
803+
For directories and files without an extension, this field is empty.
804+
.TP
801805
.B size
802806
displays the file size.
803807
.TP

src/filemanager/panel.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ 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);
162163

163164
/*** file scope variables ************************************************************************/
164165

@@ -178,9 +179,7 @@ static panel_field_t panel_fields[] = {
178179
{ "extension", 12, TRUE, J_LEFT_FIT,
179180
// TRANSLATORS: one single character to represent 'extension' sort mode
180181
// 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 },
182+
N_ ("sort|e"), N_ ("E&xtension"), TRUE, FALSE, string_file_ext, (GCompareFunc) sort_ext },
184183
{ "size", 7, FALSE, J_RIGHT,
185184
// TRANSLATORS: one single character to represent 'size' sort mode
186185
// TRANSLATORS: no need to translate 'sort', it's just a context prefix
@@ -355,6 +354,25 @@ string_file_name (const file_entry_t *fe, int len)
355354

356355
/* --------------------------------------------------------------------------------------------- */
357356

357+
static const char *
358+
string_file_ext (const file_entry_t *fe, int len)
359+
{
360+
const char *ext;
361+
362+
(void) len;
363+
364+
ext = extension (fe->fname->str);
365+
366+
if (ext[0] == '\0' || S_ISDIR (fe->st.st_mode))
367+
g_string_truncate (string_file_name_buffer, 0);
368+
else
369+
g_string_assign (string_file_name_buffer, ext);
370+
371+
return string_file_name_buffer->str;
372+
}
373+
374+
/* --------------------------------------------------------------------------------------------- */
375+
358376
static unsigned int
359377
ilog10 (dev_t n)
360378
{

0 commit comments

Comments
 (0)