[BACK]Return to util.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/util.c, Revision 1.2

1.2     ! nicm        1: /* $OpenBSD: util.c,v 1.1 2009/06/01 22:58:49 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
                      5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/types.h>
                     20:
                     21: #include "tmux.h"
                     22:
                     23: /* Return a section of a string around a point. */
                     24: char *
                     25: section_string(char *buf, size_t len, size_t sectoff, size_t sectlen)
                     26: {
                     27:        char    *s;
                     28:        size_t   first, last;
                     29:
                     30:        if (len <= sectlen) {
                     31:                first = 0;
                     32:                last = len;
                     33:        } else if (sectoff < sectlen / 2) {
                     34:                first = 0;
                     35:                last = sectlen;
                     36:        } else if (sectoff + sectlen / 2 > len) {
                     37:                last = len;
                     38:                first = last - sectlen;
                     39:        } else {
                     40:                first = sectoff - sectlen / 2;
                     41:                last = first + sectlen;
                     42:        }
                     43:
                     44:        if (last - first > 3 && first != 0)
                     45:                first += 3;
                     46:        if (last - first > 3 && last != len)
                     47:                last -= 3;
                     48:
                     49:        xasprintf(&s, "%s%.*s%s", first == 0 ? "" : "...",
                     50:            (int) (last - first), buf + first, last == len ? "" : "...");
                     51:        return (s);
                     52: }