Fossil SCM
Add the "fossil test-terminal-size" command.
Commit
b241b9c999724307575ef29d52df40f282ced62a91305914520546406c20d1d6
Parent
1422b02222558f6…
1 file changed
+19
-8
+19
-8
| --- src/terminal.c | ||
| +++ src/terminal.c | ||
| @@ -51,12 +51,11 @@ | ||
| 51 | 51 | ** Under Linux/bash the size info is also available from env $LINES, $COLUMNS. |
| 52 | 52 | ** Or it can be queried using tput `echo -e "lines\ncols"|tput -S`. |
| 53 | 53 | ** Technically, this info could be cached, but then we'd need to handle |
| 54 | 54 | ** SIGWINCH signal to requery the terminal on resize event. |
| 55 | 55 | */ |
| 56 | -int terminal_get_size(struct TerminalSize *t) | |
| 57 | -{ | |
| 56 | +int terminal_get_size(TerminalSize *t){ | |
| 58 | 57 | memset(t, 0, sizeof(*t)); |
| 59 | 58 | |
| 60 | 59 | #if defined(TIOCGSIZE) |
| 61 | 60 | { |
| 62 | 61 | struct ttysize ts; |
| @@ -94,13 +93,12 @@ | ||
| 94 | 93 | |
| 95 | 94 | /* |
| 96 | 95 | ** Return the terminal's current width in columns when available, otherwise |
| 97 | 96 | ** return the specified default value. |
| 98 | 97 | */ |
| 99 | -unsigned int terminal_get_width(unsigned int nDefault) | |
| 100 | -{ | |
| 101 | - struct TerminalSize ts; | |
| 98 | +unsigned int terminal_get_width(unsigned int nDefault){ | |
| 99 | + TerminalSize ts; | |
| 102 | 100 | if( terminal_get_size(&ts) ){ |
| 103 | 101 | return ts.nColumns; |
| 104 | 102 | } |
| 105 | 103 | return nDefault; |
| 106 | 104 | } |
| @@ -107,13 +105,26 @@ | ||
| 107 | 105 | |
| 108 | 106 | /* |
| 109 | 107 | ** Return the terminal's current height in lines when available, otherwise |
| 110 | 108 | ** return the specified default value. |
| 111 | 109 | */ |
| 112 | -unsigned int terminal_get_height(unsigned int nDefault) | |
| 113 | -{ | |
| 114 | - struct TerminalSize ts; | |
| 110 | +unsigned int terminal_get_height(unsigned int nDefault){ | |
| 111 | + TerminalSize ts; | |
| 115 | 112 | if( terminal_get_size(&ts) ){ |
| 116 | 113 | return ts.nLines; |
| 117 | 114 | } |
| 118 | 115 | return nDefault; |
| 119 | 116 | } |
| 117 | + | |
| 118 | +/* | |
| 119 | +** COMMAND: test-terminal-size | |
| 120 | +** | |
| 121 | +** Show the size of the terminal window from which the command is launched | |
| 122 | +** as two integers, the width in charaters and the height in lines. | |
| 123 | +** | |
| 124 | +** If the size cannot be determined, two zeros are shown. | |
| 125 | +*/ | |
| 126 | +void test_terminal_size_cmd(void){ | |
| 127 | + TerminalSize ts; | |
| 128 | + terminal_get_size(&ts); | |
| 129 | + fossil_print("%d %d\n", ts.nColumns, ts.nLines); | |
| 130 | +} | |
| 120 | 131 |
| --- src/terminal.c | |
| +++ src/terminal.c | |
| @@ -51,12 +51,11 @@ | |
| 51 | ** Under Linux/bash the size info is also available from env $LINES, $COLUMNS. |
| 52 | ** Or it can be queried using tput `echo -e "lines\ncols"|tput -S`. |
| 53 | ** Technically, this info could be cached, but then we'd need to handle |
| 54 | ** SIGWINCH signal to requery the terminal on resize event. |
| 55 | */ |
| 56 | int terminal_get_size(struct TerminalSize *t) |
| 57 | { |
| 58 | memset(t, 0, sizeof(*t)); |
| 59 | |
| 60 | #if defined(TIOCGSIZE) |
| 61 | { |
| 62 | struct ttysize ts; |
| @@ -94,13 +93,12 @@ | |
| 94 | |
| 95 | /* |
| 96 | ** Return the terminal's current width in columns when available, otherwise |
| 97 | ** return the specified default value. |
| 98 | */ |
| 99 | unsigned int terminal_get_width(unsigned int nDefault) |
| 100 | { |
| 101 | struct TerminalSize ts; |
| 102 | if( terminal_get_size(&ts) ){ |
| 103 | return ts.nColumns; |
| 104 | } |
| 105 | return nDefault; |
| 106 | } |
| @@ -107,13 +105,26 @@ | |
| 107 | |
| 108 | /* |
| 109 | ** Return the terminal's current height in lines when available, otherwise |
| 110 | ** return the specified default value. |
| 111 | */ |
| 112 | unsigned int terminal_get_height(unsigned int nDefault) |
| 113 | { |
| 114 | struct TerminalSize ts; |
| 115 | if( terminal_get_size(&ts) ){ |
| 116 | return ts.nLines; |
| 117 | } |
| 118 | return nDefault; |
| 119 | } |
| 120 |
| --- src/terminal.c | |
| +++ src/terminal.c | |
| @@ -51,12 +51,11 @@ | |
| 51 | ** Under Linux/bash the size info is also available from env $LINES, $COLUMNS. |
| 52 | ** Or it can be queried using tput `echo -e "lines\ncols"|tput -S`. |
| 53 | ** Technically, this info could be cached, but then we'd need to handle |
| 54 | ** SIGWINCH signal to requery the terminal on resize event. |
| 55 | */ |
| 56 | int terminal_get_size(TerminalSize *t){ |
| 57 | memset(t, 0, sizeof(*t)); |
| 58 | |
| 59 | #if defined(TIOCGSIZE) |
| 60 | { |
| 61 | struct ttysize ts; |
| @@ -94,13 +93,12 @@ | |
| 93 | |
| 94 | /* |
| 95 | ** Return the terminal's current width in columns when available, otherwise |
| 96 | ** return the specified default value. |
| 97 | */ |
| 98 | unsigned int terminal_get_width(unsigned int nDefault){ |
| 99 | TerminalSize ts; |
| 100 | if( terminal_get_size(&ts) ){ |
| 101 | return ts.nColumns; |
| 102 | } |
| 103 | return nDefault; |
| 104 | } |
| @@ -107,13 +105,26 @@ | |
| 105 | |
| 106 | /* |
| 107 | ** Return the terminal's current height in lines when available, otherwise |
| 108 | ** return the specified default value. |
| 109 | */ |
| 110 | unsigned int terminal_get_height(unsigned int nDefault){ |
| 111 | TerminalSize ts; |
| 112 | if( terminal_get_size(&ts) ){ |
| 113 | return ts.nLines; |
| 114 | } |
| 115 | return nDefault; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | ** COMMAND: test-terminal-size |
| 120 | ** |
| 121 | ** Show the size of the terminal window from which the command is launched |
| 122 | ** as two integers, the width in charaters and the height in lines. |
| 123 | ** |
| 124 | ** If the size cannot be determined, two zeros are shown. |
| 125 | */ |
| 126 | void test_terminal_size_cmd(void){ |
| 127 | TerminalSize ts; |
| 128 | terminal_get_size(&ts); |
| 129 | fossil_print("%d %d\n", ts.nColumns, ts.nLines); |
| 130 | } |
| 131 |