[Tex/LaTex] How to measure the width of items in itemize environment

itemizelistswidth

I want the output of the code below to be like this image. How to do this?
Please keep in mind that the width of [Foo Bar Foo]'s is variable. So it should be calculated automatically.

\documentclass{book}
\begin{document}
\noindent Just enough text to make the line break so we get to see a second line.
\begin{itemize}
  \item[Foo] An item
  \item[Foo Bar Foo] An item An item An item An item An item An item An item An item An item An item An item An item An item An item  
  \item[Bar Bar] An item 
\end{itemize}
Just enough text to make the line break so we get to see a second line.
\end{document}

enter image description here

Edit: I don't want to use tabular or similar environment, because there are many items which should span some pages.

Best Answer

If you don't have dangerous things like labels or counter settings (numbered equations, for instance) in the body of the itemize, you can locally redefine \item to do the measurement:

\documentclass{article}
\usepackage{enumitem,environ}

\newlength{\xitemlenx}
\newlength{\xitemleny}
\NewEnviron{xitemize}{%
  \setbox0=\vbox{
    \xitemlenx=0pt
    \def\item[##1]{\measureitem{##1}}
    \BODY
    \global\xitemleny\xitemlenx
  }%
  \itemize[
    leftmargin=\dimexpr\xitemleny+\labelsep,
    labelwidth=\xitemleny,
    align=left
  ]
  \BODY
  \enditemize
}
\newcommand{\measureitem}[1]{%
  \settowidth{\dimen0}{#1}%
  \ifdim\dimen0>\xitemlenx
    \xitemlenx=\dimen0
  \fi
}

\begin{document}

Just enough text to make the line break so we get to see a second line.
Just enough text to make the line break so we get to see a second line.
\begin{xitemize}
  \item[Foo] An item
  \item[Foo Bar Foo] An item An item An item An item An item An item An item An item An item An item An item An item An item An item  
  \item[Bar Bar] An item 
\end{xitemize}
Just enough text to make the line break so we get to see a second line.
Just enough text to make the line break so we get to see a second line.
\end{document}

enter image description here

If you need a single item, as you seem to from your comments, it's easier because just one label needs to be measured; here's the idea, integrated in the previous code.

\documentclass{article}
\usepackage{enumitem,environ}

\newlength{\xitemlenx}
\newlength{\xitemleny}
\NewEnviron{xitemize}{%
  \setbox0=\vbox{
    \xitemlenx=0pt
    \def\item[##1]{\measureitem{##1}}
    \BODY
    \global\xitemleny\xitemlenx
  }%
  \itemize[
    leftmargin=\dimexpr\xitemleny+\labelsep,
    labelwidth=\xitemleny,
    align=left
  ]
  \BODY
  \enditemize
}
\newcommand{\measureitem}[1]{%
  \settowidth{\dimen0}{#1}%
  \ifdim\dimen0>\xitemlenx
    \xitemlenx=\dimen0
  \fi
}

\newcommand{\singleitem}[2]{%
  \settowidth{\xitemleny}{#1}%
  \begin{itemize}[
    leftmargin=\dimexpr\xitemleny+\labelsep,
    labelwidth=\xitemleny,
    align=left
  ]
  \item[#1]\textbf{#2}
  \end{itemize}%
}

\begin{document}

Just enough text to make the line break so we get to see a second line.
Just enough text to make the line break so we get to see a second line.
\begin{xitemize}
  \item[Foo] An item
  \item[Foo Bar Foo] An item An item An item An item An item An item An item An item An item An item An item An item An item An item  
  \item[Bar Bar] An item 
\end{xitemize}
Just enough text to make the line break so we get to see a second line.
Just enough text to make the line break so we get to see a second line.
Now a single item:
\singleitem{Foo Bar}{An item An item An item An item An item An item An item An item An item An item An item An item An item An item}
Just enough text to make the line break so we get to see a second line.
Just enough text to make the line break so we get to see a second line.

\end{document}

enter image description here