Skip to content

Instantly share code, notes, and snippets.

@spytheman
Created February 7, 2020 14:56
Show Gist options
  • Save spytheman/6079ab7cd18f3bfd79953d1adc747e4e to your computer and use it in GitHub Desktop.
Save spytheman/6079ab7cd18f3bfd79953d1adc747e4e to your computer and use it in GitHub Desktop.
pub fn header(otext, divider string) string {
cols, _ := get_terminal_size()
mut text := otext
if text.len > 0 {
text = ' $text '
}
// clip the text if it is too long
text = if cols > text.len + 1 + 2*divider.len { text } else { text[0..cols-(1+2*divider.len)] }
// make the text align with the terminal size:
if (text.len % 2) != (cols % 2 ) {
text += divider
}
filler := divider.repeat( (cols - text.len) / (2*divider.len) )
return filler + text + filler
}
fn test_header(){
divider := term.h_divider('-')
term_width := divider.len
empty_header := term.header('', '-')
short_header := term.header('reasonable header', '-')
very_long_header := term.header(['abc'].repeat(500).join(' '), '-')
eprintln( empty_header )
eprintln( short_header )
eprintln(term.header('another longer header', '-+-'))
eprintln(term.header('another longer header', '-'))
eprintln(term.header('short','-'))
eprintln(term.header('12345','-'))
eprintln(term.header('1234','-'))
eprintln(term.header('123','-'))
eprintln(term.header('12','-'))
eprintln(term.header('1','-'))
eprintln( very_long_header )
eprintln(term.header('','-'))
assert term_width == empty_header.len
assert term_width == short_header.len
assert term_width == very_long_header.len
}
@spytheman
Copy link
Author

Produces:

--------------------------------------------------------------------------------
------------------------------ reasonable header -------------------------------
-+--+--+--+--+--+--+--+--+- another longer header -+--+--+--+--+--+--+--+--+--+-
---------------------------- another longer header -----------------------------
------------------------------------ short -------------------------------------
------------------------------------ 12345 -------------------------------------
------------------------------------- 1234 -------------------------------------
------------------------------------- 123 --------------------------------------
-------------------------------------- 12 --------------------------------------
-------------------------------------- 1 ---------------------------------------
- abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc --
--------------------------------------------------------------------------------

@spytheman
Copy link
Author

spytheman commented Feb 7, 2020

pub fn header(text, divider string) string {
    cols, _ := get_terminal_size()
    text_limit := if cols > text.len + 1 + 2*divider.len {
        text.len
    } else {
        cols-(1+2*divider.len)
    }
    text_limit_alligned := if (text_limit % 2) != (cols % 2 ) {
        text_limit + 1
    } else {
        text_limit
    }
    text_start := (cols - text_limit_alligned)/2
    filler_line := divider.repeat( 1 + cols / divider.len )[0..cols]
    result := if text.len == 0 {
        filler_line
    } else {
        filler_line[0..text_start-1] + ' ' + text[0..(text_limit)] + ' ' + filler_line[text_start+text_limit+1..cols]
    }
    return result
}

This one produces:

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------------ reasonable header -------------------------------
_-/\_-/\_-/\_-/\_-/\_-/\_-/\ another longer header \_-/\_-/\_-/\_-/\_-/\_-/\_-/\
---------------------------- another longer header -----------------------------
------------------------------------ short -------------------------------------
------------------------------------ 12345 -------------------------------------
------------------------------------- 1234 -------------------------------------
------------------------------------- 123 --------------------------------------
-------------------------------------- 12 --------------------------------------
-------------------------------------- 1 ---------------------------------------
 abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc a -
 abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd ab -
--------------------------------------------------------------------------------

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment