Skip to content

Commit

Permalink
Move xolox#misc#timer#format_timespan() → xolox#misc#format#timestamp()
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Jun 2, 2013
1 parent 7c7d70f commit 1e4f278
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 72 deletions.
16 changes: 9 additions & 7 deletions README.md
Expand Up @@ -39,7 +39,7 @@ that I haven't published yet.
<!-- Start of generated documentation -->

The documentation of the 43 functions below was extracted from
13 Vim scripts on June 2, 2013 at 16:58.
15 Vim scripts on June 2, 2013 at 18:21.

### Handling of special buffers

Expand Down Expand Up @@ -127,6 +127,14 @@ Microsoft Windows).

[shellslash]: http://vimdoc.sourceforge.net/htmldoc/options.html#'shellslash'

### Human friendly string formatting for Vim

#### The `xolox#misc#format#timestamp()` function

Format a time stamp (a string containing a formatted floating point
number) into a human friendly format, for example 70 seconds is phrased as
"1 minute and 10 seconds".

### List handling functions

#### The `xolox#misc#list#unique()` function
Expand Down Expand Up @@ -424,12 +432,6 @@ handling as Vim's [printf()] [printf] function with one difference: At the
point where you want the elapsed time to be embedded, you write `%s` and
you pass the list returned by `xolox#misc#timer#start()` as an argument.

#### The `xolox#misc#timer#format_timespan()` function

Format a time stamp (a string containing a formatted floating point
number) into a human friendly format, for example 70 seconds is phrased as
"1 minute and 10 seconds".

<!-- End of generated documentation -->

## Contact
Expand Down
2 changes: 1 addition & 1 deletion autoload/xolox/misc.vim
Expand Up @@ -4,4 +4,4 @@
" Last Change: June 2, 2013
" URL: http://peterodding.com/code/vim/misc/

let g:xolox#misc#version = '1.1.2'
let g:xolox#misc#version = '1.1.3'
35 changes: 35 additions & 0 deletions autoload/xolox/misc/format.vim
@@ -0,0 +1,35 @@
" Human friendly string formatting for Vim.
"
" Author: Peter Odding <peter@peterodding.com>
" Last Change: June 2, 2013
" URL: http://peterodding.com/code/vim/misc/

function! xolox#misc#format#timestamp(ts) " {{{1
" Format a time stamp (a string containing a formatted floating point
" number) into a human friendly format, for example 70 seconds is phrased as
" "1 minute and 10 seconds".
let seconds = a:ts + 0
" Fast common case with extra precision from reltime().
if seconds < 5
let extract = matchstr(a:ts, '^\d\+\(\.0*[1-9][1-9]\?\)\?')
if extract =~ '[123456789]'
return extract . ' second' . (extract != '1' ? 's' : '')
endif
endif
" Generic but slow code.
let result = []
for [name, size] in [['day', 60 * 60 * 24], ['hour', 60 * 60], ['minute', 60], ['second', 1]]
if seconds >= size
let counter = seconds / size
let seconds = seconds % size
let suffix = counter != 1 ? 's' : ''
call add(result, printf('%i %s%s', counter, name, suffix))
endif
endfor
" Format the resulting text?
if len(result) == 1
return result[0]
else
return join(result[0:-2], ', ') . ' and ' . result[-1]
endif
endfunction
48 changes: 3 additions & 45 deletions autoload/xolox/misc/timer.vim
@@ -1,7 +1,7 @@
" Timing of long during operations.
"
" Author: Peter Odding <peter@peterodding.com>
" Last Change: May 20, 2013
" Last Change: June 2, 2013
" URL: http://peterodding.com/code/vim/misc/

if !exists('g:timer_enabled')
Expand Down Expand Up @@ -48,56 +48,14 @@ endfunction
function! s:convert_value(value) " {{{1
if type(a:value) != type([])
return a:value
elseif !empty(a:value)
else
if s:has_reltime
let ts = xolox#misc#str#trim(reltimestr(reltime(a:value)))
else
let ts = localtime() - a:value[0]
endif
return xolox#misc#timer#format_timespan(ts)
else
return '?'
endif
endfunction

" Format number of seconds as human friendly description.

let s:units = [['day', 60 * 60 * 24], ['hour', 60 * 60], ['minute', 60], ['second', 1]]

function! xolox#misc#timer#format_timespan(ts) " {{{1
" Format a time stamp (a string containing a formatted floating point
" number) into a human friendly format, for example 70 seconds is phrased as
" "1 minute and 10 seconds".

" Convert timespan to integer.
let seconds = a:ts + 0

" Fast common case with extra precision from reltime().
if seconds < 5
let extract = matchstr(a:ts, '^\d\+\(\.0*[1-9][1-9]\?\)\?')
if extract =~ '[123456789]'
return extract . ' second' . (extract != '1' ? 's' : '')
endif
endif

" Generic but slow code.
let result = []
for [name, size] in s:units
if seconds >= size
let counter = seconds / size
let seconds = seconds % size
let suffix = counter != 1 ? 's' : ''
call add(result, printf('%i %s%s', counter, name, suffix))
endif
endfor

" Format the resulting text?
if len(result) == 1
return result[0]
else
return join(result[0:-2], ', ') . ' and ' . result[-1]
return xolox#misc#format#timestamp(ts)
endif

endfunction

" vim: ts=2 sw=2 et
43 changes: 24 additions & 19 deletions doc/misc.txt
Expand Up @@ -17,28 +17,30 @@ Contents ~
1. The |xolox#misc#escape#pattern()| function
2. The |xolox#misc#escape#substitute()| function
3. The |xolox#misc#escape#shell()| function
4. List handling functions |misc-list-handling-functions|
4. Human friendly string formatting for Vim |misc-human-friendly-string-formatting-for-vim|
1. The |xolox#misc#format#timestamp()| function
5. List handling functions |misc-list-handling-functions|
1. The |xolox#misc#list#unique()| function
2. The |xolox#misc#list#binsert()| function
5. Functions to interact with the user |misc-functions-to-interact-with-user|
6. Functions to interact with the user |misc-functions-to-interact-with-user|
1. The |xolox#misc#msg#info()| function
2. The |xolox#misc#msg#warn()| function
3. The |xolox#misc#msg#debug()| function
6. Integration between Vim and its environment |misc-integration-between-vim-its-environment|
7. Integration between Vim and its environment |misc-integration-between-vim-its-environment|
1. The |xolox#misc#open#file()| function
2. The |xolox#misc#open#url()| function
7. Vim and plug-in option handling |misc-vim-plug-in-option-handling|
8. Vim and plug-in option handling |misc-vim-plug-in-option-handling|
1. The |xolox#misc#option#get()| function
2. The |xolox#misc#option#split()| function
3. The |xolox#misc#option#join()| function
4. The |xolox#misc#option#split_tags()| function
5. The |xolox#misc#option#join_tags()| function
6. The |xolox#misc#option#eval_tags()| function
8. Operating system interfaces |misc-operating-system-interfaces|
9. Operating system interfaces |misc-operating-system-interfaces|
1. The |xolox#misc#os#is_win()| function
2. The |xolox#misc#os#find_vim()| function
3. The |xolox#misc#os#exec()| function
9. Pathname manipulation functions |misc-pathname-manipulation-functions|
10. Pathname manipulation functions |misc-pathname-manipulation-functions|
1. The |xolox#misc#path#which()| function
2. The |xolox#misc#path#split()| function
3. The |xolox#misc#path#join()| function
Expand All @@ -51,16 +53,15 @@ Contents ~
10. The |xolox#misc#path#decode()| function
11. The |xolox#misc#path#is_relative()| function
12. The |xolox#misc#path#tempdir()| function
10. String handling |misc-string-handling|
11. String handling |misc-string-handling|
1. The |xolox#misc#str#compact()| function
2. The |xolox#misc#str#trim()| function
11. Tests for my miscellaneous Vim scripts |tests-for-my-miscellaneous-vim-scripts|
12. Tests for my miscellaneous Vim scripts |tests-for-my-miscellaneous-vim-scripts|
1. The |xolox#misc#tests#run_all()| function
12. Timing of long during operations |misc-timing-of-long-during-operations|
13. Timing of long during operations |misc-timing-of-long-during-operations|
1. The |xolox#misc#timer#start()| function
2. The |xolox#misc#timer#stop()| function
3. The |xolox#misc#timer#force()| function
4. The |xolox#misc#timer#format_timespan()| function
4. Contact |misc-contact|
5. License |misc-license|
6. References |misc-references|
Expand Down Expand Up @@ -110,8 +111,8 @@ that I haven't published yet.

Start of generated documentation

The documentation of the 43 functions below was extracted from 13 Vim scripts
on June 2, 2013 at 16:58.
The documentation of the 43 functions below was extracted from 15 Vim scripts
on June 2, 2013 at 18:21.

-------------------------------------------------------------------------------
*misc-handling-of-special-buffers*
Expand Down Expand Up @@ -199,6 +200,17 @@ really, it won't make any difference. Let's just suffice to say that I have yet
to encounter a single person out there who uses this option for its intended
purpose (running a UNIX style shell on Microsoft Windows).

-------------------------------------------------------------------------------
*misc-human-friendly-string-formatting-for-vim*
Human friendly string formatting for Vim ~

-------------------------------------------------------------------------------
The *xolox#misc#format#timestamp()* function

Format a time stamp (a string containing a formatted floating point number)
into a human friendly format, for example 70 seconds is phrased as "1 minute
and 10 seconds".

-------------------------------------------------------------------------------
*misc-list-handling-functions*
List handling functions ~
Expand Down Expand Up @@ -532,13 +544,6 @@ handling as Vim's |printf()| function with one difference: At the point where
you want the elapsed time to be embedded, you write '%s' and you pass the list
returned by |xolox#misc#timer#start()| as an argument.

-------------------------------------------------------------------------------
The *xolox#misc#timer#format_timespan()* function

Format a time stamp (a string containing a formatted floating point number)
into a human friendly format, for example 70 seconds is phrased as "1 minute
and 10 seconds".

End of generated documentation

===============================================================================
Expand Down

0 comments on commit 1e4f278

Please sign in to comment.